我正在尝试这样做,当用户上传他们的个人资料图片时,它会将其大小调整为180x180,以适合个人资料图片框。然后我想要它也创建该图像的缩略图版本,以便我可以将它用于帖子等。这是我现在的代码:
function do_upload_profilepicture()
{
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$config['upload_path'] = './img/profilepictures/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $userID;
$config['max_size'] = '500';
$config['max_width'] = '1920';
$config['max_height'] = '1028';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_profilepic_form', $error);
}
else
{
$upload_data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['source_image'] = $upload_data['full_path'];
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 180;
$resize['height'] = 180;
$this->load->library('image_lib', $resize);
$this->image_lib->resize();
$this->model_users->setProfilePic($userID, $upload_data['orig_name']);
redirect('upload/create_thumb/' . $upload_data['orig_name']);
}
}
function create_thumb() {
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$imgname = $this->model_users->parseURL($_SERVER['REQUEST_URI'], 1);
$source_path = $imgsrc;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => 'img/profilepictures/' . $imgname,
'new_image' => base_url() . 'img/profilepictures/thumbs/' . $imgname,
'maintain_ratio' => TRUE,
'width' => 50,
'height' => 50
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
redirect('userprofile/home/' . $userID);
}
}
它不会创建新文件,为什么会这样?有更简单的方法吗?
答案 0 :(得分:0)
我在您的代码中发现的一个问题是,您正在重定向用户只是为了创建缩略图,您只需在do_upload_profilepicture方法中调用该方法并传递图像名称就不需要一次又一次地重定向。
function do_upload_profilepicture()
{
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$config['upload_path'] = './img/profilepictures/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $userID;
$config['max_size'] = '500';
$config['max_width'] = '1920';
$config['max_height'] = '1028';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_profilepic_form', $error);
}
else
{
$upload_data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['source_image'] = $upload_data['full_path'];
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 180;
$resize['height'] = 180;
$this->load->library('image_lib', $resize);
$this->image_lib->resize();
$this->image_lib->clear();
$this->model_users->setProfilePic($userID, $upload_data['orig_name']);
$this->create_thumb( $upload_data['orig_name']);
redirect('userprofile/home/' . $userID);
}
}
function create_thumb($imgname) {
$source_path = $imgsrc;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => 'img/profilepictures/' . $imgname,
'new_image' => 'img/profilepictures/thumbs/' . $imgname,
'maintain_ratio' => TRUE,
'width' => 50,
'height' => 50
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
}
你也在new_image中使用base_url为拇指你需要给文件路径而不是http路径。我建议你使用这个库在codeigniter codeigniter-advanced-images
中创建图像