[英]make image thumbnail before upload - php codeigniter
我想將圖像大小調整為160 x 160並制作縮略圖,然后將該縮略圖存儲在文件夾中。 我不想存儲真實圖像,只能存儲它的縮略圖。 以下是我的代碼:
$this->load->library('upload');
$this->load->library('image_lib');
$blog_image = $_FILES['blog_image']['name'];
$config = array ('upload_path' => './blogs/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'image_library' => 'gd2',
'source_image' => $blog_image,
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 160,
'height' => 160
);
$this->upload->initialize($config);
$this->upload->do_upload('blog_image');
$this->image_lib->resize();
此代碼不起作用。 它上傳圖像而不調整大小。 請幫忙。
$config['image_library'] = 'gd2';
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['new_image'] = $target_path
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 160;
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
查看此鏈接以獲取更多信息... https://stackoverflow.com/a/13154916/6588826
請嘗試以下示例代碼。
$config = array(
'upload_path' => './blogs/',
'allowed_types' => 'jpg|png|gif',
'max_filename' => '255',
'encrypt_name' => TRUE,
);
$this->load->library('upload', $config);
//check file successfully uploaded. 'blog_image' is the name of the input
if ($this->upload->do_upload('blog_image')) {
//Now go to resize
$image_data = $this->upload->data();
$config_resize = array(
'image_library' => 'gd2',
'source_image' => $image_data['full_path'], //get original image
'maintain_ratio' => TRUE,
'width' => 160,
'height' => 160
);
$this->load->library('image_lib', $config_resize);
if (!$this->image_lib->resize()) {
print_r($this->image_lib->display_errors());
}
}else{
print_r($this->upload->display_errors());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.