2011-06-15 102 views
2

在我的网站上,我允许用户上传6张图片,最大大小为5MB。它们必须格式为gif,jpg/jpeg。用户似乎上传的大多数图片大约为2MB(1006188字节),高度为1536像素,宽度为2048像素,因此非常大(特别是在尝试调整大小时)。PHP脚本GD库内存问题

然后我在服务器上有两个目录(large_img和small_img)。在large_img中,我将上传的文件移动到那里。然后,我使用GD库将该图像从该large_img目录重新调整为缩略图大小(100宽×100),然后将其移至small_img。

我已经越来越错误让我的Apache服务器上偶尔

PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 14592 bytes) 

最大内存为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'>&#8226; 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'>&#8226; 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); 
          } 
       } 
} 

继承人我的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照片或imagecreatefromgif()如果GIF图片格式

我被if()语句是破坏形象$imagedestroy($newsmallerimage);

注意:此相同的代码释放内存对于刚刚命名为['uploadedfile_1']的其他5张图像重复... 2等

任何建议可能使用file_get_contents或GD库将文件读入内存。我知道GD将整个图像保存在内存中,这可能是问题所在。 谢谢你们

+3

你'任何理由function_exists( 'imagejpeg')' ?如果GD没有安装,或者没有启用jpeg支持,你的脚本就会死在'imagecreatefromjpeg()'。 – 2011-06-15 18:25:04

+0

@Marc乙 - 没有任何理由,我不需要,如果(function_exists())部分可以摆脱那个不能,我把它的代码里面 – daza166 2011-06-15 18:37:02

回答

3

你不是免费$im,也许这就是问题所在。

+0

以外,所以我如何释放它,imagedestroy()不这样做? – daza166 2011-06-15 18:05:33

+0

@daza:你只是摧毁了调整大小的图像。 '$ im'是原始图像,您不会对此进行破坏。 – 2011-06-15 18:25:47

+0

现货我没有destroyimage($ im)。只有imagedestroy($ newsmallerimage)难怪我内存不足,这就是错误日志指向的内容。谢谢 – daza166 2011-06-15 18:29:28

0

摧毁你在if()条件添加线过imagecreatefromjpeg()

创建图像,

imagedestroy($im); 

这样,

if(!$im) 
{ 
    $image_resize_error++; 
    $array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>&#8226; An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>"; 
    imagedestroy($im); 
}