note 67832 added to function.imagecopyresampled
| From: | gviktor at gmail dot com | Date: | Wed, 05 Jul 2006 20:18:13 +0000 |
| Subject: | note 67832 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114412@lists.php.net to get a copy of this message | ||
For everybody who needs thumbs that are exactly 100x100 or so. The image is cropped to the right
proportions, and scaled - simultaneously. (This code doesn't work if you have the bug described
below in your installation.)
<?
/* resize and crop a gd image object to a given height and width */
function resize_and_crop($image, $width=200, $height=200) {
$width_orig = imagesx($image);
$height_orig = imagesy($image);
// Get crop area and offset
$scale_factor = max($width / $width_orig,
$height / $height_orig);
$width_crop = $width / $scale_factor;
$height_crop = $height / $scale_factor;
$x_offset = 0.5 * ($width_orig - $width_crop);
$y_offset = 0.5 * ($height_orig - $height_crop);
// Resample
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled(
// ==target== ==original==
$image_p, $image,
0, 0, $x_offset, $y_offset,
$width, $height, $width_crop, $height_crop);
return $image_p;
}
?>
----
Server IP: 129.16.214.50
Probable Submitter: 81.232.139.126
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- http://master.php.net/note/edit/67832
Del: integrated -- http://master.php.net/note/delete/67832/integrated
Del: useless -- http://master.php.net/note/delete/67832/useless
Del: bad code -- http://master.php.net/note/delete/67832/bad+code
Del: spam -- http://master.php.net/note/delete/67832/spam
Del: non-english -- http://master.php.net/note/delete/67832/non-english
Del: in docs -- http://master.php.net/note/delete/67832/in+docs
Del: other reasons-- http://master.php.net/note/delete/67832
Reject -- http://master.php.net/note/reject/67832
Search -- http://master.php.net/manage/user-notes.php