note 102746 deleted from function.imagecopyresampled by danbrown
| From: | danbrown@php.net | Date: | Thu, 03 Mar 2011 13:58:44 +0000 |
| Subject: | note 102746 deleted from function.imagecopyresampled by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177531@lists.php.net to get a copy of this message | ||
Note Submitter: vanadragos at yahoo dot com
----
the first function to resize the image on the page is faulty
this is a better function that works and also has a second pass because some images did not get
trough properly in the first pass
function resizeImage($src, $w, $h) {
// Get the current size
$width = ImageSx($src);
$height = ImageSy($src);
// If one dimension is right then nothing to do
// if($width <= $w || $height <= $h)
// return($src);
// Calculate new size
if(($w - $width) > ($h - $height)) { // use height
$s = $h / $height;
$nw = round($width * $s);
$nh = round($height * $s);
}
else { // Use width
$s = $w / $width;
$nw = round($width * $s);
$nh = round($height * $s);
}
// Return the new image
$nwidth = $nw;
$nheight = $nh;
// If one dimension is right then nothing to do
// if($width <= $w || $height <= $h)
// return($src);
// Calculate new size
if(($w - $nwidth) > ($h - $nheight)) { // use height
$s = $h / $nheight;
$nw = round($nwidth * $s);
$nh = round($nheight * $s);
}
else { // Use width
$s = $w / $nwidth;
$nw = round($nwidth * $s);
$nh = round($nheight * $s);
}
$im = ImageCreateTrueColor($nw, $nh);
ImageCopyResampled($im, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);
return($im);
}