编程语言
首页 > 编程语言> > PHP ImagickDraw带有轮廓文本问题

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();

标签:imagick,php,imagemagick,image-manipulation
来源: https://codeday.me/bug/20190901/1783434.html