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