imagick 加水印
In a previous article, Timothy Boronczyk wrote about how to
create watermarks with Imagick
using an overlay image. In this article, I’ll show you how to achieve a similar effect using plain text.
在上一篇文章中,Timothy Boronczyk撰写了有关如何
使用Imagick
使用叠加图像
创建水印的信息
。 在本文中,我将向您展示如何使用纯文本实现类似的效果。
At the time of this writing, virtually no documentation exists on PHP’s
Imagick API documentation
, although there are plenty of command-line examples to be found on the
ImageMagick website
, so that is where we shall begin. Converting the command-line code from the examples into PHP is simply a tedious matter of finding the appropriate methods that perform the same functionality.
在撰写
本文时
,尽管在
ImageMagick网站
上可以找到很多命令行示例,但是PHP的
Imagick API文档
上实际上没有任何文档,因此我们将从这里开始。 将示例中的命令行代码转换为PHP只是寻找合适的方法来执行相同功能的繁琐工作。
For my examples I’ll be using the following image of some completely random handsome looking gentleman.
在我的示例中,我将使用下面这张完全随意的英俊绅士形象。
在图像上绘制文字
(
Drawing Text on the Image
)
The easiest type of textual watermark to create is a string overlay on top of the image.
要创建的文本水印最简单的类型是在图像顶部覆盖一个字符串。
The command line example is:
命令行示例是:
convert image.png -font Arial -pointsize 20
-draw "gravity south
result.png
And the PHP equivalent is:
和PHP等效的是:
// Create objects
$image = new Imagick('image.png');
// Watermark text
// Create a new drawing palette
$draw = new ImagickDraw();
// Set font properties
$draw->setFont('Arial');
$draw->setFontSize(20);
$draw->setFillColor('black');
// Position text at the bottom-right of the image
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the image
$image->annotateImage($draw, 10, 12, 0, $text);
// Draw text again slightly offset with a different color
$draw->setFillColor('white');
$image->annotateImage($draw, 11, 11, 0, $text);
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
The result is:
This example is pretty straight-forward and the code’s comments make it so you shouldn’t need much extra explanation. But while this works, the text stands out too drastically from the image and the effect should probably be more subtle.
这个例子很简单,代码的注释也很简单,因此您不需要过多的解释。 但是,尽管这样做有效,但是文本在图像中显得过于突出,效果可能会更加微妙。
使用字体蒙版的透明文本
(
Transparent Text Using a Font Mask
)
For a smoother effect on the watermark text, you can make the text string transparent using a font mask.
为了使水印文本更流畅,可以使用字体蒙版使文本字符串透明。
Command line example:
命令行示例:
convert -size 300x50 xc:grey30 -font Arial -pointsize 20
fgnd.png
convert -size 300x50 xc:black -font Arial -pointsize 20 -gravity center
+matte mask.png
composite -compose CopyOpacity mask.png fgnd.png stamp.png
mogrify -trim +repage stamp.png
composite -gravity south -geometry +0+10 stamp.png image.png
result.png
PHP equivalent:
相当于PHP:
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
$mask = new Imagick();
$draw = new ImagickDraw();
// Define dimensions
$width = $image->getImageWidth();
$height = $image->getImageHeight();
// Create some palettes
$watermark->newImage($width, $height, new ImagickPixel('grey30'));
$mask->newImage($width, $height, new ImagickPixel('black'));
// Watermark text
// Set font properties
$draw->setFont('Arial');
$draw->setFontSize(20);
$draw->setFillColor('grey70');
// Position text at the bottom right of the image
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark palette
$watermark->annotateImage($draw, 10, 12, 0, $text);
// Draw text on the mask palette
$draw->setFillColor('white');
$mask->annotateImage($draw, 11, 13, 0, $text);
$mask->annotateImage($draw, 10, 12, 0, $text);
$draw->setFillColor('black');
$mask->annotateImage($draw, 9, 11, 0, $text);
// This is needed for the mask to work
$mask->setImageMatte(false);
// Apply mask to watermark
$watermark->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
// Overlay watermark on image
$image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, 0, 0);
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
The result is:
Several images are created in this example. The first image
$watermark
is grey-scale, and the second image
$mask
uses pure black for the parts I want to be transparent and white for the parts I want to keep. When I apply the mask by combining the images, any grey shades found in
$mask
as a result of anti-aliasing will be semi-transparent and result in smoother edges.
在此示例中创建了多个图像。 第一个图像
$watermark
是灰度图像,第二个图像
$mask
将纯黑色用于我要透明的部分,将白色用于我要保留的部分。 当我通过组合图像来应用蒙
$mask
,由于抗锯齿而在
$mask
发现的任何灰色阴影将是半透明的,并导致边缘更平滑。
In the command-line version of the code, the outside edges of the transparent image are clipped before the watermark is overlayed, but there
appears to be a bug
that prevents the
compositeImage()
method from preserving the position defined by
setGravity()
. This basically means that if I were to clip the edges then my watermark would lose its place at the bottom right and be re-positioned in the top-left corner. To get around this foolish behavior, I’ve created my palettes with the same dimensions as the source image so no clipping takes place.
在代码的命令行版本中,透明图像的外部边缘在叠加水印之前被裁剪,但是
似乎存在一个错误
,
该错误
阻止了
compositeImage()
方法保留由
setGravity()
定义的位置。 这基本上意味着,如果我要修剪边缘,那么我的水印将丢失其在右下角的位置,并重新定位在左上角。 为了避免这种愚蠢的行为,我以与源图像相同的尺寸创建了调色板,因此不会发生剪切。
平铺文字
(
Tiled Text
)
This final example tiles text over the entire image which makes it far more difficult to remove.
最后一个示例将文本平铺在整个图像上,这使得删除起来更加困难。
Command line:
convert -size 140x80 xc:none -fill grey
miff:- |
composite -tile - image.png result.png
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
// Watermark text
// Create a new drawing palette
$draw = new ImagickDraw();
$watermark->newImage(140, 80, new ImagickPixel('none'));
// Set font properties
$draw->setFont('Arial');
$draw->setFillColor('grey');
$draw->setFillOpacity(.5);
// Position text at the top left of the watermark
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
// Draw text on the watermark
$watermark->annotateImage($draw, 10, 10, 0, $text);
// Position text at the bottom right of the watermark
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark
$watermark->annotateImage($draw, 5, 15, 0, $text);
// Repeatedly overlay watermark on image
for ($w = 0; $w < $image->getImageWidth(); $w += 140) {
for ($h = 0; $h < $image->getImageHeight(); $h += 80) {
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $w, $h);
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
Result:
Notice that I’ve set the transparency with
setFillOpacity()
rather than using an image mask.
请注意,我已经使用
setFillOpacity()
而不是使用图像蒙版设置了透明度。
摘要
(
Summary
)
For me, image manipulation in PHP has become one of the most enjoyable aspects of the language, and I can only hope that Imagick will become bundled in future versions. If you’re looking for a way to contribute, I encourage you to convert other command line examples to PHP and then post your results on the official PHP manual so others can learn and enjoy.
对我而言,PHP中的图像处理已成为该语言中最令人愉悦的方面之一,我只能希望Imagick会捆绑在将来的版本中。 如果您正在寻找一种贡献的方法,建议您将其他命令行示例转换为PHP,然后将结果发布在PHP官方手册上,以便其他人可以学习和享受。
Image via
Fotolia
图片来自
Fotolia
翻译自:
https://www.sitepoint.com/adding-text-watermarks-with-imagick/
imagick 加水印
imagick 加水印In a previous article, Timothy Boronczyk wrote about how to create watermarks with Imagick using an overlay image. In this article, I’ll show you how to achieve a similar effect using plain...
header('Content-Type: text/
html
; charset=utf-8');
$text = '中粮屯河(sh600737)';//中粮屯河(sh600737)
$watermark = '30598810312...
<br />原文来自<br />http://eclecticdjs.com/mike/tutorials/
php
/
image
magic
k/examples_04/setgravity.
php
<br /> <br /> <br />描述:<br />bool I
magic
kDraw::setGravity ( int $gravity )
<br />设置文字
水印
的位置<br /> <br />参数:Gravity参数只能是以下常量:i
magic
k::GRAVITY_NORTHWEST //左下<br /
convert -debug configure -background none label:Kane
a.png
2015-09-23T17:06:02+08:00 0:00.000 0.000u 6.9.1 Configure
convert[13059]: utility.c/Exp
命令行:convert.exe -density 200 -units PixelsPerInch -resize 1040x1471^> -background white -flatten xxx.png xx.jpeg.
设置分辨率:
接口:setResolution($this->rx,$this->ry); rx 和 ry 是宽高的分辨率。
命令行参数:
$
image
->read
Image
("/home/www/wordpress/1212/p1_bg.png");
$watermark = new I
magic
k();
$watermark->read
Image
("/home/www/wordpress/1212/2.pn