Here's my de facto resizing function. It covers most needs. Resizes most image types and maintains transparency and alpha if the image has them.
PHP Code:
function resize($source, $destination = null, $w = 125, $h = 125, $quality = 100)
{
$details = @getimagesize($source) or die("I cannot open $source");
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$source = imagecreatefrom'.$type.'($source);');
if($details[0] < $details[1])
{
$w = round(($h / $details[1]) * $details[0]);
}
else
{
$h = round(($w / $details[0]) * $details[1]);
}
if(imageistruecolor($source))
{
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);
}
else
{
$slate = @imagecreate($w, $h) or die('Invalid thumbnail dimmensions');
if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source))))
{
$trans = ImageColorAllocate($slate, $trans['red'], $trans['green'], $trans['blue']);
imagefilledrectangle($slate, 0, 0, $w - 1, $h - 1, $trans);
imagecolortransparent($slate, $trans);
}
}
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $details[0], $details[1]);
$destination or header('Content-Type: '.$details['mime']);
eval('@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination ':'')).');');
imagedestroy($source);
imagedestroy($slate);
$destination or die;
}
If you have trouble integrating this you will need to post your code but before you do, check out the code in this thread.
Bookmarks