私のサイトでは、ユーザーが最大サイズ 5MB の 6 つの画像をアップロードできるようにしています。gif、jpg/jpeg のいずれかの形式である必要があります。ユーザーがアップロードすると思われるほとんどの画像は、およそ 2MB (1006188 バイト) で、高さ:1536 ピクセル、幅:2048 ピクセルなので、非常に大きい (特にサイズを変更しようとする場合)。
次に、サーバーに 2 つのディレクトリ (large_img と small_img) があります。large_img では、アップロードされたファイルをそこに移動します。次に、GD ライブラリを使用してその large_img ディレクトリからその画像のサイズをサムネイル サイズ (高さ 100 x 幅 100) に変更し、それを small_img に移動します。
たまにエラーが出るようになった
PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 14592 bytes)
私のApacheサーバーで許可されている最大メモリは 64MB です。したがって、私のコードには何らかの問題があります。つまり、この 64MB のいくつかのチャンクを取得し、何らかの理由でそれを解放していません。
画像を取得してサイズを変更するコードは次のとおりです。6 つのうち最初の画像にそれを提供しましたが、コピーして貼り付けることが多いため、ここにはエラー チェックを含めていません。
if(move_uploaded_file($_FILES['uploadedfile_0']['tmp_name'], $move_file_to_directoryname_large))
{
$n_width=100;$n_height=100; /*specify height/width of thumbnail image to be*/
if($_FILES["uploadedfile_0"]["type"]=="image/gif")
{
$im=imagecreatefromgif($move_file_to_directoryname_large);
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im); $height=imagesy($im);
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagegif"))
{
imagegif($newsmallerimage,$move_file_to_directoryname_small);
$images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
if($_FILES["uploadedfile_0"]["type"]=="image/jpeg")
{
$im=imagecreatefromjpeg($move_file_to_directoryname_large); /*create from image stored in directory specified when moving from /tmp*/
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im);/*Original picture width is stored*/$height=imagesy($im);/*Original picture height is stored*/
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
$imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagejpeg"))
{
imagejpeg($newsmallerimage,$move_file_to_directoryname_small);
$images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
}
Heres私のphp.ini設定:
upload_max_filesize = 30M
post_max_size = 30M
max_execution_time = 120
max_file_uploads = 6
memory_limit=128M
スクリプトの何かがメモリを消費しています。上記のエラーが発生したのは$im=imagecreatefromjpeg($move_file_to_directoryname_large);
、その jpeg 写真の場合、または gif 写真形式の場合は imagecreatefromgif()の場合、コードのこの部分で発生します。
イメージを破壊することでメモリを解放しています$imagedestroy($newsmallerimage);
['uploadedfile_1'] ...2 などという名前の他の 5 つの画像に対して、この同じコードの if() ステートメントが繰り返されることに注意してください。
おそらく、file_get_contents または GD ライブラリを使用してファイルをメモリに読み込むことをお勧めします。GD は画像全体を圧縮せずにメモリに保持していることが問題になる可能性があることを知っています。みんなありがとう