问题描述
我正在学习和练习Imagick技能.
I'm learning and practicing my Imagick skills.
我使用Imagick笔划出现轮廓文字问题.我想实现在此图像上可见的效果:一个流行的互联网模因:
I have issues with outlined text using Imagick stroke. I would like to achieve an effect visible on this image: a popular Internet meme:
这是我到目前为止的代码:
Here's the code I have so far:
$draw = new \ImagickDraw();
$outputImage = new \Imagick('meme.jpg');
$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(40);
$draw->setGravity(\Imagick::GRAVITY_NORTH);
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(1);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');
$outputImage->setFormat('png');
$outputImage->writeimage('tmp/meme.png');
问题:文字笔画看起来不太好.我在Imagick讨论板上找到了有关第二次注释图像的提示,但没有中风.不起作用.
The issue: text stroke does not look nice. I've found a tip on Imagick discussion board about annotating image second time, but without stroke. Does not work.
写图像之前:
$draw->setStrokeColor('transparent');
$outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');
有人可以给我一个线索吗?
Can anybody give me a clue?
更新
总而言之,我生成的图像如下:
Update
To conclude, my generated image looks as following:
如您所见,使用不同的字体大小时,2px笔触存在一些问题.在大字体上,它看起来不错,但是在较小的字体下,笔划和字体存在一些问题.
As you can see, I have some issues with 2px stroke while using different font size. On big fonts, it looks nice but with smaller font there are some issues with the stroke and font.
推荐答案
版本1:调整大小
版本2:合成并调整大小
Version 2: composite over and resizing
第2版给出了更好的结果.请参阅下面的代码.根据最终大小,您需要调整字体和笔触的大小,因为调整大小可能会产生不想要的效果.您也可以尝试使用版本2,而无需调整大小.
Version 2 gives a much better result. See code below. Depending on the final size, you need to play around with font and stroke size, as the resizing may give unwanted effects. You may also try version 2 without resizing.
版本1:调整大小
$draw = new ImagickDraw();
$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(4);
$draw->setStrokeAntialias(true); //try with and without
$draw->setTextAntialias(true); //try with and without
$outputImage = new Imagick();
$outputImage->newImage(1400,400, "transparent"); //transparent canvas
$outputImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');
$outputImage->trimImage(0); //Cut off transparent border
$outputImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$outputImage->clear();
$outputImage->destroy();
版本2:合成并调整大小
$draw = new ImagickDraw();
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeAntialias(true); //try with and without
$draw->setTextAntialias(true); //try with and without
//Create text
$draw->setFillColor('#fff');
$textOnly = new Imagick();
$textOnly->newImage(1400,400, "transparent"); /transparent canvas
$textOnly->annotateImage($draw, 21, 101, 0, 'STOP ME FROM MEMEING'); //parameters depend of stroke and text size
//Create stroke
$draw->setFillColor('#000'); //same as stroke color
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(8);
$strokeImage = new Imagick();
$strokeImage->newImage(1400,400, "transparent");
$strokeImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');
//Composite text over stroke
$strokeImage->compositeImage($textOnly, imagick::COMPOSITE_OVER, 0, 0, Imagick::CHANNEL_ALPHA );
$strokeImage->trimImage(0); //cut transparent border
$strokeImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$strokeImage->clear();
$strokeImage->destroy();
$textOnly->clear();
$textOnly->destroy();
这篇关于PHP ImagickDraw带有概述文本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!