NYCPHP Meetup

NYPHP.org

[nycphp-talk] Creatings Thumbnails with GD

Hans Zaunere lists at zaunere.com
Wed Nov 23 13:43:28 EST 2005



Russ Demarest wrote on Wednesday, November 23, 2005 1:32 PM:
> This works for me but I have limited needs.
> 
>          $width = imagesx($img);
>          $height = imagesy($img);
> 
>          if ( $width > $height ){
>                  $ratio = $tw / $width;
>          }else{
>                  $ratio = $th / $height;
>          }
>          $newWidth = $width * $ratio;
>          $newHeight = $height * $ratio;
> 
>          $rimg = imagecreatetruecolor($newWidth,$newHeight);
>          imagecopyresampled($rimg,$img,0,0,0,0,$newWidth,$newHeight,
> $width,$height);
> 
>          imagejpeg($rimg,$thumb);
> 
>          imagedestroy($img);
>          imagedestroy($rimg);

Wow, that's weird - that looks almost exactly like some code we just wrote
for a project.


function ConvertImageToThumbnail($inPath, $outPath) {
    // Convert the image at the path specified by $inPath
    // to a thumbnail image at the path specified by $outPath.

	// Get the width and height of the actual image.
    list($width, $height) = getimagesize($inPath);

	// How big should the thumbnail be?
    if( (119.0/$width) > (119.0/$height) )
        $scale = 119.0/$height;
    else
        $scale = 119.0/$width;

    $newwidth = $width * $scale;
    $newheight = $height * $scale;

	// Create a new thumbnail image of the correct size.
    $thumb = imagecreatetruecolor($newwidth, $newheight);

	// Load the original image.
    $source = imagecreatefromjpeg($inPath);

 
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$heigh
t);

	// Write out the thumbnail as a JPEG.
    imagejpeg($thumb, $outPath);
}  // ConvertImageToThumbnail


Have a good Thanksgiving everyone,



---
Hans Zaunere / President / New York PHP
   www.nyphp.org  /  www.nyphp.com





More information about the talk mailing list