【问题标题】:Optimizing Imagick annotateImage优化 Imagick annotateImage
【发布时间】: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 像素之间。

标签: php imagick


【解决方案1】:

看来在(linux)服务器上,shell_exec确实比php扩展快很多;提供比窗户更好的时间(甚至低于 0.1 秒)。另一方面,Windows 对shell_exec 的性能影响很小。

我假设这是因为 godaddy 上的版本 - 截至今天,它是 6.5.4-7,它是 2009 版本。我在我的 Windows 盒子上运行 6.7.7-4。如果他们运行的是 5 年前的程序版本,那么 php 扩展可能从那时起经历了重大改进。

【讨论】:

    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2013-07-06
    • 2018-05-28
    • 2021-02-24
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多