【问题标题】:PHP's imagepng() method saves an invalid imagePHP 的 imagepng() 方法保存无效图像
【发布时间】:2012-02-03 22:07:15
【问题描述】:

我正在使用 GD 库自动生成上传图像的缩略图版本。我调用适当的image____() 函数以与原始格式相同的格式保存。我的代码适用于 JPEG 和 GIF,但如果我上传 PNG 文件,生成的缩略图无效。它实际上只包含 33 个字节(到目前为止我尝试过的任何源 PNG)。此图像不显示在浏览器中,也无法通过 Preview(在 MacOS 上)打开。

我使用 imagecreatetruecolor() 和 imagecopyresampled() 来生成缩略图,如下所示:

function _resizeImageToFit($resource, $size)
{
    $sourceWidth = imagesx($resource);
    $sourceHeight = imagesy($resource);  
    if($sourceWidth >= $sourceHeight) {
        // landscape or square
        $newHeight = 1.0*$size/$sourceWidth*$sourceHeight;
        $newWidth = $size;
    }
    else {
        // portrait
        $newWidth = 1.0*$size/$sourceHeight*$sourceWidth;
        $newHeight = $size;
    }
    $thmb = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($thmb, $resource, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
    return $thmb;
}

以下是我的设置的版本信息(它是 MAMP 版本 1.9.4)

PHP 版本 5.3.2 GD版捆绑(2.0.34兼容)

以下是生成的无效缩略图 (PNG) 示例:

âPNG

IHDRdaØMì∞

【问题讨论】:

  • 这个函数的返回值怎么办?保存到文件系统还是直接提供?
  • 我打电话给$saveSuccess = $imagesave($thumbResource, $thumbPath, 100);,然后是imagedestroy($thumbResource);。我在这里根据图像类型使用一个变量来调用图像函数。当这是一个 PNG 时,调用最终为 imagepng()。 $saveSuccess(imagepng 返回的值)设置为 TRUE 表示成功,即使图像已损坏。

标签: php gd


【解决方案1】:

我发现了我的错误。 imagepng() 采用 0 到 9 的质量值范围,而 imagejpeg() 采用 0 到 100 的范围,而 imagegif() 不采用任何此类参数。我试图保存质量为 100 的 PNG。

所以,这是一个可爱的 RTM 案例。感谢您的回复。

http://ca3.php.net/manual/en/function.imagepng.php

http://ca3.php.net/manual/en/function.imagejpeg.php

http://ca3.php.net/manual/en/function.imagegif.php

【讨论】:

    【解决方案2】:

    试试这个功能。

    /** 
    * Crop new images using the source image
    *
    * @param  string $source - Image source
    * @param  string $destination - Image destination
    * @param  integer $thumbW - Width for the new image
    * @param  integer $thumbH - Height for the new image
    * @param  string $imageType - Type of the image
    * 
    * @return bool 
    */
    function cropImage($source, $destination, $thumbW, $thumbH, $imageType) 
    {
        list($width, $height, $type, $attr) = getimagesize($source);
        $x = 0;
        $y = 0;
        if ($width*$thumbH>$height*$thumbW) {
            $x = ceil(($width - $height*$thumbW/$thumbH)/2);
            $width = $height*$thumbW/$thumbH;
        } else {
            $y = ceil(($height - $width*$thumbH/$thumbW)/2);
            $height = $width*$thumbH/$thumbW;
        }
    
        $newImage = imagecreatetruecolor($thumbW, $thumbH) or die ('Can not use GD');
    
        /*
        if ($extension=='jpg' || $extension=='jpeg') {
            $image = imagecreatefromjpeg($source);
        } else if ($extension=='gif') {
            $image = imagecreatefromgif($source);
        } else if ($extension=='png') {
            $image = imagecreatefrompng($source);
        } 
        */
        switch($imageType) {
            case "image/gif":
                $image = imagecreatefromgif($source);
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                $image = imagecreatefromjpeg($source);
                break;
            case "image/png":
            case "image/x-png":
                $image = imagecreatefrompng($source);
                break;
        }
    
        if (!@imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbW, $thumbH, $width, $height)) {
            return false;
        } else {
            imagejpeg($newImage, $destination,100);
            imagedestroy($image);
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-21
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2016-05-19
      • 2021-05-28
      相关资源
      最近更新 更多