Batch scale images with PHP/GD?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Using PHP/GD, does anyone have an example how to process jpg and png files
in the current directory to place smaller versions in a specified
sub-directory (e.g. thumbs/) while retaining original aspect ratio, so that
only the resized width or height need to be specified on the command line?


Re: Batch scale images with PHP/GD?

Tuxedo wrote:
Quoted text here. Click to load it

Here's a code fragment that takes a generic image in $content and
resizes it to be no bigger than 100x7opx using GD. It actually spits
them out as HTML compatible inages, but there are suitable tools in GD
for saving them

Be ware that on one server GD didn't work properly under multiple
simultaneous  access..I suspect the underlying library is NOT reentrant

// now to shrink the picture..
// get sizes
// our thumbnails are 100px wide..dont care about the height so scale as
// we DO care about the height now, so if the height is more than 50px,
scale that
$thumbnail=imagecreatetruecolor($newwidth,$newheight); // make empty new
header("Content-Type: image/jpeg");
imagejpeg( $thumbnail,null,75);


(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.

Re: Batch scale images with PHP/GD?

On 08/08/12 14:46, Tuxedo wrote:
Quoted text here. Click to load it

I prefer to use imagemagick, has more features and you can use seam
carving if you make some dimension changes when you scale down your image.

Good example on what you can do with imagemagick can be found at (Mikko's blog, the guy who maintains the imagemagick
extension for PHP).

A seam carving example:

If you want it to be saved to a file instead, then remove the

/* Display */
header( 'Content-Type: image/jpg' );
echo $im;

and replace it with

/* Save to file */



Site Timeline