我正在学习和练习我的Imagick技能.
我使用Imagick笔划有概述文本的问题.我想在这张图片上看到一个效果:一个受欢迎的互联网模因:
这是我到目前为止的代码:
$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讨论板上找到了关于第二次注释图像的提示,但没有中风.不行.
在写图像之前:
$draw->setstrokeColor('transparent');
$outputimage->annotateImage($draw, 0, 5, 0, 'Sample text');
谁能给我一个线索?
更新
最后,我生成的图像如下所示:
如您所见,我在使用不同字体大小时遇到2px笔划的问题.在大字体上,它看起来不错,但字体较小,笔画和字体有一些问题.
解决方法:
版本1:调整大小
版本2:复合和调整大小
版本2提供了更好的结果.见下面的代码.根据最终尺寸,您需要使用字体和笔触大小,因为调整大小可能会产生不必要的效果.您也可以尝试版本2而不调整大小.
版本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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。