2012-09-13 3 views
0

투명도가있는 PNG 묶음을 반복적으로 루프하여 병합해야하는 웹 사이트를 운영합니다. 때로는 이것이 긴 과정이 될 수 있으므로 가장 효율적인 방법은 무엇인지 궁금 해서요. ImageMagick이 실제로 더 빠르다고 들으면서 저는 GD를 사용하고 있습니다 ..PHP에서 여러 PNG를 가장 효율적으로 병합하는 방법

$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); 
+0

프로파일 링을 통해 느린 부분을 확인 했습니까? 다른 기능의 구현은 무엇입니까? – alex

+0

더 효율적으로 또는 더 빠르게 만들고 싶습니까? 여러 이미지가있는 경우 병렬로 병합을 수행 할 수 있습니다. –

+1

ImageMagick 시도 –

답변

1

분명히 ImageMagick을 시도해야합니다. 구현하기 쉽습니다. exec('composite 1.png 2.png'); 만 사용하십시오. 잘 설명되어 있으며 PHP의 메모리 제한에 구애받지 않고 성능이 좋습니다.

또한 ImageMagick은 bash 스크립팅 또는 다른 터미널 기능의 독립 실행 형으로 훌륭하게 작동합니다. 즉, 배운 내용이 PHP 외부에서 유용하다는 것을 의미합니다.

1

a benchmark에 따르면 ImageMagick은 GD보다 빠릅니다. 이것은 적어도 시작일 것입니다.

PHP의 우선 순위를 보통/높음보다 높게 설정할 수 있는지 여부를 모르겠습니다.

관련 문제