ImageBatchProcessor for PHP

1. Preamble

ImageBatchProcessor class can be used to perform different kind of operations over a group of images. It can proportionally resize images (create thumbnails), rotate images, convert between different image formats, append textual labels and/or small graphic watermarks to images, etc.

Transformation parameters may be applied to all images in a directory or separately to each image in a set - the images in source or destination sets may be located in the same or different directories.

The class also contains a traversal method which walks through a directory and calls a callback function for each item. The method may be used, for example, to rename all images (or other files) in a directory using predefined naming template (photo01.jpg, photo02.jpg, ...).

1.2. Requirements

1.3. License Agreement

The class is distributed under the MIT license and may be used for commercial and non-commercial purposes without any fee.

© 2008 Vagharshak Tozalakyan <vagh{at}tozalakyan{dot}com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2. Samples of Usage

2.1. Processing the Whole Directory

In the following example all JPEG images from the c:/mypicts/album1/ directory will be processed as thumbnails to the d:/album/ directory:

<?php

// Set script execution time limit to 10 minutes.
set_time_limit(600);

// Include class definition file.
require_once 'class.ImageBatchProcessor.php';

// Create transformation object.
$t = new ImageBatchTransformation();
// Source and destination directories.
$t->source 'c:/mypicts/album1/';
$t->destination 'd:/album/';
// Destination image format: TI_JPEG, TI_PNG or TI_GIF.
$t->format TI_JPEG;
// Destination JPEG quality: 0 - 100 or -1 for default value.
$t->jpegQuality = -1;
// Interlacing mode: TI_INTERLACE_ON or TI_INTERLACE_OFF.
$t->interlace TI_INTERLACE_ON;
// Maximal width and height of destination image.
$t->maxWidth 150;
$t->maxHeight 150;
// Force small images to enlarge to fit the maximal width and height.
$t->fitToMax false;
// Replace existed files in destination directory.
$t->replaceExisted true;

// Create batch processor object.
$ibp = new ImageBatchProcessor();

// Process batch transformation.
//
// First parameter is a trasformation object, second is optional filter of
// filenames (there are 2 predefined filters: the default IBP_IMAGE_REGEXP,
// which filters only images and IBP_ALL_REGEXP for all files), third is an
// optional maximum number of files to be processed.
//
// Returns the number of processed files.
$n $ibp->process($t'/^(.*)(\.jpg|\.jpeg)$/is'10);

printf('%d images processed.'$n);

?>

2.2. Processing the Files Separately

The following example shows how to process the files separately:

<?php

set_time_limit
(600);

require_once 
'class.ImageBatchProcessor.php';

// Transform c:/mypics/picture1.jpg to c:/album/picture_large.jpg.
// Maximal size: 800x600, rotate 90' clockwise, add a text label.
$t1 = new ImageBatchTransformation();
$t1->source 'c:/mypicts/picture1.jpg';
$t1->destination 'c:/album/picture_large.jpg';
$t1->maxWidth 800;
$t1->maxHeight 600;
$t1->format TI_JPEG;
$t->jpegQuality 85;
$t1->interlace true;
$t1->rotateAngle = -90;
$t1->rotateBgColor '#000000';
$t1->replaceExisted true;
$t3->label['text'] = '(c) 2008 www.example.com';
$t3->label['vertPos'] = TI_POS_BOTTOM;
$t3->label['horzPos'] = TI_POS_RIGHT;
$t3->label['font'] = 'c:/windows/fonts/Arial.ttf';
$t3->label['size'] = 10;
$t3->label['color'] = '#ffff00';
$t3->label['angle'] = 0;

// Transform c:/mypics/picture1.jpg to c:/album/picture_small.jpg.
// Maximal size: 100x100, rotate 90' clockwise.
$t2 = new ImageBatchTransformation();
$t2->source 'c:/mypicts/picture1.jpg';
$t2->destination 'c:/album/picture_small.jpg';
$t2->maxWidth 100;
$t2->maxHeight 100;
$t2->format TI_JPEG;
$t2->interlace false;
$t2->rotateAngle = -90;
$t2->rotateBgColor '#000000';
$t2->replaceExisted true;

// Transform c:/mypics/picture2.jpg to c:/album/picture2.jpg.
// Maximal size: 450x400, add a text label and a watermark.
$t3 = new ImageBatchTransformation();
$t3->source 'c:/mypics/picture2.jpg';
$t3->destination 'c:/album/picture2.jpg';
$t3->maxWidth 450;
$t3->maxHeight 400;
$t3->format TI_JPEG;
$t->jpegQuality 85;
$t3->interlace true;
$t3->replaceExisted true;
$t3->label['text'] = '(c) 2008 www.example.com';
$t3->label['vertPos'] = TI_POS_BOTTOM;
$t3->label['horzPos'] = TI_POS_RIGHT;
$t3->label['font'] = 'c:/windows/fonts/Arial.ttf';
$t3->label['size'] = 10;
$t3->label['color'] = '#ffff00';
$t3->label['angle'] = 0;
$t3->logo['file'] = 'c:/mypics/logos/logo.gif';
$t3->logo['vertPos'] = TI_POS_TOP;
$t3->logo['horzPos'] = TI_POS_LEFT;

$ibp = new ImageBatchProcessor();
$n $ibp->processEx(array($t1$t2$t3));

printf('%d images processed.'$n);

?>

2.3. Processing the Whole Directory with Some Exceptions

Sometimes there could be a situation when almost the whole directory should be processed the same way while only some of the files should be processed another way. For example, one of the files could be rotated:

<?php

set_time_limit
(600);

require_once 
'class.ImageBatchProcessor.php';

$ibp = new ImageBatchProcessor();

$t = new ImageBatchTransformation();
$t->source 'c:/mypicts/album1/';
$t->destination 'd:/album/';
$t->format TI_JPEG;
$t->jpegQuality = -1;
$t->interlace TI_INTERLACE_ON;
$t->maxWidth 150;
$t->maxHeight 150;
$t->fitToMax false;
$t->replaceExisted true;

// Process all JPEGs from the directory except mypict.jpg.
$n $ibp->process($t'/^(?!mypict.jpg$)(.*)(\.jpg)$/is');

$t->source 'c:/mypicts/album1/mypict.jpg';
$t->destination 'd:/album/mypict.jpg';
$t->rotateAngle = -90;

// Rotate and process mypict.jpg.
$n += $ibp->processEx(array($t));

printf('%d images processed.'$n);

?>

2.4. Batch Renaming of All Images in a Directory

The following example shows how to use the dirWalk() method to rename all JPEG images in a directory:

<?php

set_time_limit
(600);

// Define a callback function.
//
// The parameters are: directory path, current index, zero-padded index, full
// filename, filename without extension and extension started with a dot.
function batchRename($path$index$padded$fileName$baseName$extension)
{
    
$oldName $path $fileName;
    
$newName $path 'photo' $padded $extension;
    return 
rename($oldName$newName);
}

require_once 
'class.ImageBatchProcessor.php';
$ibp = new ImageBatchProcessor();

// Walk through a directory.
//
// First parameter is directory path, second is the name of callback function,
// third is optional filter of filenames (there are 2 predefined filters: the
// default IBP_ALL_REGEXP and IBP_IMAGE_REGEXP for images only).
//
// Returns the number of processed files.
$ibp->dirWalk('d:/album/''batchRename''/^(.*)(\.jpg|\.jpeg)$/is');

?>