9

I already checked the file with mime type. If it is jpg or gif it is working perfectly with

$src = imagecreatefromjpeg($tmpName);

and

$src = imagecreatefromgif($tmpName);

but if the image is png $src = imagecreatefrompng($tmpName);

src variable is empty in the png case, but in jpg and gif it is showing it's resource id.

would someone tell me what i need to do?

$finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['photo']['tmp_name']);
    unset($_FILES["photo"]["type"]);
    $_FILES["photo"]["type"] = $mime;

    if ((($_FILES["photo"]["type"] == "image/gif") || ($_FILES["photo"]["type"] == "image/jpeg") || ($_FILES["photo"]["type"] == "image/jpg") || ($_FILES["photo"]["type"] == "image/pjpeg") || ($_FILES["photo"]["type"] == "image/x-png") || ($_FILES["photo"]["type"] == "image/png")) && in_array($extension, $allowedExts)) {

        if ($_FILES["photo"]["error"] > 0) {
            echo "Error uploading file <a href='step-1.php'> Try again. </a>";
            $image_check = 0;
            exit;
        } else {

            $image_check = 1;
            $fileName = $_FILES['photo']['name'];
            $tmpName = $_FILES['photo']['tmp_name'];
            $fileSize = $_FILES['photo']['size'];
            $fileType = $_FILES['photo']['type'];
            list($width1, $height1, $typeb, $attr) = getimagesize($tmpName);

            //$filePath = $uploadDir . $fileName;

            $size = filesize($_FILES['photo']['tmp_name']);

             $ext = $_FILES["photo"]["type"];

            if ($ext == 'image/jpeg' || $ext == 'image/jpg') {
            $src = imagecreatefromjpeg($tmpName);
        } else if ($ext == 'image/gif') {
            $src = imagecreatefromgif($tmpName);
        }
            else if(($ext=='image/png')||($ext=='image/x-png'))
         {
            $src = imagecreatefrompng($tmpName);
           }
       $newwidth1 = 624;


        $newheight1 = ($height1 * $newwidth1) / ($width1);
        $tmp = imagecreatetruecolor($newwidth1, $newheight1);

        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth1, $newheight1, $width1, $height1);
        $filename = "resources/images/" . $append . $_FILES['photo']['name'];

         if ($ext == 'image/jpeg' || $ext == 'image/jpg') {
            imagejpeg($tmp, $filename, 90);
        } else if ($ext == 'image/gif') {
            imagegif($tmp, $filename, 90);
        }
        else if(($ext=='image/png')||($ext=='image/x-png'))
        {

            imagepng($tmp, $filename, 90);
        }
miken32
  • 39,644
  • 15
  • 91
  • 133
user2679683
  • 91
  • 1
  • 1
  • 4
  • can u paste your code snippet ?? – Vaibs_Cool Sep 16 '13 at 05:54
  • In general that function works fine. We'll need something more to go on to help you with your *specific* case. – deceze Sep 16 '13 at 05:56
  • Have you specified the header for PNG type image ? – Shankar Narayana Damodaran Sep 16 '13 at 06:10
  • this code is working perfectly for jpg and gif images but not for png. – user2679683 Sep 16 '13 at 06:10
  • yeah, I tried that header('Content-Type: image/png'); imagepng($tmp, $filename, 90);but it is not working . – user2679683 Sep 16 '13 at 06:11
  • Have you enabled full error reporting (ie `E_ALL`)? You may find `$src` isn't being set at all. Also, rather than rely on the uploaded `type` property, why not look at `$typeb` and compare it to one of the `IMAGETYPE_*` constants? See [here](https://gist.github.com/philBrown/880506#file-imagemanipulator-php-L43) for an example – Phil Sep 16 '13 at 06:22

2 Answers2

9

Write a file

<?php
    phpinfo();
?>

Browse it, you will see JPG Support and GIF create Support are enabled but PNG Support is disabled.

Enable PNG Support, it will work.

enter image description here

MaxEcho
  • 13,989
  • 6
  • 76
  • 86
7

Change from

imagepng($tmp, $filename, 90);

to

imagepng($tmp, $filename);
PgKc
  • 71
  • 1
  • 1
  • 1
    Note that [`imagepng`](https://www.php.net/manual/en/function.imagepng.php) expects a `quality` value between -1 and 9. I assume this is the reason for the proposed change. – showdev Jul 22 '19 at 22:46