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.
Post this entry to: » del.icio.us » Digg » Furl » Newsvine » reddit » Y!
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 […]