Laziest image resize in PHP
Today I saw a post at digg.com on image resizing with PHP and there was quite a discussion. Let me share the laziest way (that I know of) how to do it - PEAR::Image_Transform is all it takes. Here goes:
require_once 'Image/Transform.php'; $i =& Image_Transform::factory(''); $i->load('test.jpg'); $i->fit(100,100); $i->save('resized.png', 'png');
In addition, the Image_Transform library offers diffferent ways (to skin the old cat) to resize an image - by given pixel value, only on the X axis, on Y, scalling in percentage and so on. And, of course, the library can do much more than resizing, as you can see in the API docs.
It supports all kinds of image manipulation extensions - GD, GD1, ImageMagick, NetPBM, Imlib... If you want to use a specific one, you set as a parameter to the factory() method. In the example above I passed an empty string, so it will try to figure out what's available in my PHP setup and use it, trying Imagick2 first, then GD, then Imlib.
You have setOption() and setOptions() methods if you want to play around with the image quality and those sort of things.
This entry was posted on Wednesday, December 13th, 2006 and is filed under images, PEAR, php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

December 14th, 2006 at 8:40 am
Stoyan Stefanov’s Blog: Laziest image resize in PHP…
…
December 15th, 2006 at 4:40 am
One drawback I find disturbing about this package is that you can only do one tranformation per image/object. When you want to do a resize and then a crop you get a notice that only one transformation is allowed, if I remember correctly.
January 30th, 2007 at 12:21 pm
@Marc
I found many of PEAR’s libraries to be lacking in substance. For image transformation, i have my own lib. As for the case that you pointed out, the code using my lib would be this:
$img = Momo_Image::load(‘test.png’);
$resized = Momo_Image::resize($img, array(‘width’ => 500)); // It’ll figure out the height by proportion
$cropped = Momo_Image::crop($resized, array(‘start_x’ => 10, ‘start_y’ => 100, ‘width’ => 200, ‘height’ => 300)); // If you omit start_x or start_y, they will default to 0
Momo_Image::render($cropped, array(‘file_path’ => ‘path/to/file.png’));
I avoid Object Oriented Programming for all of my libraries. Makes my life easier when refactoring.
March 3rd, 2007 at 6:03 pm
@Jamees
is your image library avaliable?
was planning to use Pear, but I agree it is a little basic.
March 27th, 2007 at 3:17 pm
Right on man. The best coders are lazy coders. Thanks for the tip!
October 31st, 2007 at 10:31 am
I’m using very simply program picture resizer, and I have not any problem.
February 23rd, 2008 at 4:47 pm
Hi.
Sorry for the perheps dumb question, but I’m really having troubles with using PEAR libraries since I’m not good at all at OOP.
So when trying to use Image_Transform it tells me “Image library not supported… aborting”.
Could you explain how to fix it?
I suppose that the problem is in the way the PEAR is installed at my web-site.
I don’t know if it is installed at my host, so at some foums I was suggested just to place it in a local folder at my web-site and just include it to my pages.
The PEAR extantions I ‘plug’ in the same way.
Do you think there’s any better solution?
February 23rd, 2008 at 4:56 pm
Sorry for flood.
I fixed my problem.
I had to just to change all the path in icludes.
May 11th, 2008 at 8:46 am
[...] In addition, the Image_Transform library offers diffferent ways (to skin the old cat) to resize an image – by given pixel value, only on the X axis, on Y, scalling in percentage and so on. And, of course, the library can do much more than resizing, as you can see in the API docs.4 [...]
July 17th, 2008 at 10:39 am
There is no need to host CPU-intensive thumbnailing scripts like this on your webserver anymore. Instead, you can use a specialized service for remote, on-demand image manipulation like SteadyOffload.
All you have to do is use a custom HTML attribute called “xmanip” with the img tag. This will deliver the thumbnail from one of the globally scattered cache servers:
<img srcx=”image.jpg” xmanip=”RescaleWidth 130″ />
So much easier than all the hassle with GD or ImageMagick! Moreover, this reduces the CPU load of your webserver and saves some bandwidth.
November 7th, 2008 at 5:39 am
Nice & short script … but
if you need simple PHP script to resize many images at once from command line, I have one. I also used ImageMagick tools, so PHP script will work without GD library. Hope this will help …
January 12th, 2009 at 10:33 am
Nice code lines I myself use exactley the same method! PEAR is great but if you like OO programming check out Zend Framework! (http://framework.zend.com/) It is a superb collection of classes and I risk that is better than PEAR.
And Jim: That’s cool to provide such service but when it comes to sensitive data you cannot really afford to outsource your image data processing. On the other hand resizing a picture (once at initial processing/adding to database etc) should not raise such a big CPU load. Taking that these libraryies are single threaded and a really lot of servers are multi-cored and users are resource limited if should not be a risk.
So I do not think it is a good idea to outsource image resize jobs. The optimal solution is still making your own cache of resized images and store image information in database.
March 5th, 2009 at 9:04 pm
I was googl’ing and found the same Digg discussion you were talking about – and the first thing that came to my mind was ‘doesn’t PEAR do this?’… and then I stumbled on this post
October 3rd, 2009 at 5:46 pm
I am getting the below error
Fatal error: Call to undefined method PEAR_Error::load() in /home/knowtho2/public_html/new1/photos/upload_file.php on line 41
code : $i->load(‘test.jpg’);
Am I missing any pear or Image libraries?
October 24th, 2009 at 9:01 pm
Wow, this is so simple! You just saved me an hour. Thanks!
October 1st, 2010 at 5:54 am
Ok, how to use this if the image names are coming out of database through a while loop? Thats how i am displaying images and thats where i need to resize the images.
June 15th, 2011 at 5:36 am
horrible