【问题标题】:Problem with PNG image loading function not working, and showing a blank imagePNG图像加载功能无法正常工作并显示空白图像的问题
【发布时间】:2019-11-30 21:53:38
【问题描述】:

我正在上传文件图片。我将它压缩到指定的宽度和高度,但当我尝试显示 PNG 时它总是显示空白图像,但它会显示 jpg 或 jpeg 文件。

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

这就是我所说的:

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

【问题讨论】:

  • 目标是文件夹中的文件,我在那里进行了覆盖
  • 您应该包含加载 png 的实际函数的代码。它是您创建的自定义函数,还是第三方来源的一部分?
  • 你为什么不使用这样的库:image.intervention.io
  • 请包含以下函数的代码:imagecreatefrompng() & imagepng。我们需要看看它们是如何运作的,这样我们才能确定这些代码部分的创建过程是否失败。
  • @JRFerrell 见imagecreatefrompng

标签: php image image-processing


【解决方案1】:

imagepng 需要介于 -1 和 9 之间的 quality 值,但您的代码传递的值是 100:

compress($getExt, $destination, 100);

质量
压缩级别:从 0(不压缩)到 9。默认 (-1) 使用 zlib 压缩默认值。有关详细信息,请参阅 » zlib 手册。

您可以不使用该值以对 PNG 使用默认的 zlib 压缩,对 JPG 使用默认的 IJG 压缩(大约 75):

compress($getExt, $destination);

或者您可能想要为 JPG 和 PNG 指定不同的质量值。

【讨论】:

    猜你喜欢
    • 2017-04-11
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多