NetpbmResize
A straightforward image resizing solution for PHP.
A straightforward image resizing solution for PHP.
Getting right to the point, you can distinguish two steps when performing resize operations.
1) Initialize and 2) Resize.
There could be an intermediate step where you set some options.
The constructor takes two parameters: the first is the path to the netpbm binaries, and the second is the working directory or a full path to an image (in this case, the working directory is the directory where the image is located).
Directory paths need to be passed without the trailing slash.
<?php require PATH_TO_CLASS . 'netpbmresize.class.php'; $netpbm =& new NetpbmResize(NETPBM_PATH, '/path/to/myimage.jpg'); // or $netpbm =& new NetpbmResize(NETPBM_PATH, '/path/to/working_dir'); ?>
You can resize the initialized image or pass another image as the second parameter for all the resize methods. The default method is by a square Bounding Box.
<?php $netpbm->resize(100); // or $netpbm->resize(100, 'path/to/myotherimage.jpg'); ?>
Right now, you are probably wondering: what happens to the resized image?
By default, the resulting image is stored in the working directory. The class does not delete the original image. The filename of the resized image is the same name as the original but with a suffix. The default suffix is: rs. For example: “image.jpg” would then be “image.rs.jpg”, you can change the suffix by using setSuffix and for maximum control setOutputImage:
<?php $netpbm->setSuffix('my-pretty.suffix'); // or // custom name $netpbm->setOutputImage('IDontLikeSuffixes'); ?>
Suppose you have an application that handles weekly events. An event in this case would be a birthday party, a wedding, an anniversary, etc. For each event you can have a gallery of photos. There are two options: we can just create thumbnails or we can create different sizes and a thumbnail. Let's see:
<?php // $Images contains all the uploaded photos in a nicely formatted array $Images = getFileUploadArray(); require_once PATH_TO_CLASS . 'netpbmresize.class.php'; $netpbm =& new NetpbmResize('/path/to/binaries', '/my/working_dir'); foreach ($Images as $Image) { $netpbm->resize(75, $Image['name']); } ?>
<?php // $Images contains all the uploaded photos in a nicely formatted array $Images = getFileUploadArray(); require_once PATH_TO_CLASS . 'netpbmresize.class.php'; $netpbm =& new NetpbmResize('/path/to/binaries', '/my/working_dir'); foreach ($Images as $Image) { $netpbm->resize($sizes, $Image['name']); $netpbm->squareThumbnail(75); } ?>
For more insight about using NetpbmResize take a look at the API documentation and the test file which uses SimpleTest and is available through SVN or here.