JoeAtRevolutionUnltd
Programmer
Here is my code. It works fine with jpgs, but gifs give me a black box. Any help is appreciated.
Here is how it is called.
resize_Image($name,$directory,100,125,'thumbs/')
php5
Here is how it is called.
resize_Image($name,$directory,100,125,'thumbs/')
php5
Code:
function resize_Image($cur_file,$dir,$w,$h,$thumbs)
{
$cur_dir = $dir;
$filename = $cur_dir.$cur_file;
$output_dir = $cur_dir.$thumbs;
if(preg_match("/.jpg/i", "$filename"))
{
$format = 'image/jpeg';
}
if (preg_match("/.gif/i", "$filename"))
{
$format = 'image/gif';
}
if(preg_match("/.png/i", "$filename"))
{
$format = 'image/png';
}
if($format!='')
{
switch($format)
{
case 'image/jpeg':
$source = imagecreatefromjpeg($filename);
break;
case 'image/gif';
$source = imagecreatefromgif($filename);
break;
case 'image/png':
$source = imagecreatefrompng($filename);
break;
}
list($width, $height) = getimagesize($filename);
$thumb = imagecreatetruecolor($w,$h);
imagealphablending($thumb, false);
$wm = $width/$w;
$hm = $height/$h;
$h_height = $h/2;
$w_height = $w/2;
if($width > $height) {
$adjusted_width = round($width / $hm);
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
$adjusted_width = $adjusted_width < $w ? $w : $adjusted_width;
imagecopyresampled($thumb,$source,0,0,0,0,$adjusted_width,$h,$width,$height);
}
elseif(($width < $height)) {
$adjusted_height = round($height / $wm);
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
$adjusted_height = $adjusted_height < $h ? $h : $adjusted_height;
imagecopyresampled($thumb,$source,0,-$int_height,0,0,$w,$adjusted_height,$width,$height);
}
elseif($width == $height) {
imagecopyresampled($thumb,$source,0,0,0,0,$w,$h,$width,$height);
}
$out_filename = $output_dir.$cur_file;
switch($format)
{
case 'image/jpeg':
@imagejpeg($thumb, $out_filename, 100);
break;
case 'image/gif';
@imagegif($thumb, $out_filename, 100);
break;
case 'image/png':
@imagepng($thumb, $out_filename, 100);
break;
}
return true;
}
else
return false;