Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Image URL with SetWidth(

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

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

Page: 1
Go to End
Author Topic: 2206 Views
  • DeklinKelly
    Community Member
    197 Posts

    Image URL with SetWidth( Link to this post

    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" />

  • ryanwachtl
    Community Member
    46 Posts

    Re: Image URL with SetWidth( Link to this post

    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.


    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:


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

  • banal
    Community Member
    901 Posts

    Re: Image URL with SetWidth( Link to this post

    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" />

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.