Batch Image Processing

PHP:
//php function to caluclate width of text
$text_width = imagettfbbox($fontsize, 0, $font, $filename);

imagettfbbox actually returns an array. so to get the actual width of the text you have to you use

PHP:
$text_width[2]
 
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:
PHP:
 imagettftext($new_im, $fontsize, 0, ($new_width - $text_width[2]) / 2, $new_height -20, $tc, $font, $filename);

$filename should be $final_text
 
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