Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP ImagickDraw与概述的文本问题_Php_Imagemagick_Image Manipulation_Imagick - Fatal编程技术网

PHP ImagickDraw与概述的文本问题

PHP ImagickDraw与概述的文本问题,php,imagemagick,image-manipulation,imagick,Php,Imagemagick,Image Manipulation,Imagick,我在学习和练习我的想象技能 我对使用Imagick笔划勾勒出的文本有问题。我想在这张图片上看到一个效果:一个流行的互联网迷因: 以下是我目前掌握的代码: $draw = new \ImagickDraw(); $outputImage = new \Imagick('meme.jpg'); $draw->setFillColor('#fff'); $draw->setFont('impact.ttf'); $draw->setFontSize(40); $draw->

我在学习和练习我的想象技能

我对使用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笔划的问题。对于大字体,它看起来不错,但是对于小字体,笔划和字体有一些问题。

你能发布你的结果吗,或者更具体地说,你觉得什么不好

一个解决方案是首先在transaprent背景上创建文本(带有笔划),然后在图像上合成文本。您可以创建更大字体的文本并调整大小,使其在最终图像上看起来更平滑。

版本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();
$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();
第2版:复合覆盖和调整大小

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

+1.将PHP生成的图像放大(比如600%),然后将其大小调整到正确的尺寸,这是一种行之有效的解决像素边缘和抗锯齿问题的方法。@Lukas Nagel我更新了我的问题并提供了一个示例。@Daniel重新缩放图像并调整其大小并不能解决我的问题。我按照建议将图像放大600%,然后调整图像大小以校正尺寸。提示?使用较大的字体大小,然后缩小。还可以尝试用黑色字体创建轮廓,然后在轮廓上合成白色字体。你用什么字体,让我自己试试?愚蠢的问题,影响。我会试一试,让你知道。太棒了。非常感谢您抽出时间。现在我了解了这些内容,我知道我在尝试重新调整大小时做错了什么。)