首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将imagettftext GD修改为Imagick

将imagettftext GD修改为Imagick
EN

Stack Overflow用户
提问于 2018-09-13 00:19:20
回答 2查看 280关注 0票数 0

我们有一个向图像添加文本的文件。

我们需要将代码从GD转换为Imagick,因为我们需要添加imagettftext无法添加的样式和其他功能。

任何人可以通过Imagick而不是使用imagettftext来转换以下代码以添加文本吗?

代码语言:javascript
复制
$txt = imagettfbbox($fontsize ,0,$font,$str_l1) ;
$x_img = abs($txt[2] - $txt[0]) + 15;
$y_img = abs($txt[7] - $txt[1]) ;

$im_img = imagecreate($x_img, $y_img + 4);
$bg = imagecolorallocate($im_img, 229,229,229); 
imagecolortransparent($im_img, $bg); 
imagettftext($im_img, $fontsize, 0, abs($txt[0]) , abs($txt[5]) , $textcolor, $font, $str_l1);
EN

回答 2

Stack Overflow用户

发布于 2018-09-13 01:07:24

我无法将您的命令翻译成Imagick,但我可以向您展示一个简单的示例,该示例使用Imagemagick中的-annotate向图像添加文本的大多数功能。还有其他方法可以向图像中添加文本。

请参见:

https://www.imagemagick.org/Usage/text https://www.imagemagick.org/script/command-line-options.php#annotate https://www.imagemagick.org/script/command-line-options.php#gravity https://www.imagemagick.org/script/command-line-options.php#geometry

有关Imagick的等价物,请参阅http://us3.php.net/manual/en/book.imagick.php

http://us3.php.net/manual/en/imagick.annotateimage.php http://us3.php.net/manual/en/imagickdraw.setfont.php http://us3.php.net/manual/en/imagickdraw.setfontfamily.php http://us3.php.net/manual/en/imagickdraw.setfontsize.php http://us3.php.net/manual/en/imagickdraw.setfontstretch.php http://us3.php.net/manual/en/imagickdraw.setfontstyle.php http://us3.php.net/manual/en/imagickdraw.setfontweight.php http://us3.php.net/manual/en/imagickdraw.setgravity.php

输入:

代码语言:javascript
复制
convert lena.jpg -font ubuntu -fill skyblue -stroke blue -strokewidth 1 -undercolor white -pointsize 36 -gravity south -annotate 0x10+0+20 "TESTING" lena_annotate.jpg
代码语言:javascript
复制
-font is the font name (your can use the path to the font.suffix as well)
-fill is the color of the font
-stroke is the outline color
-strokewidth is the thickness of the stroke outline
-undercolor is the color to put under the text -- you can leave that off for no underfloor.
-gravity is where the text will be placed in the image (south means at the center of the bottom)
-geometry is the offset from the gravity position
-annotate 0x10+0+20 --- 0x10 means to slant only in y by 10 (if 10x10 it will rotate rather than slant); +0+20 means raise by 20 upwards and shift left/right by 0. If you do not want any slanting, then use 0x0. If you do not want it shifted up, then use +0+0.
"TESTING" is the text you want to use.

票数 1
EN

Stack Overflow用户

发布于 2018-09-13 21:43:22

任何人可以通过Imagick而不是使用imagettftext来转换以下代码以添加文本吗?

不怎么有意思。当您遇到困难时,我们可以帮助您解决问题,提供示例,和/或为您提供正确的文档。您仍然负责移植代码以满足您的业务需求。

正如@fmw42正确指出的那样,你的任务有很好的文档记录和大量的例子。使用imagick,您可以创建图形上下文来管理样式,然后将其注释到图像。

代码语言:javascript
复制
/**
 * Create a drawing context to control style.
 */
$text = new ImagickDraw();
$text->setFont('Chalkduster');
$text->setFontSize(32);
$text->setFillColor("ORANGE");
$text->setGravity(Imagick::GRAVITY_SOUTH);
/**
 * Apply to image.
 */
$image = new Imagick("wizard:");
$image->annotateImage($text, 0, 0, 0, "Hello World!");
/**
 * Save to disk.
 */
$image->writeImage('output.png');

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52299609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档