2009-11-12 34 views
0

我有一個腳本可以從上傳的圖像中創建縮略圖。它工作正常與jpgs,但給我一個錯誤PHp - 調整PNG圖像大小時出現內存錯誤

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 26250000 bytes)

當我上傳一個PNG圖像。

的腳本是:

//create thumbnail; $modwidth and height are calculated in another part of the script 
//$original is the path to the full sized image 

$tn = imagecreatetruecolor($modwidth, $modheight); 
switch (strrchr($new_image_name,'.')) { 
    case ".jpg": 
    $image = imagecreatefromjpeg($original); 
    break; 
    case ".jpeg": 
    $image = imagecreatefromjpeg($original); 
    break; 
    case ".png": 
    $image = imagecreatefrompng($original); 
    break; 
    case ".gif": 
    $image = imagecreatefromgif($original); 
    break; 
} 
imagecopyresampled($tn, $image, 0, 0, $x_pos, $y_pos, $modwidth, $modheight, $width, $height); 
switch (strrchr($new_image_name,'.')) { 
    case ".jpg": 
    imagejpeg($tn, $target_path, 100); 
    break; 
    case ".jpeg": 
    imagejpeg($tn, $target_path, 100); 
    break; 
    case ".png": 
    imagepng($tn, $target_path, 0); 
    break; 
    case ".gif": 
    imagegif($tn, $target_path); 
    break; 
} 

正如我所說的它的JPG完美的作品,並與GIF格式。 該內存錯誤只出現在PNG上,而我只使用了1.2Mb的圖像。

我該如何解決這個問題? 感謝 帕特里克

回答

2

使用ini_set('memory_limit', '256M');

+0

謝謝塞薩爾,它工作。 腳本之後我是否必須減少內存限制? (一般來說,在設置非常高的內存限制時是否有任何問題?) – patrick

+0

沒問題,這個定義的內存只適用於這個腳本,如果你需要所有需要在php.ini中改變的腳本 – Cesar

+0

澄清:你的圖像是相當大的,如果你上傳更大的圖像,你的腳本將再次失敗。試着限制發送給腳本的圖像大小。 –

1

您需要腳本之前增加memory_limit的php.ini中設置這樣的事情

memory_limit = 128M