简体   繁体   English

PHP GD:模糊获得透明图像

[英]PHP GD: Blur for transparent image

I am trying to create the silhouette of PNG image with transparencies. 我正在尝试用透明胶片创建PNG图像的轮廓。 Here is my code 这是我的代码


//file that i am working with
$file='http://www.google.com/mapfiles/turkey.png';
$im  =imagecreatefrompng($file);
imagealphablending($im, false);
imagesavealpha($im, true);
$imw      =imagesx($im);
$imh      =imagesy($im);
//turkey body color
$bodycolor=imagecolorallocatealpha($im, 144, 144, 144, 50);
//imageholder
$imnew    =imagecreatetruecolor($imh, $imh);
imagealphablending($imnew, false);
imagesavealpha($imnew, true);
$transparent_color=imagecolorallocatealpha($imnew, 0, 0, 0, 127);
imagecolortransparent($imnew, $transparent_color);
imagefilledrectangle($imnew, 0, 0, $imh, $imh, $transparent_color);
for ($i=0; $i > 24;
        //all not transparent pixels are copied to imageholder
        if ($alpha != 127)
            {
            imagesetpixel($imnew, $i, $j, $bodycolor);
            }
        }
    }
//blur filter
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
header ("Content-type: image/png");
imagepng ($imnew);
imagedestroy ($imnew);
?>

So, I need to get the silhouette ot the turkey with blurred edges. 因此,我需要获得带有模糊边缘的火鸡的轮廓。 I need edges to be blurred "outside", so the farther the pixel was from edge, the more transparent it was with the same rbg. 我需要边缘在“外部”模糊,因此像素离边缘越远,相同的rbg越透明。 And imagefilter also lifts the turkey a bit :) 而且imagefilter也可以稍微抬高火鸡:)

For more complex image manipulations like these, I would highly recommend you use ImageMagick: http://www.imagemagick.org 对于此类更复杂的图像处理,我强烈建议您使用ImageMagick: http : //www.imagemagick.org

It's pretty easy to install unto your server, very fast and has great documentation. 它非常容易安装到您的服务器,速度非常快,并且文档丰富。 They have a whole section for what you're talking about: http://www.imagemagick.org/Usage/channels/ 他们整节介绍了您正在谈论的内容: http : //www.imagemagick.org/Usage/channels/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM