note 42632 deleted from function.imagecopyresampled by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:42:41 +0000 |
| Subject: | note 42632 deleted from function.imagecopyresampled by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77940@lists.php.net to get a copy of this message | ||
Note Submitter: lha at hexoplastia dot com
----
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);
}