Image Resizing Function Problem

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
cojsteve
Forum Newbie
Posts: 1
Joined: Fri Oct 07, 2005 12:45 pm
Location: Antigua, Guatemala

Image Resizing Function Problem

Post by cojsteve »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am having trouble with the following bit of code. The intention is to take an image file that will be uploaded and resize it until it is of an acceptable filesize. I have the function resize($imageFile) that does the resizing which is working. I then have a function adjustFileSize($imageFile) which I want to call the resize function until the filesize of the image is of an acceptable size. The adjustFileSize bit is not working properly. The problem seems to be that after it calls the resize function, which is resizing the image,  the $filesize variable does not change to reflect the smaller filesize of the image. Any help would be much appreciated. Here is the code:

Code: Select all

function resize($imageFile) 
	{
	// File and new size
	$filename = $imageFile;
	$percent = 0.66;
	

	// Get new sizes
	list($width, $height) = getimagesize($filename);
	$newwidth = $width * $percent;
	$newheight = $height * $percent;
	
	// Load
	$thumb = imagecreatetruecolor($newwidth, $newheight);
	  
	preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);
   	switch (strtolower($ext[2]))
		{
		   case 'jpg' : 
		   case 'jpeg': $source = imagecreatefromjpeg($filename);
						 break;
		   case 'gif' : $source = imagecreatefromgif($filename);
						 break;
		   case 'png' : $source = imagecreatefrompng($filename);
						 break;
	   }


	// Resize
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	
	// Output
	
	preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);
   	switch (strtolower($ext[2]))
		{
		   case 'jpg' : 
		   case 'jpeg':imagejpeg($thumb,$filename,50);
						 break;
		   case 'gif' :imagegif($thumb,$filename);
						 break;
		   case 'png' : imagepng($thumb,$filename);
						 break;
	   }
	}

//resize image until filesize is apropriate	
function adjustFileSize($imageFile, $maxFileSize)	
	{
	$filesize = filesize($imageFile);
	do 
		{
		resize($imageFile);
		$filesize = filesize($imageFile);
		}
	while($filesize > $maxFileSize);
	}

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Moved to Graphics.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

echo $filesize in your do...while loop and see what you get. Are you sure that resize() is resizing the image properly?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply