图像大小调整:jpeg质量差,黑色PNG背景

时间:2021-02-03 21:23:02

Final: I've decided to basically use this: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

最后:我决定基本上使用它:http://shiftingpixel.com/2008/03/03/smart-image-resizer/

As it handles everything, Ive turned caching off and do this in the admin controllers:

当它处理所有事情时,我已经关闭缓存并在管理控制器中执行此操作:

    $image = file_get_contents(SITE_ADMIN_IMAGE.'/SmartImage.php?width='.$this->thumb_width.'&height='.$this->thumb_height.'&image=/images/'.$this->image_directory.'/'.$formData['image_url'].'');
    file_put_contents(ROOT_PATH.'/public/images/'.$this->image_directory.'/thumb/'.$formData['image_url'], $image);

EDIT: I found this works, however it creates very sharp edges, it doesn't look right.

编辑:我发现这是有效的,但它创建非常锋利的边缘,它看起来不正确。

    imagecolortransparent($dstImage, $background);
    imagealphablending($dstImage, false);
    $colorTransparent = imagecolorallocatealpha($dstImage, 0, 0, 0, 127);
    imagefill($dstImage, 0, 0, $colorTransparent);
    imagesavealpha($dstImage, true);
    imagepng($dstImage, $toWhere);

Ideas?

想法?

Hello,

你好,

I have two issues with my class, basically the quality of the jpeg images is quite poor, but I'm not sure if thats down to my ratio resizing. Ideally I'd like this class to be strict with image sizes and crop into them, but I cant get my head around it.

我的班级有两个问题,基本上jpeg图像的质量很差,但我不确定这是否会降低我的比例。理想情况下,我希望这个课程严格按照图像尺寸进行修剪,但我无法理解它。

My main issue is that pngs always have a black bg, does anyone have experience with this happening?

我的主要问题是pngs总是有黑色的bg,有没有人有过这种情况的经验?

<?php


    class OpenSource_ImageResize {


        function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight) {

            if ($mime == NULL) {
                $mime = getimagesize($theFile);
                $mime = $mime['mime'];
            }


            if ($mime == 'image/jpeg') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefromjpeg($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    throw new exception('Could not create jpeg');
                    return false;
                }
            } else if ($mime == 'image/png') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefrompng($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    //throw new exception('Could not create png');
                    return false;
                }
            } else if ($mime == 'image/gif') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefromgif ($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    //throw new exception('Could not create gif');
                    return false;
                }
            } else {
                throw new exception('Not a valid mime type');
                return false;
            }

                $oldX = imageSX($sourceImage); 
                $oldY = imageSY($sourceImage); 

                if ($newWidth == NULL) {
                    $thumbHeight = $newHeight;
                    $thumbWidth = round($newHeight/($oldY/$oldX));
                } else 

                if ($oldX > $oldY) {
                    $thumbWidth = $newWidth; 
                    $thumbHeight = $oldY * ($newHeight/$oldX);
                } 

                if ($oldX < $oldY) {
                    $thumbWidth = round($newHeight/($oldY/$oldX));
                    $thumbHeight = $newHeight;
                }

                if ($oldX == $oldY) {
                    $thumbWidth = $newWidth; 
                    $thumbHeight = $newHeight;
                }

                    if (!gd_info()) {
                        $dstImage = ImageCreate($thumbWidth, $thumbHeight); 
                        imagecopyresized($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
                    } else {
                        $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight); 
                        imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
                    } 

                    if ($mime == 'image/png') {
                        $xparent = imagecolorresolvealpha($dstImage, 255,2,240, 0) ;
                        imagecolortransparent($dstImage,$xparent);
                        imagealphablending($dstImage,true);
                        imagepng($dstImage, $toWhere);

                    } else if ($mime == 'image/jpeg') { 
                        imagejpeg($dstImage, $toWhere);

                    } else if ($mime == 'image/gif') {
                        imagegif ($dstImage, $toWhere);

                    }

                    imagedestroy($dstImage); 
                    imagedestroy($sourceImage);
                    return true;
        }

}

3 个解决方案

#1


2  

Regarding JPEG image quality you need to make use of the third argument in imagejpeg():

关于JPEG图像质量,您需要使用imagejpeg()中的第三个参数:

imagejpeg($dstImage, $toWhere, 90); // any value above 85 should be fine

Regarding PNG transparency, you're doing it wrong. Your script is horrible and has fundamental problems, I'm gonna leave you with a revised one that fixes both of your problems. It can still be further optimized but I choose to leave some of your original less important mistakes so you don't feel lost:

关于PNG透明度,你做错了。你的剧本很糟糕,并且有根本问题,我会给你留下修改后的问题,解决你的两个问题。它仍然可以进一步优化,但我选择留下一些原始不那么重要的错误,这样你就不会感到迷茫:

class OpenSource_ImageResize
{
    // $extension is not used?
    function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight)
    {
        $sourceImage = ImageCreateFromString(file_get_contents($theFile));

        if (is_resource($sourceImage))
        {
            $info = getimagesize($theFile);

            if (is_null($mime))
            {
                $mime = $info['mime'];
            }

            if ($info[0] <= $newWidth && $info[1] <= $newHeight)
            {
                imagedestroy($sourceImage);
                return copy($theFile, $toWhere);
            }

            if (is_null($newWidth))
            {
                $thumbHeight = $newHeight;
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
            }

            else if ($info[0] > $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $info[1] * ($newHeight/$info[0]);
            }

            if ($info[0] < $info[1])
            {
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
                $thumbHeight = $newHeight;
            }

            if ($info[0] == $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $newHeight;
            }

            $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);

            /* fix PNG transparency issues */           
            ImageFill($dstImage, 0, 0, IMG_COLOR_TRANSPARENT);
            ImageSaveAlpha($dstImage, true);
            ImageAlphaBlending($dstImage, true);

            imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]);

            switch ($mime)
            {
                case 'image/png':
                    imagepng($dstImage, $toWhere, 9); // compress it (level 1 to 9)
                break;

                case 'image/jpeg':
                    imagejpeg($dstImage, $toWhere, 90); // quality = 90 (1 to 100, default is "about" 75)
                break;

                case 'image/gif':
                    imagegif($dstImage, $toWhere);
                break;
            }

            imagedestroy($dstImage);
            imagedestroy($sourceImage);

            return true;
        }
    }
}

I'm sorry for not explicitly pointing out your mistakes but they are so many and it's 3 AM here, need to get some sleep - study it and read the manual, if you have any doubts let me know.

我很抱歉没有明确指出你的错误,但它们太多了,现在凌晨3点,需要睡一觉 - 研究它并阅读手册,如果你有任何疑惑让我知道。

#2


-1  

Musty define in your class png background color.You can also change the background color of jpg and other files after defining the background color.

Musty在您的班级png背景颜色中定义。您还可以在定义背景颜色后更改jpg和其他文件的背景颜色。

#3


-1  

this link will take you to a simple function that will either crop-to-fit, or letter box the image during resize based on the function's arguments. it also has a pretty thorough explaination as to what the function is doing.

此链接将带您进入一个简单的函数,该函数将根据函数的参数在裁剪期间裁剪或拟合图像。它还对该功能的作用进行了非常详尽的解释。

http://www.spotlesswebdesign.com/blog.php?id=1

http://www.spotlesswebdesign.com/blog.php?id=1

Edit: fixed link.

编辑:固定链接。

#1


2  

Regarding JPEG image quality you need to make use of the third argument in imagejpeg():

关于JPEG图像质量,您需要使用imagejpeg()中的第三个参数:

imagejpeg($dstImage, $toWhere, 90); // any value above 85 should be fine

Regarding PNG transparency, you're doing it wrong. Your script is horrible and has fundamental problems, I'm gonna leave you with a revised one that fixes both of your problems. It can still be further optimized but I choose to leave some of your original less important mistakes so you don't feel lost:

关于PNG透明度,你做错了。你的剧本很糟糕,并且有根本问题,我会给你留下修改后的问题,解决你的两个问题。它仍然可以进一步优化,但我选择留下一些原始不那么重要的错误,这样你就不会感到迷茫:

class OpenSource_ImageResize
{
    // $extension is not used?
    function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight)
    {
        $sourceImage = ImageCreateFromString(file_get_contents($theFile));

        if (is_resource($sourceImage))
        {
            $info = getimagesize($theFile);

            if (is_null($mime))
            {
                $mime = $info['mime'];
            }

            if ($info[0] <= $newWidth && $info[1] <= $newHeight)
            {
                imagedestroy($sourceImage);
                return copy($theFile, $toWhere);
            }

            if (is_null($newWidth))
            {
                $thumbHeight = $newHeight;
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
            }

            else if ($info[0] > $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $info[1] * ($newHeight/$info[0]);
            }

            if ($info[0] < $info[1])
            {
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
                $thumbHeight = $newHeight;
            }

            if ($info[0] == $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $newHeight;
            }

            $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);

            /* fix PNG transparency issues */           
            ImageFill($dstImage, 0, 0, IMG_COLOR_TRANSPARENT);
            ImageSaveAlpha($dstImage, true);
            ImageAlphaBlending($dstImage, true);

            imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]);

            switch ($mime)
            {
                case 'image/png':
                    imagepng($dstImage, $toWhere, 9); // compress it (level 1 to 9)
                break;

                case 'image/jpeg':
                    imagejpeg($dstImage, $toWhere, 90); // quality = 90 (1 to 100, default is "about" 75)
                break;

                case 'image/gif':
                    imagegif($dstImage, $toWhere);
                break;
            }

            imagedestroy($dstImage);
            imagedestroy($sourceImage);

            return true;
        }
    }
}

I'm sorry for not explicitly pointing out your mistakes but they are so many and it's 3 AM here, need to get some sleep - study it and read the manual, if you have any doubts let me know.

我很抱歉没有明确指出你的错误,但它们太多了,现在凌晨3点,需要睡一觉 - 研究它并阅读手册,如果你有任何疑惑让我知道。

#2


-1  

Musty define in your class png background color.You can also change the background color of jpg and other files after defining the background color.

Musty在您的班级png背景颜色中定义。您还可以在定义背景颜色后更改jpg和其他文件的背景颜色。

#3


-1  

this link will take you to a simple function that will either crop-to-fit, or letter box the image during resize based on the function's arguments. it also has a pretty thorough explaination as to what the function is doing.

此链接将带您进入一个简单的函数,该函数将根据函数的参数在裁剪期间裁剪或拟合图像。它还对该功能的作用进行了非常详尽的解释。

http://www.spotlesswebdesign.com/blog.php?id=1

http://www.spotlesswebdesign.com/blog.php?id=1

Edit: fixed link.

编辑:固定链接。