0

私は、透過性のある一連のPNGを定期的にループしてそれらをマージする必要があるWebサイトを実行しています。これは長いプロセスになる場合があるため、これを行う最も効率的な方法は何かと考えていました。ImageMagickはそれほど速くないと聞いたので、GDを使用しています..

$firstTime = true;  // need to know if it's the first time through the loop
$img = null;        // placeholder for each iterative image
$base = null;       // will become the final merged image
$width = 0;
$height = 0;

while( $src = getNextImageName() ){
    $imageHandle = imagecreatefrompng($src);
    imageAlphaBlending($imageHandle, true);
    imageSaveAlpha($imageHandle, true);

    if( $firstTime ){
        $w = imagesx( $img );       // first time in we need to
        $h = imagesy( $img );       // save the width & height off
        $firstTime = false;
        $base = $img;               // copy the first image to be the 'base'
    } else {
        // if it's not the first time, copy the current image on top of base
        // and then delete the current image from memory
        imagecopy($base, $img, 0, 0, 0, 0, $w, $h);
        imagedestroy($img);
    }
}

// final cleanup
imagepng($base);
imagedestroy($base);
4

2 に答える 2

1

ぜひImageMagickを試してみてください。実装は簡単で、使用するだけexec('composite 1.png 2.png');です。それは十分に文書化されており、PHP のメモリ制限に縛られておらず、パフォーマンスも問題ありません。

さらに、ImageMagick は、bash スクリプトやその他の端末機能のスタンドアロンとしても優れた機能を発揮します。つまり、学んだことは PHP 以外でも役に立ちます。

于 2012-09-13T07:29:46.737 に答える
1

ベンチマークによると、ImageMagick は GD よりも高速です。これは少なくとも最初の一歩です。

PHPの優先度を通常以上/高に上げることもできるかどうかわかりませんか?

于 2012-09-13T07:38:48.877 に答える