Page 1 of 1

User uploaded image resizing issue with Mozilla

Posted: Tue Sep 07, 2004 2:04 pm
by voltrader
Script I use to resize user uploaded image works well in IE, but doesn't resize image in Mozilla. Any ideas why?

Code: Select all

if($picture!=null)
	{

		$type=@explode(".",$picture);

		$name=$type[0];

		$image=$this->path."images/".$picture;
		
		$size=getimagesize($image);
 
                //get photo file type
		$type=$size[2];

		$width=$size[0];
		//echo $width."<br>";
		
		$height=$size[1];
		//echo $height."==height<br>";
		
		$newwidth	= 300;
		$newheight	= 300;


		if($width<$newwidth and $height<$newheight)
			{
				return;
			}


		if($width>=$height)

		{

		$factor=$width/$newwidth;

		$newheight=$height/$factor;
		
		$newwidth=$width/$factor;
		
		}

		else
		{

		$factor=$height/$newheight;

		$newwidth=$width/$factor;
	
		$newheight=$height/$factor;

		}

	$im1=ImageCreateTrueColor($newwidth,$newheight); 

		if($type==2)
		{
		
			$name=$name.".jpg";
		
		$im = ImageCreateFromJpeg("$image");

		}

		elseif($type==3)
		{
		
			$name=$name.".png";
		
		$im = ImageCreateFromPng("$image");

		}

		elseif($type==1)
		{
	
			$name=$name.".gif";
		
		$im = ImageCreateFromGif("$image");
		}


		$newpath=$this->path."images/".$name;


	imagecopyresampled ($im1, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // reduce the image sizes


		if($type==3)
		{
			
			ImagePng($im1,$newpath); // output  image
		}
		elseif($type==2)
		{
			
			ImageJPEG($im1,$newpath,100); // output  image
		}
		elseif($type==1)
		{
			
			imagetruecolortopalette($im1, true, 256);
			
			ImageGIF($im1,$newpath); // output  image
		}


	chmod($newpath,0777);

			ImageDestroy($im1);


	
	}

}

Posted: Tue Sep 07, 2004 3:16 pm
by evilmonkey
I am really unsure as to what your problem is, but this bit of code seems to work fine accross browsers. This is actually a function, independant of a class which resizes and saves the image to a desired location. Give it a try, let me know if it's not compliant in your browser, that'd give me a reason to worry. When I tested, it was fine :? Also, if you're working with jpegs, keep in mind that GD that is bundndled with PHP does not support the creation of GIF files, and Mozilla, bieng a GPL product, may not particularily like gifs either. My code works only with jpegs, and assumes that the file bieng passed to it is also a jpeg (My upload system won't let you upload any other files, so there is no need for me to waste time and processing power running that check here).

Code: Select all

function savepic ($max_width, $max_height, $file, $filename){
	/*image resizing code
	*returns: jpeg image that is destroyed as soon as it's outputted
	*paramaeters that must be passed: $max_width, $max_hieght, $file
	*$max_width/$max_height are the maximum size of the picture in pixels
	*if not set, they default to 100 */
	
	if (@!$max_width)
	$max_width = 100;
	if (@!$max_height)
	$max_height = 100;
	
	$size = GetImageSize($file);
	$width = $size[0];
	$height = $size[1];
	
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	
	if ( ($width <= $max_width) && ($height <= $max_height) ) {
		$tn_width = $width;
		$tn_height = $height;
	}
	
	else if (($x_ratio * $height) < $max_height) {
		$tn_height = ceil($x_ratio * $height);
		$tn_width = $max_width;
	}
	else {
		$tn_width = ceil($y_ratio * $width);
		$tn_height = $max_height;
	}
	
	$src = ImageCreateFromJpeg($file);
	$dst = ImageCreateTrueColor($tn_width,$tn_height);
	ImageCopyResized($dst, $src, 0, 0, 0, 0,
	$tn_width,$tn_height,$width,$height);
	ImageJpeg ($dst, $filename, 75);
	ImageDestroy($src);
	ImageDestroy($dst);
	//end image resizing code	
}
?>
Good luck!

Posted: Tue Sep 07, 2004 3:50 pm
by voltrader
Thanks evilmonkey -- it may be something to do with bundling of gd with php in that case.

Phpinfo states my version of php as 4.3.8 and gd as "2.0.23 compatible".

Posted: Tue Sep 07, 2004 3:58 pm
by PrObLeM
*NOTE:

2.0.28 > GD > 1.5 does not support gif

http://www.boutell.com/gd/

Posted: Tue Sep 07, 2004 5:09 pm
by voltrader
Right-o -- What I can't understand is why IE renders gifs perfectly.

Posted: Tue Sep 07, 2004 8:52 pm
by evilmonkey
Mozilla might not especially like GIF's, with LZW bieng patented and all, mozilla may have been legally unable to full impliment support for it...

Posted: Tue Sep 07, 2004 11:15 pm
by Steveo31
Man, why can't there just be one browser that everyone uses that interprets code the same?


WHYYYYYYYY?!?!?!

Posted: Tue Sep 07, 2004 11:21 pm
by feyd
because competition is good.

Posted: Wed Sep 08, 2004 1:09 pm
by PrObLeM
feyd wrote:because competition is good.
It promotes Quality of products.

Posted: Wed Sep 08, 2004 1:13 pm
by feyd
PrObLeM wrote:
feyd wrote:because competition is good.
It promotes Quality of products.
exactly :)

Posted: Wed Sep 08, 2004 1:19 pm
by m3mn0n
PrObLeM wrote:
feyd wrote:because competition is good.
It promotes Quality of products.
But one of the side effects is the incompatibility of code for those products. :x

Posted: Sun Sep 12, 2004 12:36 pm
by voltrader
Figured it out... developer used MIME-type from $_FILES array to identify photo file type in code elsewhere. This was causing strange problems. I rewrote all routines to use getimagesize photo filetype identifier, and it now works perfectly in Mozilla.

Thanks again for the brainstorming help - much appreciated as always.