Not always resizing?

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Not always resizing?

Post by psychotomus »

how come my code doesn't resize all images 100% of the time?

Code: Select all

if ($file_type == "jpg")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "screens/" . $filename))
			{
				if(!$src = imagecreatefromjpeg("screens/" .$filename))
				{
					echo 'error creating image';
				}
				else
				{
					$w = imagesx($src); 
					$h = imagesy($src);
					
					if ($w > 350 || $h > 250)
					{
						
						// Create thumbnail canvas 
						$destimg = imagecreatetruecolor ("350", "250") or die ("Problem In Creating image"); 
				
						// For JPEG 
						$srcimg = imagecreatefromjpeg ("screens/" . $filename) or die ("Problem In opening Source Image");  
						
						// Resample the image from $srcimg to the $destimg
						imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, "350", "250", imagesx($srcimg), imagesy($srcimg)) or die ("Problem In resizing");   
						
						// Save JPG 
						imagejpeg ($destimg, "screens/" . $filename) or die("Problem In saving");  
					}
				}
			}
			else
			{
				$eRR = "Could not upload file";
			}
		}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Let me just say, were not here to debug your scripts for you. Don't take our precious time and effort around here for granted. At minimum, you should post what you've attempted to solve the solution, such as any debuging information.

What kind of images were you trying to upload? Size of image? Error messages? Anything is better than nothing.

To answer your question with the same effort your given us, likely because your if statements are failing. :wink:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You have a condition that makes it only resize an image if the width is greater than 350 or the height is greater than 250.

Code: Select all

if ($w > 350 || $h > 250)
You really ought to rewrite the resizing code anyway. It's ignoring the aspect ratio when it generates the smaller image which is going to make the thumbnails look really nasty.
Post Reply