[nycphp-talk] GD and image resizing

tedd tedd at
Wed Jan 31 12:24:36 EST 2007

At 9:37 AM -0500 1/31/07, Aaron Fischer wrote:
>I'm working with GD for the first time and need to receive uploaded 
>images (that part is done), determine if they are vertical or 
>horizontal orientation and then resize them appropriately given a 
>max length or width.
>I'm listening to Jeff's GD presentation now and searching the 
>archives.   I remember one or two scripts that were posted a while 
>back that do roughly what I am describing.
>I'm under severe time constraints to have a project completed so if 
>someone has some GD resize stuff to help me get going I would be 
>most appreciative.  Tips/pointers are also helpful.


If you already know the picture name and type, this will work.

// The file
$filename = 'test.jpg';
$outfilename = "/path/picture.jpg";

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig))
    $width = ($height / $height_orig) * $width_orig;
    $height = ($width / $width_orig) * $height_orig;

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);

// Output
imagejpeg($image_p, $outfilename, 100);


More information about the talk mailing list