PHP Object Oriented Image Manipulation

Making thumbnails from photos or as part of an upload script can be done in a few lines of PHP.

Taking this source image we can produce this smaller image:

Image scaled to be within 400x300

using the following code:

  <?php
  require('class.image.php');
  
  // instantiate the object
  $image = new Image('image.jpg');
  // the actual resize
  $image->scale(400, 300);
  // you could also call:
  // $image->scale(0, 300);
  // to scale based on height only, width is the same
  // there are also __set()ters for x, y, width and height that you can use like this:
  // $image->x = 400;
  // to scale based on one dimension
  
  // this writes the file to disk as small_image.jpg
  // $image->write('small_image.jpg');
  // the image extension can be omitted and the default will be used, the filename written to is returned by the call to write()
  // [Note: If you use a '.' in your filename the system will not append the extension unless you set the option extention
  // to true] 
  // alternatively you could output the image using:
  $image->output();
  // 
  // you can choose to output an image in a different format to the input type
  // this is done by passing a option at start up (see the class options for more details) or by __set()ing $image->type to one of
  // PHPs IMAGETYPE_XXX constants before the call to ouput or write:
  // $image->type = IMAGETYPE_JPEG;
  ?>
    

Also built in is a watermarking method:

  <?php
  require('class.image.php');
  
  $image = new Image('image.jpg');
  $image->scale(400, 300);
  $image->watermark('sample.png'); // or $image->watermark = 'sample.png';
  $image->output();
  ?>
    

Image scaled to be within 400x300 with a watermark applied

A 'double watermark' is also possible by calling the watermark function more than once:

  <?php
  require('class.image.php');

  $image = new Image('image.jpg');
  $image->watermark('sample.png'); // or $image->watermark = 'sample.png';
  $image->scale(400, 300);
  $image->watermark('sample.png'); // or $image->watermark = 'sample.png';
  $image->output();
  ?>
    

Image scaled to be within 400x300 with a watermark applied before and after the scaling

Cutouts:

  <?php
  require('class.image.php');
  
  $image = new Image('image.jpg');
  $image->cutout(600, 200);
  $image->output();
  ?>
    

Image of 600x200 cut out of the source image

Whitespace:

  <?php
  require('class.image.php');
  
  $image = new Image('image.jpg');
  $image->whitespace(400, 300, array('color' => '#ffc'));
  $image->output();
  ?>
    

Image scaled to 400x300 exactly padding the missing space using a colour

Whitespace using an image:

  <?php
  require('class.image.php');
  
  $image = new Image('image.jpg');
  $image->whitespace(400, 300, array('image' => 'bg.jpg'));
  $image->output();
  ?>
    

Image scaled to 400x300 exactly padding the missing space using another image

Captions:

  <?php
  require('class.image.php');
  
  $image = new Image('image.jpg');
  $image->scale(400, 300);
  $image->drawBox(array(0, 230), array(400, 300), array('size' => 4, 'color' => '#000', 'transparency' => 60));
  $image->addText('Red Berries © dom111.co.uk 2009', 30, ($image->currentY - 13), array('color' => '#fff', 'font' => 'ab', 'transparency' => 30));
  $image->output();
  ?>
    

Image scaled to 400x300 with a caption overlaying the image at the bottom

Get it here (includes a sample file and image)