I am unable to resize my image in codeigniter - php

$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

Related

Codeigniter image compression not working

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';

source image is empty codeigniter crop image

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();
}

Codeigniter file resizing on upload

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;

CodeIgniter: Making thumbs

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();

codeigniter thumbnail created but original image problem

I have the code below, if i don't use watermark it's fine but when i use watermark, both the original pics and thumbnail become very small and , watermark happens on thumbnail, I want watermark on the original image, pls help
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/blog';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('photo')){
$e = $this->upload->display_errors();
print_r($e);
}
$image = $this->upload->data();
//thumbnail creation start
$config1['image_library'] = 'gd2';
$config1['source_image'] = $image['full_path'];
$config1['create_thumb'] = TRUE;
$config1['maintain_ratio'] = TRUE;
$config1['width'] = 75;
$config1['height'] = 50;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
//thumbnail creation ends
$this->image_lib->clear();
//$image = $this->upload->data();
//start watermarking
$config2['source_image'] = $image['full_path'];
$config2['wm_text'] = 'Copyright 2011 myself';
$config2['wm_type'] = 'text';
$config2['wm_font_path'] = './system/fonts/FromWhereYouAre.ttf';
$config2['wm_font_size'] = '16';
$config2['wm_font_color'] = 'ffffff';
$config2['wm_vrt_alignment'] = 'middle';
$config2['wm_hor_alignment'] = 'center';
$config2['wm_padding'] = '20';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->watermark();
//end watermarking
please try adding
$config2['new_image'] = '';
in the watermarking code

Categories