使用imagick PHP绘制透明图像的轮廓 [英] Outline a transparent image using imagick PHP

查看:265
本文介绍了使用imagick PHP绘制透明图像的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张透明背景的图像,我想用5px的边框勾勒轮廓.在photoshop中,我可以抚摸一下以实现此目的.

I have an image with a transparent background that I'd like to outline with a 5px border. In photoshop, I can stroke it to achieve this.

我尝试使用borderImage,但不会勾勒出企鹅的轮廓.

I've tried using borderImage but it won't outline the penguin.

$image = new Imagick();
$image->readImage('tux.png');

$image->borderImage(new ImagickPixel('red'), 5, 5); // no outline

这是图片.

这是我想要实现的.

推荐答案

我将从命令行开始,以后再做PHP,或者让您开始工作……

I'll start at the command line and may do the PHP later, or let you work that bit out...

第1步-提取透明度

如您所见,-border概述了整个图像,但实际上您只想概述不透明区域,因此您需要使用透明度,或者alpha层.让我们首先提取它:

As you have seen, -border outlines the whole image, but you actually want to outline the opaque areas only so you need to work with the transparency, or alpha layer. Let's extract that first:

convert tux.png -alpha extract alpha.png

第2步-获取不透明区域的边缘

现在,您想要轮廓的边缘,所以我将使用-morphology:

Now, you want the edges of that outlined, so I'll use -morphology:

convert alpha.png -morphology edge octagon -threshold 50% edge.png

我听说人们在使用PHP进行形态学操作时遇到困难,因此这是此步骤的另一种不使用形态学的方法.基本上,它会复制alpha层,然后使用统计信息在每个3x3框中找到最亮的像素(这只会对3x3框中有一些黑白像素的边缘产生影响),然后进行区别结果与原始图像一起显示受影响的像素.比描述更容易做到!

I have heard that folks are having difficulty with morphological operations in PHP, so here is an alternative method for this step that doesn't use morphology. Basically, it duplicates the alpha layer, and then uses statistics to find the brightest pixel in each 3x3 box (which is only going to have an effect at the edges where there are some black and some white pixels in the 3x3 box) and then differences the result with the original to show the affected pixels. Easier to just do than describe!

convert alpha.png \( +clone -statistic maximum 3x3 -threshold 50% \) -compose difference -composite edge.png

将5x5框用于较粗的线.

Use a 5x5 box for a fatter line.

我看到有一个-edge 5选项更容易-我们生活和学习!

I see there is an -edge 5 option which is way easier - we live and learn!

第3步-将边缘设为红色并保持透明

现在,您希望白色变成红色,而黑色变成透明:

Now you want the white to be red and the black to be transparent:

convert edge.png -fill red -opaque white -transparent black rededge.png

第4步-在原始图片上合成红色轮廓

最后,您希望将其与原始图像进行合成:

And finally, you want to composite that over your original:

convert tux.png rededge.png -composite result.png

整个猪

或者,您可以一次完成所有操作:

Or, you can do it all in one go like this:

convert tux.png  \( +clone -alpha extract -morphology edge octagon -threshold 50% -fill red -opaque white -transparent black \) -composite result.png

相对于-morphology edge,您可能更喜欢-morphology edgeout的效果.

You may prefer the more subtle effect of -morphology edgeout over -morphology edge.

PHP版本

我的PHP技能低" ,但是我已经开始并且正在取得一些进展-稍后会更新,但是到目前为止看起来像这样:

My PHP skills are "low", but I have started and am making some progress - will update later, but so far it looks like this:

   $image = new Imagick("tux.png");
   $alpha = clone $image;
   $alpha->separateImageChannel(Imagick::CHANNEL_ALPHA);
   $alpha->negateImage(true);
   $alpha->edgeImage(5);
   $alpha->opaquePaintImage("white","red",65000,FALSE);
   $alpha->transparentPaintImage("black",0.0,0,FALSE);
   $image->compositeImage($alpha,Imagick::COMPOSITE_DEFAULT,0,0);
   $image->writeImage("result.png");

这似乎很有效,但是可能可以解决某些方面的问题-特别是65000魔术数字,也许还有一些不必要的克隆和内容-我会留给您!

That seems to pretty much work, but some aspects could probably be tidied up - specifically the 65000 magic number and maybe some unnecessary cloning and stuff -I'll leave that to you!

这篇关于使用imagick PHP绘制透明图像的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆