2011-12-14 11 views
0

jpgまたはpngの形式の大きな順序のない画像セットがある場合、最初に許可されているフォーマットのすべてのフォルダ内容をフィルタリングし、新しいフォルダにコピーするPHPスクリプトを作成したかった番号順に名前を変更して(1.jpg、2.jpg、3.jpg ..)、子フォルダ "thumbs"に各画像の50x50サムネイルを作成し、 "gallery"というダンプを含む.htmlファイルを作成します各サムネイルの "img"タグ。PHP致命的なエラー:許容されるメモリサイズ

これは、十数枚程度の画像までうまく動作し、割り当て可能な最大メモリを超えています。これは突然発生し、imagecopyresized関数が呼び出されたときに表示されます。

アドバイスありがとうございます。

出典:

<?php 

# Prepare vars 
$dir = "O:/zip/"; 
$newDir = "C:/Users/user/Desktop/zip/"; 
$thumbs = $newDir."thumbs/"; 
$gallery = $newDir."gallery.html"; 
$types = array(".jpg", ".png"); 
$files = array(); 
$tempFiles = scandir($dir); 
$i = 0; 

# Copy and rename images 
foreach($tempFiles as $file) 
{ 
    $thisType = substr($file,-4); 
    if(in_array($thisType, $types)) 
    { 
     $dest = fopen($newDir.$i.$thisType, 'w'); 
     fwrite($dest, file_get_contents($dir.$file)); 
     fclose($dest); 

     list($width, $height) = getimagesize($newDir.$i.$thisType); 
     $im = imagecreatetruecolor(50, 50); 
     if($thisType == '.jpg') 
     { 
      imagecopyresized($im, imagecreatefromjpeg($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height); 
      imagejpeg($im, $thumbs.$i.$thisType); 
     } 
     else 
     if($thisType == '.png') 
     { 
      imagecopyresized($im, imagecreatefrompng($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height); 
      imagepng($im, $thumbs.$i.$thisType); 
     } 
     imagedestroy($im); 

     $html .= "<a href='$newDir$i$thisType'><img src='$thumbs$i$thisType' alt='$i$thisType' width='50' height='50'></a>"; 
     print "Successfully processed $i$thisType<br>"; 
     $i++; 
    } 
} 
print "Done.<br>"; 

# Create html gallery for new imgs 
$dest = fopen($gallery, 'w'); 
fwrite($dest, $html); 
fclose($dest); 

print "There are ".number_format($i)." image files.\n\r"; 

?> 
+0

利用可能なメモリを増やしてみましたか? – alex

+1

私はそれを考えましたが、私はむしろメモリの問題を解決したい、私は最初のフォルダに多くの画像がある制限を増加し続ける必要があるだろう。 – Lee

答えて

3

あなたのコードはOKに見えます。 12枚目の画像の大きさは他の画像よりも大きいですか? imagecopyresized()@ php.netのドキュメントをチェックして、誰かがsetMemoryForImage()関数を書いて、現在のphp.iniの 'memory_limit'の設定でイメージのサイズを変更できるかどうかを判断します。

これは、元の画像が大きすぎると、サイズを変更しようとするとメモリが使い果たされると考えています。

+0

私はイメージが数百Kに過ぎないので、私は困惑しています。私は限界を1024に増やし、すべて〜500イメージが完全に処理されます。 – Lee

+0

@リー:あなたは何から限界を増やしましたか? –

+0

デフォルトは16で、これは大規模な変更を考慮して、私の方法に何か間違っていると信じさせるものです。 – Lee

1

Any advice is appreciated.

まあ、一見、私はリークが表示されていないが、あなたがメモリを解放しない嘘を発見するデバッグ情報をプリントアウトするmemory_get_usage()を使用することができます。

これは、このエラーだけの原因となる1つの非常に大きな画像に過ぎません。この場合、yopuはメモリの制限を増やす必要があります。

関連する問題