【发布时间】:2015-02-14 20:55:56
【问题描述】:
我正在使用 Imagick 生成文本图像(想想网站横幅 - 它们看起来像那样)。我在此函数中的服务器上的资源不足,特别是 annotate 行。
public function output_image($type = 'png') {
$this->set_draw($this->font_size);
$this->image->newImage($this->width*1.3,
$this->line_height*2.5,
'transparent'); // make an image that's too big
$this->image->annotateImage($this->draw,
$this->font_size*0.5, //x offset for cursive fonts
$this->font_size, // vertical offset for tall ascenders
0, //angle
$this->text); // add the text
$this->image->trimImage(0); // trim it.
$this->image->setImageFormat($type);
$this->base64 = base64_encode($this->image);
echo "<img src='data:image/$type;base64,{$this->base64}'/>";
}
在我的本地环境(win 8.1、xampp、4gb ram)上,速度很快。
在服务器上(godaddy linux,1 GB 内存),它最大限度地利用了资源,并且比在我的本地环境中花费的时间长 10 倍(句子长度最多 4.5 秒)。
我已经对类中的每个函数进行了计时,发现时间都花在了annotateImage()这一行。
我可以用硬件来解决这个问题,但我想知道是否有更好的方法在图像上写入文本?或者加快注释工作的方法(降低图像质量等)?
【问题讨论】:
-
我对其他软件解决方案(shell_exec、gd 等)持开放态度。图片宽度在 500-1000 像素之间。