Page 1 of 1

cant get small images to upload _SOLVED_

Posted: Sun Apr 29, 2012 11:22 pm
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);			
                                                        }
                
                }

Re: cant get small images to upload

Posted: Mon Apr 30, 2012 1:48 am
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);			
}
?>

Re: cant get small images to upload

Posted: Mon Apr 30, 2012 6:02 pm
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.