azukah Posted March 20, 2010 Share Posted March 20, 2010 hi- this code which works fine when i select and image to upload but it doesn't work when no image is selected. the code is supposed to upload the product even if no image is selected becasue it insert the "generic.jpg" image whenever no image is selected ... make sense?? any ideas would be greatly appreciate it! i <?php function upload_prod_img(){ $originalFile= $_FILES['prod_img']; $shortname=substr($originalFile['name'], 0, -4); $timestamp=substr(time(), -4, 4); $imageNewName = $shortname.$timestamp; $dimensions = getimagesize($originalFile['tmp_name']); $desiredW= 300; if($dimensions[0]>0){ $desiredH= number_format($dimensions[1] /$dimensions[0] *$desiredW); $dest= imagecreatetruecolor($desiredW, $desiredH); imageantialias($dest, TRUE); switch($dimensions[2]) { case 1: $src = imagecreatefromgif($originalFile['tmp_name']); break; case 2: $src = imagecreatefromjpeg($originalFile['tmp_name']); break; case 3: $src = imagecreatefrompng($originalFile['tmp_name']); break; default: return false; break; } imagecopyresampled($dest, $src, 0, 0, 0, 0, $desiredW, $desiredH, $dimensions[0], $dimensions[1]); $imageNewName = strtolower($imageNewName); $imageNewName =str_replace(" ", "_",$imageNewName); $imageNewName=$imageNewName.".jpg"; $destURL="../../images/products/".$imageNewName;//PATH imagejpeg($dest,$destURL, 80); }else{ $imageNewName="generic.jpg"; } return $imageNewName; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195941-php-upload-image-help/ Share on other sites More sharing options...
azukah Posted March 20, 2010 Author Share Posted March 20, 2010 i checked again and i actually get the product added with the generic.jpg image BUT.... i still get an error msf on this line: $dimensions = getimagesize($originalFile['tmp_name']) if i comment the line out i don't get the error and everything is fine but the code doesn't work when i do select an image bcz i need the dimensions ...help, please? Quote Link to comment https://forums.phpfreaks.com/topic/195941-php-upload-image-help/#findComment-1029222 Share on other sites More sharing options...
DavidAM Posted March 20, 2010 Share Posted March 20, 2010 You are getting the error because the file does not exist. A little change to the sequence and the if statement should solve the problem: if (file_exists ($originalFile['tmp_name'])) { $dimensions = getimagesize($originalFile['tmp_name']); $desiredW= 300; // if($dimensions[0]>0){ REMOVED IN FAVOR OF IF ABOVE $desiredH= number_format($dimensions[1] /$dimensions[0] *$desiredW); $dest= imagecreatetruecolor($desiredW, $desiredH); imageantialias($dest, TRUE); switch($dimensions[2]) { case 1: $src = imagecreatefromgif($originalFile['tmp_name']); break; case 2: $src = imagecreatefromjpeg($originalFile['tmp_name']); break; case 3: $src = imagecreatefrompng($originalFile['tmp_name']); break; default: return false; break; } imagecopyresampled($dest, $src, 0, 0, 0, 0, $desiredW, $desiredH, $dimensions[0], $dimensions[1]); $imageNewName = strtolower($imageNewName); $imageNewName =str_replace(" ", "_",$imageNewName); $imageNewName=$imageNewName.".jpg"; $destURL="../../images/products/".$imageNewName;//PATH imagejpeg($dest,$destURL, 80); }else{ // OF COURSE $dimensions is not defined now, so if you need it later, deal with that $imageNewName="generic.jpg"; } PS: Use [ code ] tags when posting code, it makes it easier to read Quote Link to comment https://forums.phpfreaks.com/topic/195941-php-upload-image-help/#findComment-1029227 Share on other sites More sharing options...
azukah Posted March 20, 2010 Author Share Posted March 20, 2010 thanks a lot mate ..it works!! and thanks for the heads up on the code tags. i won't forget again! Quote Link to comment https://forums.phpfreaks.com/topic/195941-php-upload-image-help/#findComment-1029239 Share on other sites More sharing options...
DavidAM Posted March 20, 2010 Share Posted March 20, 2010 Glad to help. One thing I did not think about before. The IF that I removed, may need to be brought back into play. If the file the user uploads is NOT an image, or is corrupted, you may want to use that IF to substitute the generic file in its place. Just something to consider. Happy Coding Quote Link to comment https://forums.phpfreaks.com/topic/195941-php-upload-image-help/#findComment-1029261 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.