note 99913 deleted from function.imagecopyresampled by danbrown

From: Date: Tue, 14 Sep 2010 18:18:27 +0000
Subject: note 99913 deleted from function.imagecopyresampled by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172559@lists.php.net to get a copy of this message
Note Submitter: FluffyKitten ---- A slight modification to the CroppedThumbnail function from seifer (function creates a thumbnail of the specified size, if aspect ratios differ it crops to the centre of the source image) I've amended it to calculate correct parameters for imagecopyresampled, instead of calling imagecreatetruecolor & imagecopyresampled twice <?php /* params: $imgSrc: image file to be copied (include dir path if necessary) $imgDest: name of file to save the thumbnail as (include dir if necessary) $width_thumb,$height_thumb: width & height to create thumb */ function CroppedThumbnail($imgSrc,$imgDest,$width_thumb,$height_thumb) { $myImage = imagecreatefromjpeg($imgSrc); list($width_orig, $height_orig) = getimagesize ($imgSrc); //Calculate scaling $ratio_orig = $width_orig/$height_orig; $ratio_thumb = $width_thumb/$height_thumb; if ($ratio_thumb < $ratio_orig) { $copy_width = $height_orig*$ratio_thumb; $copy_height = $height_orig; } else { $copy_width = $width_orig; $copy_height = $width_orig/$ratio_thumb; } $src_x = ($width_orig - $copy_width)/2; $src_y = ($height_orig - $copy_height)/2; $thumb = imagecreatetruecolor($width_thumb, $height_thumb); imagecopyresampled($thumb, $myImage, 0, 0, $src_x, $src_y, $width_thumb, $height_thumb, $copy_width, $copy_height); imagejpeg($thumb,$imgDest); //save thumb as a file imagedestroy($myImage); } //sample usage CroppedThumbnail ("images/mypic.jpg","images/thumbs/mypic_sm.jpg",100,100); ?>

« previous php.notes (#172559) next »