Need help with php image upload

Discussion in 'PHP' started by new2006k, Dec 16, 2013.

  1. #1
    Can someone with PHP experience check these code and see why when I'm uploading a PNG image I get white image as result. Thank you for your help in advance.

    
    /*Function For Uploading Fixed Size Image*/
    function info_file_upload_imageonly($f,$dest_dir,$newname) // file upload , check formats,check
    {
      //  $dest_dir = '../product_big';
    
      $upstat=0;
      $dest = $dest_dir . '/' . $f['name'];
      $up_err_msg='';
      $extn='';
      //echo $f['name'] . "<br>";
      $arr=explode(".",$f['name']);
      $ii=count($arr);
      //echo $ii . "<br>";
      $ii=$ii-1;
      $extn=strtolower($arr[$ii]);
      //$extn=strtolower($extn1);
      $newname=$dest_dir . "/" . $newname . "." . $extn;
      //echo $newname . "<br>";
      if ($extn!='jpg' && $extn!='png' && $extn!='jpeg' && $extn!='gif')
      { //echo $extn;
      $up_err_msg="Invalid File Format.";
      // echo $up_err_msg . $extn;
      }
      else{  // upload file
      if (!is_uploaded_file ($f['tmp_name']))
      {
      $up_err_msg="Error: No file uploaded";
    
      }
      else if (!file_exists ($dest_dir))
      {
      $up_err_msg="Error: Destination directory " . $dest_dir . "does not exist!";
      $upstat=1;
      }
      else if (!is_dir ($dest_dir))
      {
      $up_err_msg="Error: Destination directory " . $dest_dir . " is not a directory!";
      $upstat=1;
      }
      else if (file_exists ($newname)) // previously file_exists path was $dest
      {
      $up_err_msg="File already exist. Please rename the file and upload it again.";
      $upstat=1;
      }
    
    // all clear, move the file to its permanent location
      if ($upstat!=1)
      {
      $r = move_uploaded_file ($f['tmp_name'], $dest);
      if ($r == false)
      {
      $up_err_msg="Error: Could not copy file to " . $dest;
      exit;
      }
      else
      {
      $up_err_msg="";
      chmod ($dest,0755);
      @rename($dest,$newname);
      }
      }
      } //end upload file
    
      $up_err_msg1[0]["msg"]=$up_err_msg;
      $up_err_msg1[0]["extn"]=$extn;
      return $up_err_msg1;
    }
    
    
    
    
    
    
    /* Image Uploading and Resizing function */
    function info_img_upload_resize_gen1($f,$newname,$path,$thumb_size,$big_size,$big_size_width=0) //
    {
      //upload section
      $dest_dir = substr($path,0,strlen($path)-1);
      $upstat=0;
      $dest = $path . $f['name'];
      $up_err_msg='';
      $extn='';
      //echo $f['name'] . "<br>";
      $arr=explode(".",$f['name']);
      $ii=count($arr);
      //echo $ii . "<br>";
      $ii=$ii-1;
      $extn=strtolower($arr[$ii]);
      //$extn=strtolower($extn1);
      //$new_name=$newname;
      $imgname= $newname . "." . $extn;
      $newname=$path . $newname . "." . $extn;
      //print_r($arr);
      //echo $newname; die();
      //$newname1="../parks_pic/thumb/" . $new_name . "." . $extn;
      //echo $newname . "<br>";
      //if ($extn!='jpg' && $extn!='jpeg' && $extn!='gif' && $extn!='png')
      if ($extn!='jpg' && $extn!='jpeg' && $extn!='gif' && $extn!='png')
      {
      $up_err_msg="Invalid File Format.";
      }
      else
      {  // upload file
    
      if (!is_uploaded_file ($f['tmp_name']))
      {
      $up_err_msg="Error: No file uploaded";
      }
      else if (!file_exists ($dest_dir))
      {
      $up_err_msg="Error: Destination directory " . $dest_dir . "does not exist!";
      $upstat=1;
      }
      else if (!is_dir ($dest_dir))
      {
      $up_err_msg="Error: Destination directory " . $dest_dir . " is not a directory!";
      $upstat=1;
      }
      else if (file_exists ($dest))
      {
      $up_err_msg="File already exist. Please rename the file and upload it again.";
      $upstat=1;
      }
    
      // all clear, move the file to its permanent location
      if ($upstat!=1)
      {
      $r = move_uploaded_file($f['tmp_name'], $dest);
      if ($r == false)
      {
      $up_err_msg="Error: Could not copy file to " . $dest;
      exit;
      }
      else
      {
      $up_err_msg="";
      chmod ($dest,0755);
      $dest1=$dest;
    
      //
      //if (file_exists ($dest))
      //  unlink($newname);
      //
    
      rename($dest,$newname);
    
      //rename($dest1,$newname1);
      }
      }
      } //end upload file
    
      $up_err_msg1[0]["msg"]=$up_err_msg;
      $up_err_msg1[0]["extn"]=$extn;
      if($up_err_msg1[0]["msg"]!="")
      {
      return $up_err_msg1;
      }
    
      $filename1 = $newname;
    
      list($width, $height) = getimagesize($filename1);
      $res_img= $path . $filename1;
      if ($width>$thumb_size || $height>$thumb_size)
      {
    
    
    //$extnarr=explode(".",$filename);
    //$extn=$extnarr[1];
    
    
      if($big_size==0)
      {
      $th_filename1 = $path . $imgname;
    
      }
      else
      {
      $th_filename1 = $path ."thumb/". $imgname;
      $big_filename1 = $path . $imgname;
      }
      $filename=$filename1;
      $percent = 0.2;
    
      // Content type
      if ($extn=='jpg' || $extn=='jpeg')
      header('Content-type: image/jpeg');
      else if ($extn=='gif')
      header('Content-type: image/gif');
      else if ($extn=='png')
      header('Content-type: image/png');
      //end content type
      // Get new dimensions
      list($width, $height) = getimagesize($filename);
      $ratio=$width/$height;
      if ($width>$height)
      {
      $new_width=$thumb_size;
      $new_height=(int)($thumb_size/$ratio);
      }
      else
      {
      $new_height=$thumb_size;
      $new_width=(int)($thumb_size*$ratio);
      }
    
      if($big_size!=0)
      {
      if ($width>$height)
      {
      $new_big_width=$big_size;
      $new_big_height=(int)($big_size/$ratio);
      }
      else
      {
      $new_big_height=$big_size;
      $new_big_width=(int)($big_size*$ratio);
      }
           
          if($big_size_width>0) {
      $new_big_width=(int)$big_size_width;
          }
    
      }
    
      // echo "Featutred : " . $featured . " width=" . $new_width . " height: " . $new_height;
      //$new_width = $width * $percent;
      //$new_height = $height * $percent;
      // end dimension
      // Resample
    
      $image_p = imagecreatetruecolor($new_width, $new_height);
      if ($extn=='jpg' || $extn=='jpeg')
      {
      $image = imagecreatefromjpeg($filename);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagejpeg($image_p,$th_filename1, 100);
      }
      else if ($extn=='gif')
      {
      $image = imagecreatefromgif($filename);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagegif($image_p,$th_filename1, 100);
      }
      else if ($extn=='png')
      {
      $image = imagecreatefrompng($filename);
      //imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagepng($image_p,$th_filename1, 100);
      }
    
      //die();
      if($big_size!=0)
      {
      list($width, $height) = getimagesize($filename1);
      //$res_img= $path . $filename1;
      if ($width>$big_size || $height>$big_size)
      {
      $image_p = imagecreatetruecolor($new_big_width, $new_big_height);
      $new_width = $new_big_width;
      $new_height = $new_big_height;
      if ($extn=='jpg' || $extn=='jpeg')
      {
      $image = imagecreatefromjpeg($filename);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagejpeg($image_p,$big_filename1, 100);
      }
      else if ($extn=='gif')
      {
      $image = imagecreatefromgif($filename);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagegif($image_p,$big_filename1, 100);
      }
      else if ($extn=='png')
      {
      $image = imagecreatefrompng($filename);
      //imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // Output
      imagepng($image_p,$big_filename1, 100);
      }
      }
      }
      }
      else
      {
      copy($filename1,$path.$imgname);
      }
    
    Code (markup):

     
    new2006k, Dec 16, 2013 IP
  2. RiptideSolutions

    RiptideSolutions Active Member

    Messages:
    142
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    65
    #2
    I could be completely off on this, but looking quickly over your code, it seems that you have a difference between the .png extensions and all the other extensions. If all the other extensions are working properly and PNG is the only one that is off, I would start with this and see if this works:

    // imagecopyresampled(......
    PHP:
    Seems you have // in there for the png both times "imagecopyresampled" is called towards the bottom of your script. This will cause that line of code to not "fire" or run. I would try to remove the // from them and run the script again to see if that fixes it. Again, I could be totally off, but that's what I found from just a quick scan of the code.
     
    RiptideSolutions, Dec 16, 2013 IP
  3. new2006k

    new2006k Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I tried
     
    new2006k, Dec 17, 2013 IP