Jump to:

23545 Posts in 19310 Topics by 2888 members

General Questions

SilverStripe Forums » General Questions » Resizing an Image?

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: 568 Views
  • Pix
    Avatar
    Community Member
    145 Posts

    Resizing an Image? Link to this post

    Hi!

    I made a shortcode to embed images that I upload through the files and images tab. In my template, I would like to be able to resize this image but I can not really see how to do that. I've done it before using $SetWidth, ResizedImage, etc, with images in custom page types but it does not seem to work with the URL to the image I generate with my shortcode pardser. is that because it is an "image object" when it is a page type? I hope I am making sense here, I'm on the edge of my SilverStripe knowledge coding in the dark :0)

    Basically I want to use a resize function on the source for an image tag, hopefully saving and caching the resized image like it seems to do in SS. How can I do that?

    Thank you!

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Resizing an Image? Link to this post

    You could try something like this - where SpecialImage returns an image object. From memory I think you can use for instance $SpecialImage.CroppedImage(200,100) to render the whole image tag, but <% control SpecialImage.CroppedImage(200,100) %> won't work, hence two control blocks.

          <% if SpecialImage %>
       
             <% control SpecialImage %>
                
                <% control CroppedImage(200,100) %>
                
                   <img src="$Link" alt="" width="$Width" height="$Height" />
                   
                <% end_control %>
                
             <% end_control %>
             
          <% end_if %>

  • Pix
    Avatar
    Community Member
    145 Posts

    Re: Resizing an Image? Link to this post

    Thank you for your reply!

    But what is "SpecialImage"? Is that something particular to SS, or is that something I have to add to my page? I think that is my problem in that I am not dealing with an Image Object (at least I don't think I am), I just want to resize the image on a plain old image tag like:

    <a class="rfb" href="./assets/Uploads/$directory/$imagename" title="$caption"><img alt="$caption" src="./assets/Uploads/$directory/$imagename->SetHeight(100);" /></a>

    only that doesn't do anything

    If I take out the resize I see the image just fine, so I know the shortcode and template is working, like this:
    <a class="rfb" href="./assets/Uploads/$directory/$imagename" title="$caption"><img alt="$caption" src="./assets/Uploads/$directory/$imagename" /></a>

    See what I mean? How would I resize a dynamically generated image tag that I am assuming is not an image object?

  • Pix
    Avatar
    Community Member
    145 Posts

    Re: Resizing an Image? Link to this post

    Hi,

    Does anyone have any idea on this? Is there a way with SS to resize an image on an image tag? The image is not an Image Object attached to the page it's just in a folder on my site and I am generating an image tag to put it on the page. I've seens some PHP scripts for creating thumbnails on the fly and caching them, I suppose I could work that in somehow, but I was wondering if there was a way to do it with SS.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Resizing an Image? Link to this post

    Hi Pix,

    If you want to use SilverStripe's image functions you'll need to be working with an image object. So if you have some kind of shortcode function - rather than using it to return a string such as the directory name, make your function fetch the relevant image (i.e. using a DataObject::get) and return it to the template so you can access SilverStripe's built in image functions.

    Also $imagename->SetHeight(100) wouldn't work in a template even if $imagename was an image object, the syntax would be $imagename.SetHeight(100)

    568 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.