【问题标题】:CodeIgniter - resizing images causes "out of memory"CodeIgniter - 调整图像大小会导致“内存不足”
【发布时间】:2016-03-26 17:10:29
【问题描述】:

我为一个客户编写了一个实用程序(使用 CodeIgniter 3.0.5),允许他上传照片,并针对网络调整了它们的大小。该脚本几个月来一直运行良好,但突然间他出现了内存不足的错误,大致如下:


文件名:IMG_0047.JPG 测试
调整图像大小...
新图片:/homepages/20/d153810528/htdocs/toolbox/stuff/images/tool_photos/IMG_0047_resized.JPG
新文件名:IMG_0047_resized.JPG

致命错误:内存不足(已分配 35389440)(试图分配 4032 字节)在 /homepages/20/d153810528/htdocs/toolbox/cat/libraries/Image_lib.php 上 第 1455 行



“调整大小”的图像实际上并未保存。

我知道首选解决方案是使用 php_ini 分配更多内存,但似乎托管服务提供商 -- 1and1 。 com——不允许这样做;它设置为硬120M;我猜他们不希望客户搞砸他们的共享服务器。

有什么想法吗?

这是处理大小调整的代码:

   public function uploadapicture() {

$status = '';
$msg = '';
$file_element_name = 'picture';

if ($status !== 'error') {
    $config['upload_path'] = 'stuff/images/tool_photos/';
    $config['allowed_types'] = 'gif|jpeg|png|jpg';
    $config['max_size'] = 1024 * 50;
    $config['encrypt_name'] = FALSE;

    $this->load->library('upload',$config);
    if (!$this->upload->do_upload($file_element_name)) {
        $status = 'error';
        $msg = $this->upload->display_errors('','');
    } else {
        $data = $this->upload->data();
        $image_path = $data['full_path'];
        if (file_exists($image_path)) {
            $status = 'success';
            $msg = 'Main picture "' . $_FILES[$file_element_name]['name'] . '" successfully uploaded';
        } else {
            $status = 'error';
            $msg = 'There was a problem saving the main picture.';
        }
    }
    @unlink($_FILES[$file_element_name]);

    $file_element_name = 'thumbnail';
    if ((strlen($_FILES[$file_element_name]['name']) > 0) && !$this->upload->do_upload($file_element_name)) {
        $status = 'error';
        $msg .= $this->upload->display_errors('','');
    } else if (strlen($_FILES[$file_element_name]['name']) > 0) {
        $data = $this->upload->data();
        $image_path = $data['full_path'];
        if (file_exists($image_path)) {
            $status = 'success';
            $msg .= 'Thumbnail successfully uploaded';
        } else {
            $status = 'error';
            $msg .= 'There was a problem saving the thumbnail.';
        }
    }
    if ($status === 'success') {
        echo "<br><pre>Post stuff:" . print_r($_POST,1);
        $toolToInsert = array(
            'picture_filename' => $_FILES['picture']['name'],
            'name' => $this->input->post('name'),
            'purchase_price' => $this->input->post('purchase_price'),
            'public_notes' => $this->input->post('public_notes'),
            'public_misc' => $this->input->post('public_misc'),
            'purchased_from' => $this->input->post('purchased_from'),
            'private_purchase_date' => $this->input->post('private_purchase_date'),
            'private_purchase_price' => $this->input->post('private_purchase_price'),
            'purchase_location' => $this->input->post('purchase_location'),
            'sold_by' => $this->input->post('sold_by'),
            'date_sold' => $this->input->post('date_sold'),
            'sale_price' => $this->input->post('sale_price'),
            'sold_to_name' => $this->input->post('sold_to_name'),
            'sold_to_phone' => $this->input->post('sold_to_phone'),
            'sold_to_email' => $this->input->post('sold_to_email'),
            'private_notes' => $this->input->post('private_notes'),
            'private_misc' => $this->input->post('private_notes'),
            'entered_this_year' => $this->input->post('entered_this_year'),
            'year_entered' => date('Y')
         );

        if (isset($_FILES['thumbnail']['name'])) {
            $toolToInsert['thumbnail_filename'] = $_FILES['thumbnail']['name'];
        }
        foreach($_POST as $pKey => $pVal) {
            if (substr($pKey,0,9) === 'category_') {
                error_log("Found a category: ".print_r($pVal,1)." for key of ".print_r($pKey,1));
                $post_category[] = substr($pKey,9);
            }
        }
        if (isset($post_category)) {
            $toolToInsert['category'] = implode(',',$post_category);
        }

        if (isset($_POST['active'])) {
            $toolToInsert['active'] = 1;
        }
        $this->load->model('Letme_model');
        $result = $this->Letme_model->insertTool('tool_db',$toolToInsert);
        echo "Result: \n";
        echo print_r($result,1);
    }

}
echo json_encode(array('status' => $status, 'msg' => $msg));
}

【问题讨论】:

  • 这里的内容太多了。您需要使用echo memory_get_usage(true); exit(); 逐步检查您的代码,找出内存过载的确切来源,然后我们可以提供一些建议。

标签: php codeigniter image-resizing


【解决方案1】:

在 php 5.x 中增加内存以上传图片的一种方法是在您的 .htaccess

只需添加以下行:

## I need more memory to upload large image
<IfModule mod_php5.c>
    php_value memory_limit 256M  ## or whatever you need
</IfModule>

【讨论】:

  • 是的,我也是这么想的,但是在这个帐户上,他们不允许访问 .htaccess。
  • 也许花一两块钱买一个体面的托管服务?你是什​​么意思无法访问.htaccess?它在你的根目录下,你应该可以控制它!
  • 很遗憾,这不是我的选择。
  • 好吧,我应该澄清一下......他们允许更改 .htaccess 但不允许更改 memory_limit。让它高于 120M,你只会得到 500 个错误。事实上,他们的帮助文档非常清楚地说明了这一点。说实话,我已经多次尝试说服我的客户他应该在上传图片之前重新调整图片大小,但他不想做这些额外的步骤。我希望这次经历能说服他。说真的,他最近尝试的图像很大——我们说的是 3000 x 4000。
猜你喜欢
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多