PHP ImagickDraw带有轮廓文本问题

我正在学习和练习我的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();

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...