Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Resizing images

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: 1634 Views
  • k0m0r
    Avatar
    Community Member
    39 Posts

    Resizing images Link to this post

    Hi.
    I'm trying to generate thumbnails based on image orientation. I would like to resize the image keeping its ratio by its smaller dimension, and then crop the rest so it fits the given area.
    For example:

    source: 200x150px
    target: 100x100px

    Image orientation is landscape, so I take the smaller dimension (height: 150px).
    I resize it using SetHeight(100) to 133x100px and then use CroppedImage(100,100) to crop the center part of the processed image.

    However, first usage of getFormattedImage() returns Image_Cached object and for some reason I cannot use the function again on this object, because it returns null.

    How do I make the following code to work?

    function getThumbLandscape() {

    $img = $this->Image();
    print_r($img); // this prints out Image object

    $img = $img->getFormattedImage('SetHeight',100);      
    print_r($img); // this prints out Image_Cached object

    $img = $img->getFormattedImage('CroppedImage',100,100);
    print_r($img); // this prints out null ???!!?

    return $img;
    }

    Please help!

  • k0m0r
    Avatar
    Community Member
    39 Posts

    Re: Resizing images Link to this post

    Anyone?

  • kuenkuen82
    Avatar
    Community Member
    41 Posts

    Re: Resizing images Link to this post

    My solution is to use the Image_Cached ID and get the Image from the Filing System (I think there is a few ways to do this, if you know please post it up, I'll be look at the feed)

    ----
    This doesn't work as cached Files are not part of the Filing system

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: Resizing images Link to this post

    Has anyone managed to find a solution for this yet?

    Just to add my two cents. I've got this to the point of returning a filename of the twice resized image, but I'd really like to return an image object.

    Here's my code: http://www.sspaste.com/paste/show/4f1ccf5a0e737

  • Plato
    Avatar
    Community Member
    26 Posts

    Re: Resizing images Link to this post

    Basically you need to use a function that returns (and works with) a GD object rather than a Image_Cached object.

    The issue is that you're using SetHeight directly rather than using a generate function, which returns an Image_Cached rather than a GD object. You need to get your hands on the GD object to make manipulations.

    The getFormatedImage is an access function that serves up the GD object to the appropriate function and returns an Image_Cached. So you only really want to use this when accessing from a template (through something like SetHeight, etc, which is a function that calls getFormattedImage).

    I have 2 solutions for you (both in the same file). One lazy hack that takes 3 lines of code, and one more efficient code that is basically a bunch of copy and paste.
    http://sspaste.com/paste/show/4f1cd4494bb76

    Solution 1 (the more efficient) is to apply the two extensions to their appropriate classes (the one prefixed Image to Image, and the one prefixed GD to the GD class).

    Solution 2 is a bad idea as it gobbles up memory as you create new GD objects twice as much as is necessary. But if you look at lines 1-11 of that paste, substitute the commented lines (9 & 10) for the one above (8), and that's pretty much it.

    From here I hope you can see where you were going wrong. Once you've got the GD object you can make manipulations as you would using it's member functions, and you can extend it using php's GD functions as you wish.

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