Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6737 > unrolled thread

Batch scale images with PHP/GD?

Started byTuxedo <tuxedo@mailinator.com>
First post2012-08-08 14:46 +0200
Last post2012-08-08 18:18 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Batch scale images with PHP/GD? Tuxedo <tuxedo@mailinator.com> - 2012-08-08 14:46 +0200
    Re: Batch scale images with PHP/GD? The Natural Philosopher <tnp@invalid.invalid> - 2012-08-08 14:08 +0100
    Re: Batch scale images with PHP/GD? "J.O. Aho" <user@example.net> - 2012-08-08 18:18 +0200

#6737 — Batch scale images with PHP/GD?

FromTuxedo <tuxedo@mailinator.com>
Date2012-08-08 14:46 +0200
SubjectBatch scale images with PHP/GD?
Message-ID<jvtn3a$58j$1@news.albasani.net>
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

[toc] | [next] | [standalone]


#6739

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-08-08 14:08 +0100
Message-ID<jvtoc5$7l4$1@news.albasani.net>
In reply to#6737
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.

[toc] | [prev] | [next] | [standalone]


#6745

From"J.O. Aho" <user@example.net>
Date2012-08-08 18:18 +0200
Message-ID<a8fhq8F6gcU1@mid.individual.net>
In reply to#6737
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web