【发布时间】: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