Jump to content

php upload image help!


azukah

Recommended Posts

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;

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/195941-php-upload-image-help/
Share on other sites

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?

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

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  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.