我一直在尝试调整图像的大小,但没有运气!我在上传功能中调整了大小,但不确定发生了什么错误!下面的代码显示了我的上传函数(并在其中调整大小):
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('image_lib', $config);
$this->load->library('upload', $config);
$fullImagePath = '';
if (! $this->upload->do_upload())
{
$this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
$error = array('file_error' => $this->upload->display_errors());
// print_r($error);
$this->load->view('layout/header');
$this->load->view('add_gallery', $error);
$this->load->view('layout/footer');
}else{
//echo "UPLOAD SUCCESS";
// set a $_POST value for 'image' that we can use later
/*Image Manipulation Class*/
$config['image_library'] = 'gd2';
$config['source_image'] = $fullImagePath;
echo $fullImagePath;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['max_width'] = '480';
$config['max_height'] = '640';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$upload_data = $this->upload->data();
$fullImagePath = '/uploads/' . $upload_data['file_name'];
}
return $fullImagePath;
}
上传工作得很好,我得到fullImagePath(链接)存储在数据库中。无论如何,只是不确定如何处理调整大小。
resize()
函数要求您在调用它之前首先配置source_image
,以便它可以对指定的图像应用调整大小。
您正在发送一个空路径。在这里你声明路径为空字符串$fullImagePath = '';
,然后在这里你定义它到配置选项$config['source_image'] = $fullImagePath;
,之后你调用$this->image_lib->resize();
,所以它不能做一个空图像的大小调整。
你也加载了两次image_lib
,这是不需要的。
我修改了你的代码。测试它,看看它是否工作
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$fullImagePath = '';
if (! $this->upload->do_upload()){
$this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
$error = array('file_error' => $this->upload->display_errors());
$this->load->view('layout/header');
$this->load->view('add_gallery', $error);
$this->load->view('layout/footer');
}else{
$upload_data = $this->upload->data();
$fullImagePath = '/uploads/' . $upload_data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $fullImagePath;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['max_width'] = '480';
$config['max_height'] = '640';
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
return $fullImagePath;
}