note 52542 added to function.imagecopyresampled
| From: | del at kartoon dot net | Date: | Thu, 05 May 2005 08:38:27 +0000 |
| Subject: | note 52542 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88956@lists.php.net to get a copy of this message | ||
This snippet allows you to grab a thumbnail from the center of a large image. This was used for a
client photo gallery for art to give a teaser of the image to come (only a small portion). You
could get dynamic with this value. I also put in a scaling factor in case you want to scale down
first before chopping.
<?php
// The file
$filename = 'moon.jpg';
$percent = 1.0; // if you want to scale down first
$imagethumbsize = 200; // thumbnail size (area cropped in middle of image)
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize); // true color for best quality
$image = imagecreatefromjpeg($filename);
// basically take this line and put in your versin the -($new_width/2) + ($imagethumbsize/2) &
-($new_height/2) + ($imagethumbsize/2) for
// the 2/3 position in the 3 and 4 place for imagecopyresampled
// -($new_width/2) + ($imagethumbsize/2)
// AND
// -($new_height/2) + ($imagethumbsize/2)
// are the trick
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) +
($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52542
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52542&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52542&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52542&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52542&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52542&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52542&report=yes
Search -- http://master.php.net/manage/user-notes.php