【问题标题】:imagecreatefrompng is not working at allimagecreatefrompng 根本不工作
【发布时间】:2013-09-20 04:45:34
【问题描述】:

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

$src = imagecreatefromjpeg($tmpName);

$src = imagecreatefromgif($tmpName);

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

src 变量在 png 的情况下为空,但在 jpggif 中显示为resource id

有人能告诉我我需要做什么吗?

$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);
        }

【问题讨论】:

  • 你能粘贴你的代码 sn-p 吗??
  • 一般来说这个功能可以正常工作。我们需要更多的东西来帮助您处理您的具体案例。
  • 您是否指定了PNG类型图像的标题?
  • 此代码适用于 jpg 和 gif 图像,但不适用于 png。
  • 是的,我试过那个 header('Content-Type: image/png'); imagepng($tmp, $filename, 90);但它不起作用。

标签: php php-gd


【解决方案1】:

写一个文件

<?php
    phpinfo();
?>

浏览它,你会看到JPG SupportGIF create SupportenabledPNG Supportdisabled

启用PNG Support,它将起作用。

【讨论】:

  • 答案已编辑,您可能需要启用 PNG 支持
  • 你试试,否则 if((strtolower($ext)=='image/png')||(strtolower($ext)=='image/x-png'))跨度>
  • 另一件事,必须检查您正在尝试的图像是否是有效的 png。
  • mime type 用于检查图像类型。我想这是验证图像是否为 png。如果我错了,请纠正我。
【解决方案2】:

改变

imagepng($tmp, $filename, 90);

imagepng($tmp, $filename);

【讨论】:

  • 请注意,imagepng 期望 quality 的值介于 -1 和 9 之间。我认为这是建议更改的原因。
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2016-10-29
  • 2018-02-10
  • 2017-03-19
  • 2013-11-08
  • 2015-08-23
  • 2017-10-14
  • 1970-01-01
相关资源
最近更新 更多