note 94251 added to function.imagecopyresampled
| From: | seifer at loveletslive dot com | Date: | Sun, 25 Oct 2009 13:27:12 +0000 |
| Subject: | note 94251 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162083@lists.php.net to get a copy of this message | ||
Okay so I saw that others have posted this already but I just messed around and made it so figured I
would share it.
Mine works, not to say others don't, I haven't tested them.
This function creates a thumbnail of the specfied size.
If the thumbnail is a different ration it will automatically crop the center of the source image.
It works if the source image is bigger or smaller than the desired "cropped thumbnail."
Here is the code...
<?php
function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE -
Returns an image resource.
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
$myImage = imagecreatefromjpeg($imgSrc);
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig,
$height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)),
($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width,
$thumbnail_height);
imagedestroy($process);
imagedestroy($myImage);
return $thumb;
}
//Create the thumbnail
$newThumb = CroppedThumbnail("MyImageName.jpg",75,100);
// And display the image...
header('Content-type: image/jpeg');
imagejpeg($newThumb);
?>
----
Server IP: 117.55.229.1
Probable Submitter: 118.208.68.211
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/94251
Del: integrated -- https://master.php.net/note/delete/94251/integrated
Del: useless -- https://master.php.net/note/delete/94251/useless
Del: bad code -- https://master.php.net/note/delete/94251/bad+code
Del: spam -- https://master.php.net/note/delete/94251/spam
Del: non-english -- https://master.php.net/note/delete/94251/non-english
Del: in docs -- https://master.php.net/note/delete/94251/in+docs
Del: other reasons-- https://master.php.net/note/delete/94251
Reject -- https://master.php.net/note/reject/94251
Search -- https://master.php.net/manage/user-notes.php