2012-07-09 28 views
0

我有一個包含大約1000個圖像URL的XML文件。我想使用PHP創建所有圖像的精靈。使用PHP和XML創建1000個圖像的CSS-sprite

  • XML文件的大小約爲30 MB
  • 的圖像是大約15KB大小

我怎樣才能得到它呢?

這裏是我的代碼(我得到致命錯誤:讓內存大小...):

<?php 

$dest    = imagecreatefromjpeg('15000x2000.jpg'); 
$increasing_width = 0; 
$increasing_height = 0; 

$xmldata = '1000-images.xml'; 

$open = fopen($xmldata, 'r'); 

$content = stream_get_contents($open); 

fclose($open); 

$xml = new SimpleXMLElement($content); 
foreach ($xml->xpath('//image') as $image) { 
    $src = imagecreatefromjpeg('test/' . $image->picture->attributes() . '.jpg'); 

    if ($increasing_width == '15000') { 
     $increasing_width = '0'; 
     $increasing_height += 200; 
    } 

    imagecopymerge($dest, $src, $increasing_width, $increasing_height, 0, 0, 150, 200, 100); 

} 

imagejpeg($dest, '15000x2000.jpg'); 

?> 
+0

分配更多的內存?聽起來這將是一個巨大的圖像(像素明智),這將需要一個公噸的內存。 – deceze 2012-07-09 11:30:04

+1

我有兩個問題:你爲什麼要將數字與字符串進行比較,以及實際上允許的內存大小有多少? – raina77ow 2012-07-09 11:31:05

+0

記住兩點建議:要麼增加php.ini中的memory_limit指令,要麼使用ImageMagick(http://www.php.net/manual/en/book.imagick.php)庫代替gd2 – antoniom 2012-07-09 11:31:16

回答

1

您將需要增加memory_limit

插入代碼的開頭:

ini_set('memory_limit','128M'); # Default: 128M 

「請注意,有沒有內存限制,設置這個指令爲-1。 」 - php.net


如果你得到一個超時後有足夠的內存,您將需要增加max_execution_time

插入代碼的開頭:

ini_set('max_execution_time', 30); # Default 30 

當在命令行運行PHP的默認設置爲0 - php.net