cant get small images to upload _SOLVED_

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
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

cant get small images to upload _SOLVED_

Post by me! »

I have been at this for a while not and being that it is about 12:30am perhaps I am just not seeing it...

Problem is this code works good for large (like digital camera huge) images. but I have a small .jpg that came off a website that is only a few hunderd pix high and wide it it wont save. The file path gets saved to the db but the image never makes it to the upload directory.

Any thoughts are helpful.

Code: Select all

// do we have an image to save?
	if ($_FILES['uploadfile']["size"] > 1)
		{
             
                //new weith        
                $newwidth=500;
			
			// format file name and path same as the item #
			$filename = "equipment_images/item-$id-".time().".jpg";

			// This is the temporary file created by PHP
			$uploadedfile = $_FILES['uploadfile']['tmp_name'];
                        
                        // remove the old file if there is one
                        $query = "SELECT picture FROM _wv1k_equipment WHERE id='$id'  ";
                                    $result = mysql_query($query) or die(mysql_error());
                                    while($mysql = mysql_fetch_array($result))
                                            {
                                            $remove_image = $mysql["picture"];
                                            }
                            if ($remove_image != '')
                                    {
                                    unlink("$remove_image");
                                    echo "Removing: $remove_image<br />";
                                    }

			// Create an Image from it so we can do the resize
			$src = imagecreatefromjpeg($uploadedfile);

			// Capture the original size of the uploaded image
			list($width,$height)=getimagesize($uploadedfile);

                                        //if it is smaller thgan the "new" width do nothing, if it's bigger shrink it
					if ($width >= $newwidth) //($width > $newwidth)
						{
						// Create an Image from it so we can do the re-size
						$src = imagecreatefromjpeg($uploadedfile);
						
						// Resized the image to be the width as defined by $newwidth
						// and maintain the original aspect ratio. This prevents the image from being "stretched" or "squashed". 
						$newheight=($height/$width)*$newwidth;
						$tmp=imagecreatetruecolor($newwidth,$newheight);
						
						// this line actually does the image resizing, copying from the original
						// image into the $tmp image
						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
						imagejpeg($tmp,$filename,100);
				
						imagedestroy($src);
						imagedestroy($tmp);
						 // NOTE: PHP will clean up the temp file it created when the request
						// has completed.	
						}else	{
								// if we dont need to re-size it just save the image
								move_uploaded_file($uploadedfile, $filename);			
                                                        }
                
                }
Last edited by me! on Mon Apr 30, 2012 6:03 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: cant get small images to upload

Post by social_experiment »

I tested it without the code writing the filename to the database and the results i get - creates a copy of the large image (resized) and uploads small images. What results do you get if you test the code without the sql component?

The code i used :

Code: Select all

<?php
$newwidth = 500;
		
$filename = time() . '.jpg';
//
$uploadedfile = $_FILES['fupload']['tmp_name'];
// 
$src = imagecreatefromjpeg($uploadedfile);
//
list($width,$height)= getimagesize($uploadedfile);
// 
if ($width >= $newwidth) {
	
 $src = imagecreatefromjpeg($uploadedfile);
			
 $newheight = ($height / $width) * $newwidth;
 $tmp = imagecreatetruecolor($newwidth, $newheight);
			
 imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 imagejpeg($tmp, $filename, 100);
			
 imagedestroy($src);
 imagedestroy($tmp);			
}
else {
 move_uploaded_file($uploadedfile, $filename);			
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: cant get small images to upload

Post by me! »

Yep was up to late last night.
This morning I opened up the file and noticed that I had pasted another upload snippet in the beginning of the page just to add to it. problem was I never noticed THAT I LEFT IN! oops!
So the code and sql are just fine.
Post Reply