User uploaded image resizing issue with Mozilla

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

User uploaded image resizing issue with Mozilla

Post 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);


	
	}

}
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post 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".
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

*NOTE:

2.0.28 > GD > 1.5 does not support gif

http://www.boutell.com/gd/
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post by voltrader »

Right-o -- What I can't understand is why IE renders gifs perfectly.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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...
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Man, why can't there just be one browser that everyone uses that interprets code the same?


WHYYYYYYYY?!?!?!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because competition is good.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

feyd wrote:because competition is good.
It promotes Quality of products.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PrObLeM wrote:
feyd wrote:because competition is good.
It promotes Quality of products.
exactly :)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post 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.
Post Reply