note 57990 added to function.imagecopyresampled
| From: | Nicolas dot Grasset at notup2u dot net | Date: | Thu, 20 Oct 2005 21:33:12 +0000 |
| Subject: | note 57990 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97326@lists.php.net to get a copy of this message | ||
If you are looking for a function that can resample your photos to a given size, no matter its
original size and/or ratio, here it is. And to make more fun (beacause life has to be fun), you can
even chose a border size for the picture!
$position can either be topleft, topright, bottomleft, bottomright or center.
<?php
function imagecopyresampledselection($filename, $desired_width, $desired_height, $bordersize,
$position)
{
// Get new dimensions
list($width, $height) = getimagesize($filename);
if($desired_width/$desired_height > $width/$height):
$new_width = $desired_width;
$new_height = $height * ($desired_width / $width);
else:
$new_width = $width * ($desired_height / $height);
$new_height = $desired_height;
endif;
// Resize
$image_p = imagecreatetruecolor($new_width, $new_height);
$image_f = imagecreatetruecolor($desired_width, $desired_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Adjust position
switch($position)
{
case("topleft"):
$x = $bordersize;
$y = $bordersize;
break;
case("topright"):
$x = $new_width - $desired_width + $bordersize;
$y = $bordersize;
break;
case("bottomleft"):
$x = $bordersize;
$y = $new_height - $desired_height + $bordersize;
break;
case("bottomright"):
$x = $new_width - $desired_width + $bordersize;
$y = $new_height - $desired_height + $bordersize;
break;
case("center"):
$x = ($new_width - $desired_width) / 2 + $bordersize;
$y = ($new_height - $desired_height) / 2 + $bordersize;
break;
}
// Resample with 1px border
imagecopyresampled($image_f, $image_p, $bordersize, $bordersize, $x, $y, $desired_width - 2 *
$bordersize,
$desired_height - 2 * $bordersize,
$desired_width - 2 * $bordersize,
$desired_height - 2 * $bordersize);
return $image_f;
}
$image_f = imagecopyresampledselection('test.jpg', 571, 319, 1, "center");
header('Content-type: image/jpeg');
imagejpeg($image_f, null, 100);
?>
To see a result, just browse http://www.notup2u.net/v8
Nicolas Grasset
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57990
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57990&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57990&report=yes
Search -- http://master.php.net/manage/user-notes.php