1

I am uploading a file image. I compress it to a specified width and height, but it always shows a blank image when I attempt to display a PNG, but it WILL display jpg or jpeg files.

function compress($extention, $destination, $quality, $tmpsource)
{
    $extention = strtolower($extention);
    list($width, $height) = getimagesize($destination);
    $y = (800 * $height / $width);
    $getType = null;
    if ($extention == "jpeg") {
        $source = imagecreatefromjpeg($destination);
    }
    if ($extention == "jpg") {
        $source = imagecreatefromjpeg($destination);
    }
    if ($extention == "png") {
        $source = imagecreatefrompng($destination);
    }

    $thumb = imagecreatetruecolor(800, $y);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 800, $y, $width, $height);

    if ($extention == "jpeg") {
        imagejpeg($thumb, $destination, $quality);
    }
    if ($extention == "jpg") {
        imagejpeg($thumb, $destination, $quality);
    }
    if ($extention == "png") {
        imagepng($thumb, $destination, $quality);
    }

    return $destination;
}

Here is where I call it:

if (in_array($imgTypeArr[$i], $allowedTypes))
{
    $newImgName = uniqid($newUniqName . "_") . "." . $getExt;
    $imgUploadArr[] = $newImgName;
    $destination = "../images/products/" . $proFolder . "/" . $newImgName;
    move_uploaded_file($imgTmpArr[$i], $destination);
    compress($getExt, $destination, 100);
}
showdev
  • 28,454
  • 37
  • 55
  • 73
DrCod3r
  • 91
  • 2
  • 6
  • the destination is the file from the folder im doing an override up there – DrCod3r Jul 22 '19 at 21:55
  • You should include the code of the actual function that loads the png. Is it a custom function you created, or is it part of a third party source? – SmugDoodleBug Jul 22 '19 at 21:57
  • Why don't you use a library like this: http://image.intervention.io – Latheesan Jul 22 '19 at 22:02
  • Please include the code for the following functions: imagecreatefrompng() & imagepng. We need to see how they function, so we can determine if the creation process fails in those portions of code. – SmugDoodleBug Jul 22 '19 at 22:03
  • @JRFerrell See [imagecreatefrompng](https://www.php.net/manual/en/function.imagecreatefrompng.php). – showdev Jul 22 '19 at 22:04
  • i didnt get you about code for the following function: imagecreatefrompng – DrCod3r Jul 22 '19 at 22:08
  • but im moving the file then after i do the compress – DrCod3r Jul 22 '19 at 22:17
  • Oh, sorry. I see what you mean. And you said JPGs work fine? – showdev Jul 22 '19 at 22:17
  • yes jpeg and jpg works like charm the problem only with png show blank img like nothing – DrCod3r Jul 22 '19 at 22:24
  • actually ive that problem on my live website so i decide to test on local the same problem do you c any probs in my code ? – DrCod3r Jul 22 '19 at 22:38
  • It might be that [`imagepng`](https://www.php.net/manual/en/function.imagepng.php) expects a `quality` value between -1 and 9, and you're passing it 100. – showdev Jul 22 '19 at 22:43
  • yes thanks alot @showdev it fixes the problem but it loses its transparency and replaced with black any fix for that ? – DrCod3r Jul 22 '19 at 23:18
  • It's possible that you'll need to enable the blending mode for PNGs with [`imagealphablending`](https://www.php.net/manual/en/function.imagealphablending.php) and/or save alpha channel information with [`imagesavealpha`](https://www.php.net/manual/en/function.imagesavealpha.php). See [PHP GD imagecreatefromstring discards transparency](https://stackoverflow.com/questions/12181573/php-gd-imagecreatefromstring-discards-transparency). If you run into trouble, you may want to post a new question. – showdev Jul 22 '19 at 23:34
  • works like charm thanks alot @showdev :) – DrCod3r Jul 22 '19 at 23:37

1 Answers1

0

imagepng expects a quality value between -1 and 9, but your code passes a value of 100:

compress($getExt, $destination, 100);

quality
Compression level: from 0 (no compression) to 9. The default (-1) uses the zlib compression default. For more information see the » zlib manual.

You can leave off that value to use the default zlib compression for PNGs and the default IJG compression (about 75) for JPGs:

compress($getExt, $destination);

Or you may want to specify different quality values for JPGs and PNGs.

showdev
  • 28,454
  • 37
  • 55
  • 73