note 99913 added to function.imagecopyresampled
| From: | FluffyKitten at osu1 dot php dot net | Date: | Tue, 14 Sep 2010 18:00:39 +0000 |
| Subject: | note 99913 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172558@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 86.43.65.107
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/99913
Del: integrated -- https://master.php.net/note/delete/99913/integrated
Del: useless -- https://master.php.net/note/delete/99913/useless
Del: bad code -- https://master.php.net/note/delete/99913/bad+code
Del: spam -- https://master.php.net/note/delete/99913/spam
Del: non-english -- https://master.php.net/note/delete/99913/non-english
Del: in docs -- https://master.php.net/note/delete/99913/in+docs
Del: other reasons-- https://master.php.net/note/delete/99913
Reject -- https://master.php.net/note/reject/99913
Search -- https://master.php.net/manage/user-notes.php