Forum Moderators: coopster
<?php
$url = "path/to/large/image.png";
$image = imagecreatefrompng($url);
$result = imagecreate(148,111);
$fg = imagecolorallocate($result,255,255,255);
imagefilledrectangle($result,0,0,148,111,$fg);
$w = imagesx($image);
$h = imagesy($image);
$wscale = 148 / $w;
$hscale = 111 / $h;
if ($wscale < $hscale) {
$xresult = 148;
$yresult = $h * $wscale;
$xoffset = 0;
$yoffset = (111 - $yresult)/2;
} else {
$yresult = 111;
$xresult = $w * $hscale;
$xoffset = (148 - $xresult) / 2;
$yoffset = 0;
}
ImageCopyresized($result,$image,$xoffset,$yoffset,0,0,$xresult,$yresult,$w,$h);
imagepng($result, "thumb.png");
imagedestroy($result);
?>
$w = imagesx($image);
$h = imagesy($image);
$wscale = 148 / $w;
$hscale = 111 / $h;
/**
* Resizes images proportionally for display format
*
* @param string $image path to image
* @param integer $maxImgWidth maximum image width for display
* @return array
*/
function resizeProportionally($image, $maxImgWidth = 100)
{
$w = $maxImgWidth;
$h = $maxImgWidth;
if ($list = @getimagesize($image)) {
list($w, $h, $t, $a) = $list;
if ($w > $maxImgWidth) {
$h = $h*($maxImgWidth/$w);
$w = $maxImgWidth;
}
}
return array($w, $h);
}
list($iw, $ih) = resizeProportionally($image, 111);
if ($h > $maxImgWidth) {
$w = floor($w*($maxImgWidth/$h));
$h = $maxImgWidth;
}