0

I have a basic image upload/compression script on my website and most images seem to work fine. Having an issue with PNG for some reason, whenever I try to add a PNG image, the actual image is uploaded but the compressed image is saved as a full black square?

I can't seem to find what could be wrong with this script? The images were saving without a filetype, so I did edit the code to upload all as JPG to try to fix the error, it did fix what they were being saved as but the black images remain the same?

Can anybody point me in the right direction here?

if(isset($_POST['upload_images'])){
if(count($_FILES['upload']['name']) > 0){
        //Loop through each file
        for($i=0; $i<count($_FILES['upload']['name']); $i++) {
          //Get the temp file path
            $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

            //Make sure we have a filepath
            if($tmpFilePath != ""){

                //save the filename
                $shortname = $_FILES['upload']['name'][$i];

                //save the url and the file
                $dirname = "galleries/".$portfolio_ref."/";
                $filePath = $dirname.$_FILES['upload']['name'][$i];
                $new_filename = date('dmYHis').rand(100000,9999999);
                $new_filename2 = $dirname.$new_filename.'.jpg';



                //Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $new_filename2)) {

$file = $new_filename2;
$data = $filePath;    
$cut_name = substr($data, strpos($data, "/") + 1);  
$cut_name = explode('/',$cut_name); 
$cut_name = end($cut_name); 

$newfile = $dirname.'thb_'.$new_filename.'.jpg';
$info = getimagesize($file);


list($width, $height) = getimagesize($file);

    $max_width = 300;
    $max_height = 400;

        //try max width first...
    $ratio = $max_width / $width;
    $new_width = $max_width;
    $new_height = $height * $ratio;

    //if that didn't work
    if ($new_height > $max_height) {
        $ratio = $max_height / $height;     
        $new_height = $max_height;
        $new_width = $width * $ratio;
    }

if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($file);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($file);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($file);
    elseif ($info['mime'] == 'image/jpg') $image = imagecreatefromjpeg($file);
    elseif ($info['mime'] == 'image/JPEG') $image = imagecreatefromjpeg($file);
    elseif ($info['mime'] == 'image/PNG') $image = imagecreatefrompng($file);
    elseif ($info['mime'] == 'image/GIF') $image = imagecreatefromgif($file);
    elseif ($info['mime'] == 'image/JPG') $image = imagecreatefromjpeg($file);

$image = imagecreatetruecolor($new_width, $new_height);
$photo = imagecreatefromjpeg($file);
imagecopyresampled($image, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


    imagejpeg($image, $newfile, 70);
    $origional_image = $file;
    $compressed_image = $newfile;
    $digit_date = date("dmy");
    $upload_date = date("Y-m-d");
    $imgid = $digit_date.$i.rand();

     mysqli_query($conn,"INSERT INTO umsikzw_images (image_id,portfolio_ref,thumb_link,full_link,image_name,date) VALUES ('$imgid','$portfolio_ref','$compressed_image','$origional_image','No Information','$upload_date')");


                }
              }
        }
}
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Snappysites
  • 804
  • 1
  • 10
  • 41
  • 1
    https://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent Second answer was good enough for me. But chosen answer may be good for you. All in all I'va had the same problem with PNGs already and so did this other post OP – Lou May 07 '18 at 15:04
  • This thread did the trick for me. Thanks! – Snappysites May 07 '18 at 16:17

0 Answers0