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?
 
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
 
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?
 
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?
 
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:
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?
 
Hey Marc,

I altered it to this:

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

	//Sets font size and font file variables
	$fontsize = 20; 
	$font = './3.ttf'; //place font in same folder as this script 

	//loop through array
	foreach($images as $image) { 
		//basename create bare filename without folder/extension
		$orig_text = 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 + 60;
		$new_height = $height + 95;
		
		//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);
		
		//position the existing image into the new image
		imagecopyresampled($new_im, $im, 30, 30, 0, 0, $width, $height, $width, $height);
		
		//php function to caluclate width of text
		$text_width = imagettfbbox($fontsize, 0, $font, $orig_text);
		
		//definte our text colour -black in this case
		$tc = imagecolorallocate($im, 0, 0, 0);
		
		//If the width of the text is greater than the width of the image, then
		if($text_width[2] > $new_width)
		{
			//explode text at first hyphen
			$lines = split('-', $origtext);
			//Filename with new line added
			$final_text = implode("\n", $lines);
		} else {
			//else, Use the original text
			$final_text = $orig_text;
		}
		
		//write the text using
		imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height -20, $tc, $font, $filename);
		
		//writes the completed image to the new folder
		imagejpeg($new_im, 'new/' . $orig_text . '.jpg');
		
		//free up resources
		imagedestroy($im); 
		imagedestroy($new_im); 
	} 
?>

I tried it but it doesn't put any text on the image whatsoever. Any ideas?
 
Last edited:
Hmm,

Now the text has come back, but on the images where it meets my 'if' condition, there is no text at all. Can you notice any reason why?

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

	//Sets font size and font file variables
	$fontsize = 20; 
	$font = './3.ttf'; //place font in same folder as this script 

	//loop through array
	foreach($images as $image) { 
		//basename create bare filename without folder/extension
		$orig_text = 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 + 60;
		$new_height = $height + 95;
		
		//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);
		
		//position the existing image into the new image
		imagecopyresampled($new_im, $im, 30, 30, 0, 0, $width, $height, $width, $height);
		
		//php function to caluclate width of text
		$text_width = imagettfbbox($fontsize, 0, $font, $orig_text);
		
		//definte our text colour -black in this case
		$tc = imagecolorallocate($im, 0, 0, 0);
		
		//If the width of the text is greater than the width of the image, then
		if($text_width[2] > $new_width)
		{
			//split text at first hyphen
			$lines = split('-', $origtext);
			//Filename with new line added
			$final_text = implode("\n", $lines);
		} else {
			//else, Use the original text
			$final_text = $orig_text;
		}
		
		//write the text using
		imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height -20, $tc, $font, $final_text);
		
		//writes the completed image to the new folder
		imagejpeg($new_im, 'new/' . $orig_text . '.jpg');
		
		//free up resources
		imagedestroy($im); 
		imagedestroy($new_im); 
	} 
?>
 
Last edited:
Right, Thanks,

Got it working on two lines now, just got to make alterations to:

- Center both lines

- Center a single line vertically but center a double lined image appropriatley.


When I have finished making changes, I'll post it here
 
With many thanks to Marc for all his help in getting this to work. The final code I have used is shown below:

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

	//Sets font size and font file variables
	$fontsize = 20; 
	$font = './3.ttf'; //place font in same folder as this script 

	//loop through array
	foreach($images as $image) { 
		//basename create bare filename without folder/extension
		$orig_text = 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 + 60;
		$new_height = $height + 95;
		
		//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);
		
		//position the existing image into the new image
		imagecopyresampled($new_im, $im, 30, 30, 0, 0, $width, $height, $width, $height);
		
		//php function to caluclate width of text
		$text_width = imagettfbbox($fontsize, 0, $font, $orig_text);
		
		//define our text colour -black in this case
		$tc = imagecolorallocate($im, 0, 0, 0);
		
		//If the width of the text is greater than the width of the image, then
		if($text_width[2] > $new_width)
		{
			//explode text at first hyphen
			$lines = split(' - ', $orig_text);
			
			//php function to re-caluclate width of text due to multiple lines
			$line_1_width = imagettfbbox($fontsize, 0, $font, $lines[0]);
			$line_2_width = imagettfbbox($fontsize, 0, $font, $lines[1]);
			
			//write the text using
			imagettftext($new_im, $fontsize, 0, ($new_width - $line_1_width[2]) / 2, $new_height - 40, $tc, $font, $lines[0]);
			imagettftext($new_im, $fontsize, 0, ($new_width - $line_2_width[2]) / 2, $new_height - 10, $tc, $font, $lines[1]);
			
		} else {
			//else, Use the original text
			$final_text = $orig_text;
			
			//write the text using
			imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height - 26, $tc, $font, $final_text);
		}
		
		
		//writes the completed image to the new folder
		imagejpeg($new_im, 'new/' . $orig_text . '.jpg');
		
		//free up resources
		imagedestroy($im); 
		imagedestroy($new_im);
		
		echo "FINISHED!";
	} 
?>
 
Back
Top Bottom