2013-02-21 35 views
0

因此,我需要合併一些圖像(36或48)圖像到另一個圖像(enter image description here);在很好的時間合併大量的圖像與PHP GD(imagecopymerge)

這是4800x4800像素的分辨率。所以每個廣場將有695x695;目前,我想出了這個解決方案:

$i = 1; 
     $x = 140; $y = 140; 
     foreach($files as $file): 
      if($i > 1) $template = 'test.png'; 
      else $template = 'templates/24x24-TEMPLATE.png'; 
      $this->save_image($file,'templates/temp.jpg'); 
      $src = imagecreatefromjpeg('templates/temp.jpg'); 
      $src2 = imagecreatetruecolor(695,695); 
      imagecopyresampled($src2, $src, 0, 0, 0, 0, 695, 695, 612, 612); 
      imagejpeg($src2,'templates/temp.jpg'); 
      imagedestroy($src2); 
      $src = imagecreatefromjpeg('templates/temp.jpg'); 
      $dest = imagecreatefrompng($template); 
      imagealphablending($dest, false); 
      imagesavealpha($dest, true); 
      imagealphablending($src, false); 
      imagesavealpha($src, true); 
      imagecopymerge($dest, $src, $x, $y, 0, 0, 695, 695, 100); //have to play with these numbers for it to work for you, etc. 
      imagepng($dest,'test.png'); 
      /* Destroy the images to free up space */ 
      imagedestroy($dest); 
      imagedestroy($src); 
      $x = $x + 695 + 65; 
      if($i % 6 == 0): 
       $y = $y + 695 + 65; 
       $x = 140; 
      endif; 
      $i++; 
     endforeach; 

巫下載到被放到廣場上的文件,用正方形將其合併,並會替所有圖像,直到所有方格填滿。但是那個代碼,對於20張圖片來說,最多需要5分鐘!我需要能夠在30秒內運行的東西來填充圖像的方格,然後生成單個文件PDF。

反正有改善嗎?或者還有其他方式可以做得更快更好嗎?

+0

你試過wkhtmltopdf,你會在html中顯示圖像,然後調用wkhtmltopdf – sanj 2013-02-21 10:45:01

+0

我其實不需要那樣做。我將需要在後端處理圖像,因爲我不想在網頁中放置像4800x4800這樣的大文檔。 – roshkattu 2013-02-21 10:47:27

回答

1

我認爲你的意思是快速而有效的。

然後答案可能是在PHP之外做。儘管數據大部分是使用GD擴展保存在自定義數據結構中,但PHP代碼仍然存在很多開銷。有lots of tools for merging images用於生成CSS精靈 - 但它們並不都能控制圖像的佈局。你可以使用GD和C編寫你自己的。

但是爲什麼要麻煩?如果您非常希望將輸出視爲PDF,爲什麼不直接將各個圖像直接寫入PDF?

+0

是的。對。我昨天累了,而且我的圖像世代讓我感到困惑。我應該從開始就將所有內容合併到PDF中。哦 – roshkattu 2013-02-21 11:06:54