Batch scale images with PHP/GD? [message #178809] |
Wed, 08 August 2012 12:46 |
Tuxedo
Messages: 1 Registered: August 2012
Karma: 0
|
Junior Member |
|
|
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
|
|
|
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: 0
|
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.
|
|
|
Re: Batch scale images with PHP/GD? [message #178817 is a reply to message #178809] |
Wed, 08 August 2012 16:18 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 08/08/12 14:46, 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?
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
valokuva.org (Mikko's blog, the guy who maintains the imagemagick
extension for PHP).
A seam carving example: http://valokuva.org/?m=200802
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 */
$im->writeImage('test.png');
$im->clear();
$im->destroy();
--
//Aho
|
|
|