I've been trying to upload and resize an image. The upload seems to be finally be working, but it's not resizing.
In my constructor I am loading the library
$this->load->library('image_lib');
and then in my function I'm calling the the resize and upload
$this->require_auth();
$img = $this->input->post('photo1');
$config['upload_path'] = './HTML/files/';
$config['file_name'] = 'photo1.jpg';
$config['file_path'] = './HTML/files/photo1.jpg';
$config['max-size'] = 2000;
$config['image_library'] = 'gd2';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 549;
$config['height'] = 549;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['new_image'] = $config['file_path'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('photo1')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}else{
$this->blog_model->editServiceImg1($config['file_path']);
redirect('/blog');
}
I have also tried this
$this->require_auth();
$img = $this->input->post('service_photo1');
$config['upload_path'] = './HTML/files/';
$config['file_name'] = 'service_photo1.jpg';
$config['file_path'] = './HTML/files/service_photo1.jpg';
$config['max-size'] = 2000;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('service_photo1')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}else{
$config['image_library'] = 'gd2';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 549;
$config['height'] = 549;
$config['new_image'] = $config['file_path'];
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->blog_model->editServiceImg1($config['file_path']);
redirect('/blog');
}
Is there something I'm missing? I've tried doing the resize after the initial upload as well and that seemed to do nothing as well. Any help would be much appreciated.
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = './HTML/files/';
$config['file_path'] = './HTML/files/photo1.jpg';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
If the above code won't work then please check the folder permissions where the images are upload.
You can also give 777 permission to the folder in following manner :-
sudo chmod -R 777 path
Solution will surely help you
I went through this code many times and finally resolved this issue. It seems that when you have
$config['create_thumb'] = TRUE;
in your code it will create a thumbnail with the resized proportions and if you do not tell it where to go it will hide it in a folder. So the resizing was working, just not on the right image.
To resolve this, I changed the above to.
$config['create_thumb'] = FALSE;
Related
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 150;
$config['file_name'] = $_FILES['image']['name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->initialize($config);
$this->load->library('upload',$config);
$this->upload->initialize($config);
I am unable to resize image ... please give me my mistake
As per the #jigar Shah comment you have to first initalize upload library before resize
$this->load->library('upload');
$config['upload_path'] = './uploads';
$config['allowed_types'] ='csv|xls|xlsx';
$config['max_size'] = 1024; //in KB
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['max_filename'] = 25;
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_name')) {
//if file upload failed then catch the errors
$this->handle_error($this->upload->display_errors());
$is_file_error = TRUE;
} else {
//store the file info
$image_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path']; //get original image
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 100;
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
$this->handle_error($this->image_lib->display_errors());
}
}
For more reference please visit
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
I am not able to save the image in the destination folder, the directory remains empty.
$this->load->library('image_lib');
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$config['image_library'] = 'gd2';
$config['source_image'] = '/arquivos_upload/link/'. $slug .'.jpg';
$config['allowed_types'] = 'jpg|png';
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['width'] = 405;
$config['height'] = 405;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
okay, so I have two separate configs for the same library in the same controller function. The first one runs just fine, but the second does not. How can I make it so codeigniter unsets the first one and uses the second config.
// Retrieve the data from the upload
$data = $this->upload->data();
//Re-size the large image and re-save it
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
$this->image_lib->crop();
echo 'Image Resized!';
}
//Create a thumbnail for the image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
echo 'Image Thumbnail Created!';
}
So it re-sizes the image and then is supposed to thumbnail an image.
You can use the initialize method provided by the library, so you call it instead of loading the library each time:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->image_lib->initialize($config);
....
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->image_lib->initialize($config);
I have a problem making thumbs after an image upload. I can upload images perfectly but it doesn't make thumbs for me. I have a map uploads where the original images are stored and a map thumbs in uploads. It's probably a path issue i guess. Thanks
This is my code in my controller.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '6000';
$config['max_width'] = '3024';
$config['max_height'] = '3768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
$datap = array('upload_data' => $this->upload->data());
$gallery_path = realpath(APPPATH . '../uploads');
$upload_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = $gallery_path . '/thumbs';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();