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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Image URL with SetWidth(


Go to End


3 Posts   4649 Views

Avatar
DeklinKelly

Community Member, 197 Posts

31 July 2010 at 8:57am

How can I get the URL for an image resized with SetWidth().

This does NOT work:

<img src="$Image.SetWidth(150).URL" alt="$Heading.ATT" title="$Heading.ATT" />

Avatar
ryanwachtl

Community Member, 46 Posts

31 July 2010 at 10:12am

Edited: 31/07/2010 10:13am

You can't. You won't be able to use URL after SetWidth(). There is an example in the SilverStripe book on this, done by Subclassing Image.

example:

class Page_MyImage extends Image {
	
	function generateMyImageThumbnail($gd) {
		return $gd->resizeByWidth(50);
	}
	
}

Where Page_ is your class, such as Page or HomePage, etc.

Then update your has_one

	static $has_one = array(
		'Image' => 'Page_MyImage',
	);

And you should be able to use in your template like:

$Image.MyImageThumbnail.URL

You can change "MyImage" and "MyImageThumbnail" to whatever you like.

Avatar
bummzack

Community Member, 904 Posts

31 July 2010 at 8:13pm

There's actually a simpler solution than subclassing. This code always worked for me:

<% control Image %>
<img src="$SetWidth(150).Link" alt="$Heading.ATT" title="$Heading.ATT" />
<% end_control %>

Don't know where that heading is coming from, eventually you'll have to use $Top.Heading, or something along these lines (ugly, I know):

<img src="<% control Image %>$SetWidth(150).Link<% end_control %>" alt="$Heading.ATT" title="$Heading.ATT" />