How to use the NetpbmResize class

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.

1. Initialize

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.

  1. <?php
  2. require PATH_TO_CLASS . 'netpbmresize.class.php';
  3.  
  4. $netpbm =& new NetpbmResize(NETPBM_PATH, '/path/to/myimage.jpg');
  5. // or
  6. $netpbm =& new NetpbmResize(NETPBM_PATH, '/path/to/working_dir');
  7. ?>

2. Resize

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.

  1. <?php
  2. $netpbm->resize(100);
  3. // or
  4. $netpbm->resize(100, 'path/to/myotherimage.jpg');
  5. ?>

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:

  1. <?php
  2. $netpbm->setSuffix('my-pretty.suffix');
  3. // or
  4. $netpbm->setSuffix(md5(time()));
  5. // custom name
  6. $netpbm->setOutputImage('IDontLikeSuffixes');
  7. ?>

Real world example

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:

Just create a thumbnail for each uploaded photo

  1. <?php
  2. // $Images contains all the uploaded photos in a nicely formatted array
  3. $Images = getFileUploadArray();
  4.  
  5. require_once PATH_TO_CLASS . 'netpbmresize.class.php';
  6. $netpbm =& new NetpbmResize('/path/to/binaries', '/my/working_dir');
  7.  
  8. foreach ($Images as $Image) {
  9. $netpbm->resize(75, $Image['name']);
  10. }
  11. ?>

Create different sizes from the original photo and make a square thumbnail

  1. <?php
  2. // $Images contains all the uploaded photos in a nicely formatted array
  3. $Images = getFileUploadArray();
  4.  
  5. require_once PATH_TO_CLASS . 'netpbmresize.class.php';
  6. $netpbm =& new NetpbmResize('/path/to/binaries', '/my/working_dir');
  7.  
  8. $sizes = array(200 => '_s', 400 => '_m');
  9. foreach ($Images as $Image) {
  10. $netpbm->resize($sizes, $Image['name']);
  11. $netpbm->squareThumbnail(75);
  12. }
  13. ?>

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.