note 42632 added to function.imagecopyresampled
| From: | lha at hexoplastia dot com | Date: | Mon, 24 May 2004 16:48:09 +0000 |
| Subject: | note 42632 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70033@lists.php.net to get a copy of this message | ||
A function to resize any given image to any other specified size, even if it differs in aspect
ratio. The created image doesn't look distorted because the algorithm crops the image if
necessary to fit in the new size, trying to keep the maximum portion of the original image after
cropping.
function intelligentshrink($sourcefile, $destfile, $fw, $fh, $jpegquality = 60)
{
list($ow, $oh, $from_type) = getimagesize($sourcefile);
switch($from_type)
{
case 1: // GIF
$srcImage = imageCreateFromGif($sourcefile);
break;
case 2: // JPG
$srcImage = imageCreateFromJpeg($sourcefile);
break;
case 3: // PNG
$srcImage = imageCreateFromPng($sourcefile);
break;
}
$tempw = $fw;
$temph = number_format((($oh*$fw)/$ow), 0);
if($temph < $fh)
{
$tempw = number_format((($ow*$fh)/$oh), 0);
$temph = $fh;
}
$tempImage = imageCreateTrueColor($tempw, $temph);
imageAntiAlias($tempImage, true);
imagecopyresampled($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh);
// Calculate offsets
if($temph > $fh)
{
$offsety = number_format(($temph/2)-($fh/2), 0);
$offsetx = 0;
}
else
{
$offsety = 0;
$offsetx = number_format(($tempw/2)-($fw/2), 0);
}
$destImage = imageCreateTrueColor($fw, $fh);
imagecopyresampled($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh);
imageJpeg($destImage, $destfile, $jpegquality);
}
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42632
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42632&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42632&report=yes
Search -- http://master.php.net/manage/user-notes.php