Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing gifs gives a black box

Status
Not open for further replies.
Aug 23, 2004
174
US
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

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;
 
in debugging it is a good idea to remove the error suppressors.

i notice that you have applied a quality level to imagegif even though the function does not allow for one. i would suggest removing that and the prepended @ sign and reporting back. hopefully you will get some meaningful error language if the fix has not solved the problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top