Despite reading the manual, the FAQ's on thumbnailing and using imagecreatetruecolor for jpgs and pngs, a number of my thumbnails still look washed out or very blue.
After two months of searching, I give up. Can someone point out the simple error of my (coding) ways? This is function is originally based on the user comments in the php manual.
function resample_image($forcedwidth, $forcedheight,
$sourcefile, $destfile, $imgqual)
{
//return;
$g_imgcomp = $imgqual;
$g_srcfile = $sourcefile;
$g_dstfile = $destfile;
$g_fw = $forcedwidth;
$g_fh = $forcedheight;
if(!file_exists($g_srcfile)) return;
$image_type = image_type($sourcefile);
$g_is=getimagesize($g_srcfile);
if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh)) {
$g_iw=$g_fw;
$g_ih=($g_fw/$g_is[0])*$g_is[1];
} else {
$g_ih=$g_fh;
$g_iw=($g_ih/$g_is[1])*$g_is[0];
}
// create a copy of the image in memory
// creates thumbnail
// resizes
// saves or outputs image
if ($image_type == "jpeg") {
$img_src=imagecreatefromjpeg($g_srcfile);
$img_dst=imagecreatetruecolor($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0,
$g_iw, $g_ih, $g_is[0], $g_is[1]);
imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
} elseif ($image_type == "gif") {
$img_src=imagecreatefromgif($g_srcfile);
$img_dst=imagecreate($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0,
$g_iw, $g_ih, $g_is[0], $g_is[1]);
imagegif($img_dst, $g_dstfile);
} elseif ($image_type == "png") {
$img_src=imagecreatefrompng($g_srcfile);
$img_dst=imagecreatetruecolor($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0,
$g_iw, $g_ih, $g_is[0], $g_is[1]);
imagepng($img_dst, $g_dstfile);
} else {
return;
}
imagedestroy($img_src); // frees memory
imagedestroy($img_dst); // frees memory
return $g_dstfile;
}
NOTES: 1) I know I should use "case". 2) What is returned doesn't matter. I check whether the thumb exists again before displaying it. 3) image_type is a function utilizing getimagesize
I'm sure you already have an idea, but just in case, this is sample result (actual sizes) from:
to
