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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Getting the URL of a resized image


Go to End


7 Posts   5265 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, 344 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?

Avatar
Friizu

Community Member, 17 Posts

29 February 2016 at 11:34pm

Edited: 29/02/2016 11:38pm

uh i searched same thing, sometimes u build template for gallery with some js and need re-sized/croped etc image path (not img tag with image)

Silver 3.x u can get it easy like $Image.SetSize(75,75).URL

example returns your image re-sized 75*75px path.

Ofc u can crop, scale re-size etc
https://docs.silverstripe.org/en/3.2/developer_guides/files/images/

That's not code for class, its code for template , can be used for any image and vals supported like for ex:
$MySiteConfig.CoverMain.CroppedImage(2000, 575).URL

tnx Bambii7 to help me figure that out fast ;)

Avatar
Friizu

Community Member, 17 Posts

3 March 2016 at 1:40am

This is what i finally did to get nice gallery

<% if Photos %>
<% loop Photos %>
<a class="photo" href="$SetRatioSize(1920,1200).URL">$CroppedImage(400,256) </a>
<% end_loop %>
<% end_if %>

So i set ratio for original image and got url back and then i cropped thumb .... ofc there is some javascript as well to make it look fancy but thats basic ...