Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Getting the URL of a resized image


Reply


5 Posts   771 Views

Avatar
Lime Blast

Community Member, 22 Posts

23 May 2013 at 2:13am

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 :)

Avatar
Lime Blast

Community Member, 22 Posts

23 May 2013 at 2:18am

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?

Avatar
Devlin

Community Member, 225 Posts

27 May 2013 at 11:46pm

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.

Avatar
Bambii7

Community Member, 254 Posts

29 May 2013 at 3:30pm

I use this

$Image.SetSize( 75, 75 ).getAbsoluteURL

Avatar
Lime Blast

Community Member, 22 Posts

29 May 2013 at 8:11pm

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