ImageMagick中慢速的字体渲染 [英] Slow font rendering in ImageMagick

查看:93
本文介绍了ImageMagick中慢速的字体渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下ImageMagick脚本(与Imagick for PHP一起使用)生成字体图像.该脚本大约需要0.1秒才能生成大小为48的大约30个字符的图像.目标速度大约为0.01秒.恐怕切换到GD库可能是实现此目的的唯一方法(我在此处阅读表示,GD中的文本生成要快得多.但是,由于没有重力和修剪之类的功能,使用GD生成此类图像会更加麻烦.是否有人在此代码中看到明显的瓶颈,还是该切换库了?

I'm using the following ImageMagick script (with Imagick for PHP) to generate an image of a font. This script takes about 0.1 seconds to generate an image of about 30 characters at size 48. The target speed is about 0.01 seconds. I'm afraid switching to the GD library may be the only way to achieve this (I read here that text generation is much faster in GD). However, without features like gravity and trim, it's much more cumbersome to generate this type of image using GD. Does anyone see an obvious bottleneck in this code, or is it time to switch libraries?

$image = new Imagick();
$draw = new ImagickDraw();
$background = new ImagickPixel('none');
$draw->setFont($font);
$draw->setFontSize($size);
$draw->setFillColor(new ImagickPixel('#'.$color));
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->annotation(0, 0, $text);
$image->newImage(5*mb_strlen($text, 'UTF-8')*$size, 5*$size, $background);
$image->setImageFormat('png');
$image->drawImage($draw);
$image->trimImage(0);
$image->writeImage($path_server['dirname'].'/'.$path_server['basename']);

推荐答案

答案是 来切换库,但不能切换到GD.相反,我切换到了 GraphicsMagick ,它是ImageMagick的一个分支,专注于效率和优化.根据GraphicsMagick网站的说法,Flickr和Etsy等世界上最大的摄影网站都在使用它.以下GraphicsMagick代码的运行速度比相应的ImageMagick代码快大约 10倍,这使我每次操作可以达到0.01秒的目标(实际上接近0.008秒):

The answer was to switch libraries, but not to GD. Rather, I switched to GraphicsMagick, which is a fork of ImageMagick that focuses on efficiency and optimization. According to the GraphicsMagick website, it's used by some of the world's largest photo sites including Flickr and Etsy. The following GraphicsMagick code runs about 10 times faster than the corresponding ImageMagick code, which allowed me to hit my target of 0.01 seconds per operation (actually it's closer to 0.008 seconds):

$image = new Gmagick();
$draw = new GmagickDraw();
$draw->setfont($font);
$draw->setfontsize($size);
$draw->setfillcolor('#'.$color);
$draw->setgravity(Gmagick::GRAVITY_CENTER);
$draw->annotate(0, 0, mb_ereg_replace('%', '%%', $text));
$image->newimage(5*mb_strlen($text)*$size, 5*$size, 'none', 'png');
$image->drawimage($draw);
$image->trimimage(0);
$image->writeimage($path_server['dirname'].'/'.$path_server['basename']);

您会注意到,还有其他一些不错的功能.例如,大多数函数不必通过创建ImagickPixel对象来定义颜色,而是只需将颜色作为字符串即可.此外,在GraphicsMagick中,函数名称似乎更加自洽(使用 annotate 代替 annotation ).不用说,我对此很满意.

You'll notice that there are a few other nice features as well. For example, instead of having to define a color by creating an ImagickPixel object, most functions simply take a color as a string. Also, the function names seem more self-consistent in GraphicsMagick (annotate instead of annotation). Needless to say, I'm pretty happy with it.

这篇关于ImageMagick中慢速的字体渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆