使用PHP GD lib压缩和调整图像大小,不适用于png和怪异的jpg结果

时间:2019-09-13 12:42:21

标签: php image gd php-gd

我正在尝试使用php GD库压缩和调整图像大小。几乎所有关于SO的答案以及其他任何地方都是相同的,但是对于我的解决方案,PNG的转换不正确,而某些jpg的结果却很奇怪。

这是我正在使用的代码:

public function resizeImages() {
    ini_set('max_execution_time', 0);

    //Initial settings, Just specify Source and Destination Image folder.
    $ImagesDirectory    = FCPATH . 'design/img/test/'; //Source Image Directory End with Slash
    $DestImagesDirectory    = FCPATH . 'design/img/test/thumb/'; //Destination Image Directory End with Slash
    $NewImageWidth      = 150; //New Width of Image
    $NewImageHeight     = 150; // New Height of Image
    $Quality        = 90; //Image Quality

    //Open Source Image directory, loop through each Image and resize it.
    if($dir = opendir($ImagesDirectory)){
        while(($file = readdir($dir))!== false){
            $imagePath = $ImagesDirectory.$file;
            $destPath = $DestImagesDirectory.$file;
            $checkValidImage = @getimagesize($imagePath);

            if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
            {
                //Image looks valid, resize.
                if (resize_image($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
                {
                    echo $file.' resize Success!<br />';
                    /*
                    Now Image is resized, may be save information in database?
                    */

                } else {
                    echo $file.' resize Failed!<br />';
                }
            }
        }
        closedir($dir);
    }
}

和resize_image函数如下所示:

function resize_image($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
    {
        list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
        $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
        $NewWidth               = ceil($ImageScale*$iWidth);
        $NewHeight              = ceil($ImageScale*$iHeight);
        $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

        $imagetype = strtolower(image_type_to_mime_type($type));

        switch($imagetype)
        {
            case 'image/jpeg':
                $NewImage = imagecreatefromjpeg($SrcImage);
                break;
            case 'image/png':
                $NewImage = imagecreatefrompng($SrcImage);
                break;
            default:
                return false;
        }

        //allow transparency for pngs
        imagealphablending($NewCanves, false);
        imagesavealpha($NewCanves, true);

        // Resize Image
        if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
        {
            switch ($imagetype) {
                case 'image/jpeg':
                    if(imagejpeg($NewCanves,$DestImage,$Quality))
                    {
                        imagedestroy($NewCanves);
                    }
                    break;
                case 'image/png':
                    if(imagepng($NewCanves,$DestImage,$Quality))
                    {
                        imagedestroy($NewCanves);
                    }
                    break;
                default:
                    return false;
            }
            return true;
        }
    }

每个单一的png都不起作用,它只是返回一个0字节的文件,并且“不支持文件类型”,即使该类型在Windows中被识别为.PNG ...

一些JPG也返回了一个奇怪的结果,请参见以下屏幕截图,该屏幕快照指示了我有关png和一些jpg的问题: faulty images

1 个答案:

答案 0 :(得分:1)

1)提及手册,请勿使用getimagesize来验证文件是否为有效图像:

  

请勿使用getimagesize()来检查给定文件是否为有效图像。使用专用的解决方案,例如Fileinfo扩展名。

$checkValidImage = exif_imagetype($imagePath);
if(file_exists($imagePath) && ($checkValidImage == IMAGETYPE_JPEG || $checkValidImage == IMAGETYPE_PNG))

2)尽管imagejpeg()接受0到100的质量,但是imagepng()想要0到9之间的值,您可以执行以下操作:

if(imagepng($NewCanves,$DestImage,round(($Quality/100)*9)))

3)使用readdir (),您应该跳过当前目录.和父目录..

    while(($file = readdir($dir))!== false){
        if ($file == "." || $file == "..")
            continue;

修改

第2点特别重要,imagepng ()接受大于9的值,但随后因zlib或libpng错误而失败,并生成损坏的png文件。

我尝试调整一些png和jpeg的大小,但这些更改没有遇到任何问题。

相关问题