【问题标题】:My PHP script fails uploading 50M above images我的 PHP 脚本无法上传 50M 以上的图片
【发布时间】:2015-02-20 12:25:13
【问题描述】:

我必须上传 75MB(JPEG 和 PNG)的图像。出现这样的错误:

(致命错误:内存不足(已分配-1912078336)(试图分配30000字节)

如果我上传 40+、50+ 以上的 png 或 jpeg。

在这一步$new_image = imagecreatetruecolor($width, $height); 如果我上传 30+ JPG 图片我会收到错误:

致命错误:内存不足(已分配-1910767616)(试图分配43200字节)

我还在 PHP 值下方的 .htaccess 中添加:

php_value upload_max_filesize 2000M
php_value max_input_time 1000
php_value memory_limit 2000M
php_value max_execution_time 20000
php_value post_max_size 2000M

这是脚本:

<?php
set_time_limit(300);
ini_set('memory_limit', '64800M');
ini_set("max_execution_time",10000);
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$target_dir = "uploads/";
$unq_name=(mt_rand(10,1000));
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$source_photo = 'uploads/'.basename( $_FILES["fileToUpload"]["name"]);
$dest_photo = 'uploads/testing_'.$unq_name.'.'.$imageFileType;


$image = new SimpleImage(); 
$image->load($source_photo);
$image->scale(50);
$image->save($dest_photo); 


class SimpleImage {   
var $image; var $image_type;   
function load($filename){   
            $image_info = getimagesize($filename); 
            $this->image_type = $image_info[2]; 
            if( $this->image_type == IMAGETYPE_JPEG ) 
             {  
             $this->image = imagecreatefromjpeg($filename);
             } 
             elseif( $this->image_type == IMAGETYPE_GIF ) 
             {   
             $this->image = imagecreatefromgif($filename); 
             } 
             elseif( $this->image_type == IMAGETYPE_PNG )
             { 
             $this->image = imagecreatefrompng($filename);
             } 
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=70, $permissions=null) 
{
            image_type=$this->image_type;

          if( $image_type == IMAGETYPE_JPEG )
          {
             imagejpeg($this->image,$filename,$compression);
          } 
          elseif( $image_type == IMAGETYPE_GIF ) 
          {
             imagegif($this->image,$filename);  
          } 
          elseif( $image_type == IMAGETYPE_PNG )
           {
            imagealphablending($this->image, false);
            imagesavealpha($this->image,true);
            imagepng($this->image,$filename);
          }   
          if( $permissions != null) {
             chmod($filename,$permissions);
          }
  }

function output($image_type=IMAGETYPE_JPEG)
{
   if( $image_type == IMAGETYPE_JPEG )
   { 
       imagejpeg($this->image);
    }
     elseif( $image_type == IMAGETYPE_GIF ) 
    {  
     imagegif($this->image);
      } 
     elseif( 
     $image_type == IMAGETYPE_PNG )
     {   
     imagepng($this->image); 
     } 
} 
function getWidth()
{  
          return imagesx($this->image); 
} 
function getHeight() {  
           return imagesy($this->image);        
}
function resizeToHeight($height)
{   
       $ratio = $height / $this->getHeight(); 
       $width = $this->getWidth() * $ratio; 
       $this->resize($width,$height);
} 
function resizeToWidth($width)
{ 
             $ratio = $width / $this->getWidth(); 
             $height = $this->getheight() * $ratio; 
             $this->resize($width,$height);
}   
function scale($scale) 
{ 
         $width = $this->getWidth() * $scale/100; 
         $height = $this->getheight() * $scale/100; 
         $this->resize($width,$height);
}  

function resize($width,$height) 
{   

              $new_image = imagecreatetruecolor($width, $height); 
                if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) 
                { 
                                          $current_transparent = imagecolortransparent($this->image);
                                            if($current_transparent != -1) 
                                            { 
                                            $transparent_color = imagecolorsforindex($this->image, $current_transparent); 
                                            $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 
                                            imagefill($new_image, 0, 0, $current_transparent); 
                                            imagecolortransparent($new_image, $current_transparent); 
                                            } 
                                            elseif( $this->image_type == IMAGETYPE_PNG) 
                                            {
                                                 imagealphablending($new_image, false); 
                                                 $color = imagecolorallocatealpha($new_image, 255, 255, 255, 127); 
                                                 imagefill($new_image, 0, 0, $color); imagesavealpha($new_image, true);
                                            } 
                                    } 
                                 imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
                                 $this->image = $new_image;

                } 
}  

?>

【问题讨论】:

  • 您可能需要在 php.ini 配置中更改内存限制,请参阅 stackoverflow.com/questions/6314733/…
  • ^ 他已经这样做了。试试这个 Vasanth:ini_set('memory_limit', -1);
  • 调整图像大小所需的内存也取决于图片的分辨率。要调整 3000 x 3000 图片的大小,您需要大约 50MB 内存才能做到这一点
  • 我几乎重写了你的帖子。我已经添加了代码(我们更喜欢外部链接),引入了 php.ini 设置更改,并重新格式化它以使其可读。如果你能把你的帖子做成和上面类似的样子,那就可以为编辑们省下不少工作了!谢谢。
  • 让我试试这个 "ini_set('memory_limit', -1);"

标签: php file upload


【解决方案1】:

请更改您的 php.ini 设置。

找线:

upload_max_filesize = 100M
post_max_size = 100M

如果您有共享主机,您应该告诉管理员尚未使用 cpanel 等,请从控制面板执行此操作,另请参阅 herehere

请在创建新问题之前进行搜索。Google 是您的朋友。

【讨论】:

  • 任何建议请上传 jpeg:40MB, PNG:50MB。但我需要上传最大 75MB 的图片大小。
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2012-06-30
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
相关资源
最近更新 更多