CodeIgniter - 调整图像大小会导致“内存不足”

时间:2016-03-26 17:10:30

标签: php codeigniter image-resizing

我为一个允许他上传照片的客户端编写了一个实用程序(带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字节)in   /homepages/20/d153810528/htdocs/toolbox/cat/libraries/Image_lib.php on   第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));
}

1 个答案:

答案 0 :(得分:0)

在上传图片时,增加内存在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>