叠加时PNG质量丢失

时间:2015-09-02 02:34:33

标签: php image png

我覆盖了两个图像,包括png或1 png和1 jpg,png位于顶部。但是,与原始PNG相比,覆盖的PNG的质量非常糟糕。 enter image description here

这是我从控制台运行的代码。我应该改变哪些与png相关的陈述?

switch (mime_content_type($imagePath)) {
        case 'video/mp4':
            break;
        case 'image/png':
            // add cardos
            $stamp =(dirname(__FILE__). "/Cardos.png");

            $imgOverlay = imagecreatefrompng($imagePath);
            $imgAvatar = imagecreatefrompng($stamp);

            $width = imagesx($imgOverlay);
            $height = imagesy($imgOverlay);

            $imgBanner = imagecreatetruecolor($width, $height);
            imagecopyresampled($imgBanner, $imgOverlay, 0, 0, 0, 0, $width, $height, $width, $height);
            imagecopyresampled($imgBanner, $imgAvatar, 0, 0, 0, 0, $width, $height, imagesx($imgAvatar), imagesy($imgAvatar));


            header('Content-type: image/jpg');
            imagepng($imgBanner,$imagePath);
            imagedestroy($imgBanner);
            break;
        case 'image/jpeg':
            // add cardos
            $stamp =(dirname(__FILE__). "/Cardos.png");

            $imgOverlay = imagecreatefromjpeg($imagePath);
            $imgAvatar = imagecreatefrompng($stamp);

            $width = imagesx($imgOverlay);
            $height = imagesy($imgOverlay);

            $imgBanner = imagecreatetruecolor($width, $height);
            imagecopyresampled($imgBanner, $imgOverlay, 0, 0, 0, 0, $width, $height, $width, $height);
            imagecopyresampled($imgBanner, $imgAvatar, 0, 0, 0, 0, $width, $height, imagesx($imgAvatar), imagesy($imgAvatar));


            header('Content-type: image/jpg');
            imagejpeg($imgBanner, $imagePath);
            imagedestroy($imgBanner);

            break;
    }

1 个答案:

答案 0 :(得分:0)

imagejpeg()imagepng()具有第三个质量属性。然而,它们都有不同的质量。这是一个你可以尝试做你需要做的课程。请注意,叠加图像的选择基于__DIR__(类文件的位置,您可能需要根据需要进行更改):

以下是课程:

<?php
    class   AvatarFactory
        {
            protected   $imagepath;
            protected   $stamp;
            protected   $mime_type;

            public  function __construct($filepath = "/Cardos.png")
                {
                    // This link is important to note...you can alter
                    // based on where files are placed in your directories
                    $this->stamp    =   __DIR__.$filepath;
                }

            public  function Initialize($imagepath = false)
                {
                    $this->imagepath    =   $imagepath;
                    // This function sets the mime type
                    $this->mime_type    =   $this->CheckFileType();
                    return $this;
                }

            protected   function CheckFileType()
                {
                    if(empty($this->imagepath) || !is_file($this->imagepath))
                        return false;

                    return mime_content_type($this->imagepath);
                }

            public  function BuildImage($settings = false)
                {
                    // These values can be changed to auto-size the final image
                    $to_width   =   (!empty($settings['width']) && is_numeric($settings['width']))? $settings['width'] : false;
                    $to_height  =   (!empty($settings['height']) && is_numeric($settings['height']))? $settings['height'] : false;
                    $quality    =   (!empty($settings['quality']) && is_numeric($settings['quality']))? $settings['quality'] : 80;

                    switch($this->mime_type) {
                            case ('image/png'):
                                 $imgOverlay    =   imagecreatefrompng($this->imagepath);
                                 $imgType       =   'png';
                                 break;
                            case ('image/jpeg'):
                                 $imgOverlay    =   imagecreatefromjpeg($this->imagepath);
                                 $imgType       =   'jpg';
                                 break;
                            default:
                                $imgOverlay =   false;
                        }

                    if(!$imgOverlay)
                        return false;

                    $imgAvatar  =   imagecreatefrompng($this->stamp);
                    $width      =   imagesx($imgOverlay);
                    $height     =   imagesy($imgOverlay);
                    $to_width   =   (!empty($to_width))? $to_width:$width;
                    $to_height  =   (!empty($to_height))? $to_height:$height;
                    $imgBanner  =   imagecreatetruecolor($to_width, $to_height);
                    imagecopyresampled($imgBanner, $imgOverlay, 0, 0, 0, 0, $to_width, $to_height, $width, $height);
                    imagecopyresampled($imgBanner, $imgAvatar, 0, 0, 0, 0, $to_width, $to_height, imagesx($imgAvatar), imagesy($imgAvatar));
                    header('Content-type: '.$this->mime_type);
                    ($imgType =='png')? imagepng($imgBanner,$this->imagepath,(10-($quality/10))) : imagejpeg($imgBanner,$this->imagepath,$quality);
                    imagedestroy($imgBanner);
                }
        }
?>

使用:

<?php
    // Make sure the class file is on this page somehow
    // (by inclusion or pasted above this)
    // You can feed a different overlay image in here
    $engine =   new AvatarFactory();
    // You can feed in quantity (0 being worst, 100 being best)
    // You can feed a new height and width if desired like so:
    // ->BuildImage(array("quality"=>65,"height"=>400,"width"=>400));
    // The default is just to do the same as you have in your script
    $engine ->Initialize(__DIR__.'/background_img.png')
            ->BuildImage(array("quality"=>65));
?>