PHPImageResize
Revision as of 13:44, 25 February 2009 by Michael Murtaugh (talk | contribs) (New page: <source lang="php"> // scale src image to fit given w / h $sw = imagesx($src); $sh = imagesy($src); // try "fit width" ($nw = $tw) $center = true; $sdx = 0; $sdy = 0; $nw = ...)
// scale src image to fit given w / h
$sw = imagesx($src);
$sh = imagesy($src);
// try "fit width" ($nw = $tw)
$center = true;
$sdx = 0; $sdy = 0;
$nw = $w;
$nh = round($sh * ($nw / $sw));
if ($nh < $h) {
// if height is to small "fit height"
$nh = $h;
$nw = round($sw * ($nh / $sh));
if ($center) { $sdx = round(($nw - $w) / 2); }
} else {
if ($center) { $sdy = round(($nh - $h) / 2); }
}
// scale image
$scaledimage = imagecreatetruecolor($nw, $nh);
imagecopyresampled($scaledimage, $src, 0, 0, 0, 0, $nw, $nh, $sw, $sh);