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

Resizing images


Go to End


5 Posts   2287 Views

Avatar
k0m0r

Community Member, 39 Posts

4 August 2010 at 12:46am

Edited: 04/08/2010 12:48am

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!

Avatar
k0m0r

Community Member, 39 Posts

4 August 2010 at 10:27am

Anyone?

Avatar
kuenkuen82

Community Member, 41 Posts

2 September 2010 at 10:16pm

Edited: 02/09/2010 11:07pm

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

Avatar
Tama

Community Member, 138 Posts

23 January 2012 at 3:49pm

Edited: 23/01/2012 4:10pm

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

Avatar
Plato Creative

Community Member, 26 Posts

24 January 2012 at 8:46am

Edited: 24/01/2012 9:20am

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.