Optimizing Imagick annotateImage

2019-09-06 00:28发布

问题:

I'm generating images of text using Imagick (think site banners - they look like that). I'm running out of resources on the server in this function, specifically the annotate line.

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}'/>"; 
    }

On my local environment (win 8.1, xampp, 4gb ram), this is fast.

On the server (godaddy linux, 1 GB ram), it maxes out the resources and it takes 10x longer than it does on my local environment (up to 4.5 seconds for sentence-length).

I've timed every function in the class and found that the time is being spent in the line annotateImage().

I can throw hardware at the problem, but I was wondering if there's a better way to write text on an image? Or a way to speed up how annotate works (lower the image quality, etc)?

回答1:

It appears that on the (linux) server, shell_exec is indeed much much faster than the php extension; giving better times that the windows was (even sub 0.1 seconds). On the other hand, windows takes a tiny performance hit with shell_exec.

I'm assuming it's because of the version on godaddy - as of today, it's 6.5.4-7, which is a 2009 version. I was running 6.7.7-4 on my windows box. If they're running a 5 year old version of the program, the php extension may have undergone significant improvement since then.



标签: php imagick