Re: Batch scale images with PHP/GD? [message #178811 is a reply to message #178809] |
Wed, 08 August 2012 13:08   |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
Tuxedo wrote:
> 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?
>
> Tuxedo
>
>
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..
$im=imagecreatefromstring($content);
// get sizes
$width=imagesx($im);
$height=imagesy($im);
// our thumbnails are 100px wide..dont care about the height so scale as
width
$newheight=round(($height*100)/$width);
$newwidth=100;
// we DO care about the height now, so if the height is more than 50px,
scale that
if($newheight>50)
{
$newwidth=round(100*50/$newheight);
$newheight=50;
}
$thumbnail=imagecreatetruecolor($newwidth,$newheight); // make empty new
wotsit.
imagecopyresampled($thumbnail,
$im,0,0,0,0,$newwidth,$newheight,$width,$height);
header("Content-Type: image/jpeg");
imagejpeg( $thumbnail,null,75);
--
Ineptocracy
(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.
|
|
|