PHP Imagick's setFont method take too long tim

2019-09-13 02:49发布

问题:

I got a very weird problem in my PHP environment using Imagick:

My environment is like this:

Darwin 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

PHP 7.0.16 (cli) (built: Feb 16 2017 22:57:49) ( NTS )

imagick module version => 3.4.3RC4
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.9.7-6 Q16 x86_64 2017-02-01 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.7-7 Q16 x86_64 2017-02-09 http://www.imagemagick.org

And the method setFont of Imagick will cost too long to execute(and even get the default 30 second timeout in execution).

The code is just like this:

<?php
    $img = new Imagick();
    $img->setFont("./SpicyRice.ttf");
    echo "Done";

And the code $img->setFont("./SpicyRice.ttf") will get stuck.

No error is thrown, PHP just hang at that method, and timeout the default 30 seconds of execution.

Is there anyone have any thoughts about this? This is the font file that I used.

回答1:

With the help by @Danack, I resolve the problem and fix it finally.

The problem is so simple, but is really hard to identify at the first time, I think.

The problem for this is that I installed many new fonts into the font library, and didn't rebuild the font config cache.

So, each time when Imagick initing, it try to get the font config and the font config trying to read every font in my font folder to write the font cache.

But, since PHP has a 30 seconds timeout, so the PHP process will fail before the font cache is rebuilt. So, this is endless problem, unless I run the fc-cache command in the command line, and generate the font cache, then, next time when I call this php, the Imagick plugin will use the system's font cache instead to generate all the informations for the font.

And this explains why this works in my command line, because I'm the user of the system, and the system will create font cache for me only.

So, when I run the PHP using command line, it will work, since it has the right font cache, but for the server, since it is httpd and running using system's font cache, it won't work.

So, for now, PHP's imagick is working like normal.

Thank you again, @Danack. Without your help, I won't know the problem like this. :)