php imagecreatefrompng 根本不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18821162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:16:42  来源:igfitidea点击:

imagecreatefrompng is not working at all

phpphp-gd

提问by user2679683

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

我已经用 mime 类型检查了文件。如果它是 jpg 或 gif,它可以完美地配合使用

$src = imagecreatefromjpeg($tmpName);

and

$src = imagecreatefromgif($tmpName);

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

但如果图像是png$src = imagecreatefrompng($tmpName);

srcvariable is empty in the pngcase, but in jpgand gifit is showing it's resource id.

src变量在png情况下为空,但在jpggif 中它显示它是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);
        }

回答by MaxEcho

Write a file

写文件

<?php
    phpinfo();
?>

Browse it, you will see JPG Supportand GIF create Supportare enabledbut PNG Supportis disabled.

浏览它,你会看到JPG Supportand GIF create Supportare enabledbut PNG Supportis disabled

Enable PNG Support, it will work.

启用PNG Support,它将起作用。

enter image description here

在此处输入图片说明

回答by PgKc

Change from

更改自

imagepng($tmp, $filename, 90);

to

imagepng($tmp, $filename);