Batch Image Processing

Associate
Joined
31 Jan 2007
Posts
1,860
Hey

I can batch process images to resize, crop, rename and whatever but I ahve 150+ images in 1 folder but I need a program that can add a 10 pixel border to the left, top and right edges and add a 25 pixel border to the bottom edge and then add some text into the bottom border based on either the filename of the image or a .txt file.

Anyone know of any software which could do this in batch?
 
Sorry misread your full requirements, my bad :(

Photoshop can do batch work, but it's something I've never delved into other than the resize, crop etc - which I find Irfanview simpler at.

Sorry.
 
Yeh, I use irfanview for simple, resize, rename and crop batches but I need to find an app which can do what I stated in my OP
 
learn php? that could do it easily. :D

edit: if you can host all the files in a single zip file i can download, i'll write a script and process them for you. i'm only prepared to work with filenames though. i'm not messing about processing text files as it will take too much of my time. :p
 
Last edited:
If you could write a script and send it over then I'll could look at it and learn how it works and run it myself.

That would be excellent. - Working from the file name would be great. Is there anyway that the font of the text could be changed?
 
here we go... create 2 folders.

old (this should contain the original images)
new (php needs to be able to write to this folder)

now place this script in the same folder as the above 2 folders...

PHP:
<?php
$images = glob('old/*.jpg');

$fontsize = 10;
$font = './tahoma.ttf'; //place font in same folder as this script

foreach($images as $image) {
	$filename = basename($image, '.jpg');
	$im = imagecreatefromjpeg($image);
	$width = imagesx($im);
	$height = imagesy($im);
	$new_width = $width + 20;
	$new_height = $height + 35;
	$new_im = imagecreatetruecolor($new_width,$new_height);
	$bg = imagecolorallocate($im, 255, 255, 255);
	imagefill($new_im, 0 ,0, $bg);
	imagecopyresampled($new_im, $im, 10, 10, 0, 0, $width, $height, $width, $height);
	$text_width = imagettfbbox($fontsize, 0, $font, $filename);
	$tc = imagecolorallocate($im, 0, 0, 0);
	imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height - 8, $tc, $font, $filename);
	imagejpeg($new_im, 'new/' . $filename . '.jpg');
	imagedestroy($im);
	imagedestroy($new_im);
}
?>

might not be a good idea to do this on shared hosting as it will hog the cpu. or just do a few images at a time.

edit: corrected a few typos...
 
Last edited:
Hey, Great, Will go and try this now. Is there any chance that you could add some line comments in to just explain what is happening and how it is doing it?
 
there were a few typos above that i've just corrected so make sure you copy the updated script. i'll add some comments i suppose. :p

also, you won't get any confirmation of what it's doing. you should see an hourglass for a bit and then a blank white page. just check the "new" folder to see if it worked. :p
 
Last edited:
It works but I get this error:

Warning: imagettftext() [function.imagettftext]: Invalid font filename in D:\wamp\www\imageconverts\script.php on line 20

Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename in D:\wamp\www\imageconverts\script.php on line 18
 
Right, That was my fault, forgot to put the font name into the script. Sorry

Two issues:

How can I move the font up slightly and can I make the font bold?

Can the execution time be increased as it keeps timing out
 
Last edited:
try set_time_limit(0); as the first line of code.

positioning:
PHP:
imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height - 8, $tc, $font, $filename);

"$new_height - 8" is the y-cordinate so try 10 maybe.

as for fonts....use a bold font? or look at the php manual page for imagettftext. some workarounds there.

comments:

PHP:
<?php
//scan folder for jpg files and put them in an array
$images = glob('old/*.jpg');

//should be obvious
$fontsize = 10;
$font = './tahoma.ttf'; //place font in same folder as this script

//loop through array
foreach($images as $image) {
	//basname create bare filename without folder/extension
    $filename = basename($image, '.jpg');
	//read current image into php resource $im that can be processed
    $im = imagecreatefromjpeg($image);
	//get width/height of image
    $width = imagesx($im);
    $height = imagesy($im);
	//calculate width/height of new image adding on extra for borders
    $new_width = $width + 20;
    $new_height = $height + 35;
	//create new image using the dimensions we've just worked out
    $new_im = imagecreatetruecolor($new_width,$new_height);
	//before you can use a colour, you have to define it first. uses RGB so 255, 255, 255  is white
    $bg = imagecolorallocate($im, 255, 255, 255);
	//fill our new image with this new colour
    imagefill($new_im, 0 ,0, $bg);
	//copy the original source into the new image.
    imagecopyresampled($new_im, $im, 10, 10, 0, 0, $width, $height, $width, $height);
	//php function to caluclate width of text
    $text_width = imagettfbbox($fontsize, 0, $font, $filename);
	//definte our text colour -black in this case
    $tc = imagecolorallocate($im, 0, 0, 0);
	//write the text using
    imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height - 8, $tc, $font, $filename);
	//writes the completed image to the new folder
    imagejpeg($new_im, 'new/' . $filename . '.jpg');
	//free up resources
    imagedestroy($im);
    imagedestroy($new_im);
}
?>
 
Last edited:
Thats fantastic. I really appreciate you help with doing that. I know quite a bit of PHP but havn't done any image manipulation using it yet.

Right, I don;t want to keep asking you questions on how to alter it to do different things so you can just tell me to go and ask somewhere else if you wish but my next questions are:

Some of the filenames are too long to fit at the bottom of certain images where the height is longer than the width. Can the text be altered to run onto 2 lines if this occurs?
 
Back
Top Bottom