Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch picture resizing?

Status
Not open for further replies.

fourtyseven

Programmer
Apr 4, 2005
10
CA
I need a way of resizing a directory full of pictures. what I do is ftp new pictures to the images folder. then on the admin section of the site I run a php script that runs a dos batch script which updates a text file containing all the filenames of .jpeg files contained in that folder. The images section of the site uses the text file to load links to the pictures.

however, what I need to do now is somehow write a script that will run at the same time as my php/dos batch update script, and will resize all of the files in the folder to be 409px * 293px. The photos are high quality and take forever to load if I don't resize them to be smaller. is there anyway I can use PHP to do this? or will I need to download some sort of application that will process scripts to do this? I have a windows GUI application my pc (iRedSoft Image Resizer) that will do this, but I don't want to have to remote desktop home to my pc after uploading new pictures, just so I can run the application and resize the pictures, I want it to be an automated script that I can run from the admin section of the site.

thanks in advance for any suggestions.
 
The php GD image manipulation library can be very handy for things like this. It has to be enabled in your php.ini before using it.

Code:
$fileName = "someImg.jpg"; 
$fp = fopen($fileName,"rb");
$fileData = fread($fp);
$bigPicture = imagecreatefromstring($fileData);
$oldH = imagesy($bigPicture);
$oldW = imagesx($bigPicture);
$resizedPicture = imagecreatetruecolor(409,293);
imagecopyresized($resizedPicture,$bigPicture,0,0,0,0,409,239,$oldW,$oldH);
imagedestroy($bigPicture);
imagejpeg($resizedPicture,"newFileName.jpg");
imagedestroy($resizedPicture);


you should be able to stick that in a loop that scans the directory and be able to change the "someImg.jpg" and "newFileName.jpg" filenames to that which is your hearts desire.

Lemme know if it doesn't work....

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
thanks for the suggestion!

I keep getting the following errors, even though the process is working (it is created a resized image in the specified directory, but isn't deleting the originals):

Warning: imagecreatefromstring(): Data is not in a recognized format. in C:\Program Files\Apache Group\Apache2\htdocs\admin\updatepics\resizepix.php on line 14

Warning: imagesy(): supplied argument is not a valid Image resource in C:\Program Files\Apache Group\Apache2\htdocs\admin\updatepics\resizepix.php on line 16

Warning: imagesx(): supplied argument is not a valid Image resource in C:\Program Files\Apache Group\Apache2\htdocs\admin\updatepics\resizepix.php on line 17

Warning: imagecopyresized(): supplied argument is not a valid Image resource in C:\Program Files\Apache Group\Apache2\htdocs\admin\updatepics\resizepix.php on line 19

Code:
$fileName = "[URL unfurl="true"]http://www.mysite.com/images/test/test.txt";[/URL]
$fp = fopen($fileName,"r");


do {
	$image = fgets($fp);
	$bigPicture = imagecreatefromstring($image);
	echo $bigPicture;
	$oldH = imagesy($bigPicture);
	$oldW = imagesx($bigPicture);
	$resizedPicture = imagecreatetruecolor(409,293);
	imagecopyresized($resizedPicture,$bigPicture,0,0,0,0,409,239,$oldW,$oldH);
	imagedestroy($bigPicture);
	imagejpeg($resizedPicture,$image);
	imagedestroy($resizedPicture);

} while (!feof($fp));

I posted my code as well...any ideas? Its probably something simple that I'm missing...
 
okay....thats my bad...sorry.

The imagecreatefromstring method takes a string that has binary file data in it. swap that out for the function imagecreatefromjpeg.

If you want to echo the filename then change this [tt]echo $bigPicture[/tt] to [tt]echo $image[/tt]



Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
I assume you got it working then....

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top