简体   繁体   English

PHP ImagickDraw线程化

[英]PHP ImagickDraw threaded

I'm trying to use ImagickDraw on PHP to draw an image using a very large number (~100,000) of circle() rectangle() etc calls. 我正在尝试在PHP上使用ImagickDraw,以使用大量(〜100,000)的circle(),rectangle()等调用来绘制图像。 These are split between the 4 cmyk channels so each channel gets around ~30k calls. 这些在4个cmyk通道之间分配,因此每个通道约有30k个呼叫。

The actual circle() and rectangle() calls themselves are really fast and that entire part of the program runs in less than a second; 实际的circle()rectangle()调用本身确实非常快,并且程序的整个部分运行时间不到一秒钟; then I hit the part where I use drawImage on each of the 4 separate ImagickDraw objects and this takes >15 seconds for each layer to run... understood it's a very complicated image, but is there any way to speed this up? 然后我碰到我在4个单独的ImagickDraw对象中的每个对象上使用drawImage的部分,并且每层运行需要15秒以上的时间...了解这是一个非常复杂的图像,但是有什么方法可以加快速度吗?

I've considered using pthreads, having a separate pthread for each of the 4 ImagickDraw objects, and this just caused the program to hang: 我考虑过使用pthreads,为4个ImagickDraw对象中的每一个都有一个单独的pthread,这只会导致程序挂起:

class Render extends Thread
{
    public $im;
    private $svg;

    public function __construct($width, $height, $bg, $svg)
    {
        $this->im = new Imagick();
        $this->svg = $svg;
        $this->im->newImage($width, $height, $bg);

    }

    public function run()
    {
        $id = new ImagickDraw();
        $id->setVectorGraphics($this->svg);
        $this->im->drawImage($id);
    }
}

$threads = [];
$imarray = [];
foreach($drawar as $c=>$s){
    $threads[$c] = new Render($finalsize['width'], $finalsize['height'], 'white', $s);
    $threads[$c]->start();
}

foreach($threads as $c=>$p){
    $p->join();
    $imarray[$c] = $p->im;
    echo "Got {$c} data\n";
}

Thank you! 谢谢!

I'm surprised that i) works ii) is that fast. 我很惊讶我)ii)这么快。

What I'd suggest trying is forget making this many individual calls to Imagick. 我建议尝试的是忘记给Imagick这么多单独的电话。

Instead generate an SVG XML document by hand, using the appropriate svg circle and rectangle tags. 而是使用适当的svg圆形和矩形标记手动生成SVG XML文档。 Then either use Imagick to composite/convert that image into the output image format you want. 然后使用Imagick将该图像合成/转换为所需的输出图像格式。

I've considered using pthreads, having a separate pthread for each of the 4 ImagickDraw objects, and this just caused the program to hang: 我考虑过使用pthreads,为4个ImagickDraw对象中的每一个都有一个单独的pthread,这只会导致程序挂起:

Yeah, that's not going to work. 是的,那是行不通的。 The underlying C library isn't compatible with pthreads. 底层的C库与pthreads不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM