Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Getting the URL of a resized image

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 518 Views
  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Getting the URL of a resized image Link to this post

    I'm trying to build a custom function which returns the URL of a resized image..

    The deal is that I'm going to have a series of thumbnail images listed on the page, each of which will link to a larger version of the image. I don't want the linked to image to be the full size image, however, as this is likely to be too large, so I wanted to provide a link to a resized image.

    (Following me so far?)

    Right now the code in my template looks like this:

    <% if $Photos %>
       <ul class="project-photos">
          <% loop $Photos %>
             <li><a href="{$URL}">{$CroppedImage(210,210)}</a></li>
          <% end_loop %>
       </ul>
    <% end_if %>

    what I was planning on changing the `{$URL}` to something like `{$croppedURL(610)}`, and having a function on my Photo class (which extends the Image class) which returned the resized the URL, figuring I would do something like this:

    public function croppedURL($width = false) {
       if ($width != false) {
          $this->resizeByWidth($width);
       }
       return $this->URL();
    }

    But upon doing so, I get a Server Error 500.

    I'vee tried my best to make this work following the examples found on http://doc.silverstripe.org/framework/en/reference/image - but thus far I've not had any luck - can someone help please?

    Thank you

  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Re: Getting the URL of a resized image Link to this post

    As seems to be the story of my life right now - but no sooner than I finish posting this request for help, I find the answer.

    Having looked at http://www.ssbits.com/snippets/2009/resizing-an-image-in-a-custom-img-tag/ I've found that the solution to my problem already exists, I simply need to replace the `{$URL}` with `{$SetWidth(630).URL}` and it does exactly what I need it to do..

    .. so that's great, but returning to my original question, just for my own knowledge, can anyone tell me why my attempted solution didn't work?

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: Getting the URL of a resized image Link to this post

    can anyone tell me why my attempted solution didn't work?

    There is no method URL() in the Photo, Image or File class.

    It would had worked if you used $this->getURL() or $this->URL.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Getting the URL of a resized image Link to this post

    I use this

    $Image.SetSize( 75, 75 ).getAbsoluteURL

  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Re: Getting the URL of a resized image Link to this post

    @Bambii7 - is that the code for the class file?

    518 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.