裁剪cirlcle图像并与标记与透明bg合并

时间:2015-09-23 08:00:46

标签: php png gd alpha-transparency php-gd

我试图裁剪正方形图像并将其与标记合并,但我无法将拼写的圆形图像透明。

当将圆形图像保存为PNG时,它会显示透明边角,但是当我在Photoshop中打开它时,角落上会出现白色,如下图所示。

This i s the original picture im using

I get this one ,but i want it without the white corners

这是使用的代码:

//SAVED THE CIRCLE PNG IMAGE
            $width = 320;
            $height = 320;
            $img1 = '';
            switch($fileExt){
                case '.png':
                    $img1 = ImageCreateFrompng($img= $image_config['new_image']);
                break;
                case '.jpg':
                    $img1 = ImageCreateFromjpeg($img= $image_config['new_image']);
                break;
                case '.gif':
                    $img1 = ImageCreateFromgif($img= $image_config['new_image']);
                break;
            }
            $x=$width ;
            $y=$height;
            $img2 = imagecreatetruecolor($x, $y);
            $bg = imagecolorallocate($img2, 255, 255, 255); 
            imagefill($img2, 0, 0, $bg);
            $e = imagecolorallocate($img2, 0, 0, 0);
            $r = $x <= $y ? $x : $y;
            imagefilledellipse ($img2, ($x/2), ($y/2), $r, $r, $e); 
            imagecolortransparent($img2, $e);
            imagecopymerge($img1, $img2, 0, 0, 0, 0, $x, $y, 100);
            imagecolortransparent($img1, $bg);
            header("Content-type: image/png"); 
            imagepng($img1, './img/deviceImg/pin'.$datetime.'.png');
            imagedestroy($img2); // kill mask first
            imagedestroy($img1); // kill canvas last

            //MERGING IT WITH THE PIN
            $width = 320;
            $height = 320;
            $image_1 = imagecreatefrompng('./img/deviceImg/pin.png');
            imagesavealpha($image_1, true);
            imagealphablending($image_1, true);
            $image_2 = imagecreatefrompng('./img/deviceImg/pin'.$datetime.'.png');
            imagesavealpha($image_2, true);
            imagealphablending($image_2, true);

            imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);
            imagepng($image_1, './img/deviceImg/pinASD'.$datetime.'.png');

1 个答案:

答案 0 :(得分:0)

透明度仅使用imagecopymerge()复制,而不是imagecopy()。所以你的倒数第二行:

imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);

应改为:

imagecopymerge($image_1, $image_2, 40, 22, 0, 0, $width, $height, 100);

注意最后的额外参数(pct)。根据手册:

  

两个图像将根据pct合并,范围从0到100.当pct = 0时,不执行任何操作,当100此函数与pallete图像的imagecopy()行为相同时,除了忽略alpha分量,而它为真彩色图像实现了alpha透明度。