Can't Resize Photo in Codeigniter - codeigniter

I need to resize photo when uploaded into path, but the photo can't resize, please correct me about my code here:
private function _do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['file_name'] = slug($this->input->post('judul')).'_'.time();
$config['image_library'] = 'gd2';
$config['quality'] = '20%';
$config['remove_space'] = TRUE;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->load->library('upload',$config);
if (!$this->upload->do_upload('photo')) {
$this->session->set_flashdata('msg', $this->upload->display_errors('',''));
redirect('backoffice/tambah_video');
}
return $this->upload->data('file_name');
}

Please specify new image path and try to save.
$config['new_image'] = '/path/to/new_image.jpg';
Also make sure that you have GD library enabled.

You need to do resize() after do_upload(), and separate the configs for the image_lib and upload libraries, now they are mixed.
Try this:
private function _do_upload()
{
// step 1: upload
$upload_config['upload_path'] = './uploads/';
$upload_config['allowed_types'] = '*';
$upload_config['file_name'] = slug($this->input->post('judul')).'_'.time();
$this->load->library('upload', $upload_config);
if (!$this->upload->do_upload('photo')) {
$this->session->set_flashdata('msg', $this->upload->display_errors('',''));
redirect('backoffice/tambah_video');
}
// step 2: resize
$resize_config['source_image'] = $this->upload->upload_path . $this->upload->file_name; // uploaded file path
$resize_config['image_library'] = 'gd2';
$resize_config['quality'] = '20%';
$resize_config['remove_space'] = TRUE;
$resize_config['create_thumb'] = TRUE;
$resize_config['maintain_ratio'] = TRUE;
$resize_config['width'] = 75;
$resize_config['height'] = 50;
$this->load->library('image_lib', $resize_config);
$this->image_lib->resize();
return $this->upload->data('file_name');
}
If $resize_config['create_thumb'] = TRUE; the name of the small image will be suffixed _thumb. If you need to overwrite the uploaded image, do $resize_config['create_thumb'] = FALSE;. See also the thumb_marker and new_image configs in the official documentation.

Related

resizing images not working with me in codeigniter

i want to resize image in codeigniter in one function
i called Image_lib and upload libraries on my controller
the images folder gets read/write .
That is my controller.
public function update(){
$config['upload_path']="./images/";
$config['allowed_types']='jpg|jpeg|png';
$config['encrypt_name']=TRUE;
$config['max_size']='1024';
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$this->load->view('settings_v',array(
'userdata'=>$this->user->userdata(),
'c_data'=>$this->Settings_m->index(),
'error'=>$this->upload->display_errors(),
));
} else{
$image=$this->Settings_m->image(); // get current image to remove it
if(!empty($image)){
$path='images/'.$image;
unlink($path);
}
$config['image_library'] = 'gd2';
$config['source_image'] = '.images/'.$filename;
$config['new_image'] = '.images/'.$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 30;
$config['height'] = 30;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$file_data=$this->upload->data();
$filename=$file_data['file_name'];
$this->Settings_m->update($filename);
//redirect('Settings','refresh');
}
}
Thanks ,
Change '.images/ to './images/' also you never defined $filename before using it in image lib config.
$file_data = $this->upload->data();
$filename = $file_data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './images/' . $filename;
$config['new_image'] = './images/' . $filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 30;
$config['height'] = 30;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->Settings_m->update($filename);
//redirect('Settings','refresh');
Also if you just want to resize the source image and not have a separate resized image you should remove $config['new_image'] and $config['create_thumb'] as per the docs. Otherwise just use create_thumb or new_image (with a different path/filename than the original) but not both.
Notes regarding this preference:
If neither of the two preferences listed above (create_thumb, and
new_image) are used, the resizing method will instead target the
original image for processing.

Codeigniter Image_lib library source_img

Here's my code :
if(isset($_FILES["image"])){
$upload_dir = "files/images/user/profile/";
$ext = end(explode(".", $_FILES["image"]["name"]));
$config['upload_path'] = $upload_dir."original/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = "2048";
$config['encrypt_name'] = TRUE;
// $config['file_name'] = $this->session->userdata("username").time().".".$ext;
$this->load->library("upload", $config);
if($this->upload->do_upload("image")){
$image = $this->upload->data();
$conf_res["image_library"] = "gd2";
$conf_res["source_img"] = realpath($upload_dir."original/".$image["file_name"]);
$conf_res["new_image"] = $upload_dir."200x200/".$image["file_name"];
$conf_res["create_thumb"] = TRUE;
$conf_res["maintain_ratio"] = TRUE;
$conf_res["width"] = 200;
$conf_res["height"] = 200;
$this->load->library("image_lib", $conf_res);
if($this->image_lib->resize()){
echo "done."; die();
} else {
echo $this->image_lib->display_errors().$conf_res["new_image"].$image["file_name"];
}
} else {
$error = $this->upload->display_errors();
echo $error.$config['upload_path'];
}
}
And the error i'm getting is :
<p>You must specify a source image in your preferences.</p>
<p>Your server does not support the GD function required to process this type of image.</p>
files/images/user/profile/200x200/2606737d1b67a54cfea0a9d4f16ef336.jpg
2606737d1b67a54cfea0a9d4f16ef336.jpg
I've been thinking for about an hour what is wrong with my path. I've even checked phpinfo if it has gd2 installed. I cant find out what is the problem!
Try changing from
$conf_res["source_img"] = realpath($upload_dir."original/".$image["file_name"]);
to this
$conf_res['source_image'] = realpath($upload_dir."original/".$image["file_name"]);
and this also
$this->load->library('image_lib');
$this->image_lib->initialize($conf_res);
See more info here

crop image with imagemagick Codeigniter

It's the first time that im using imagemagick
well im trying to crop an image but the problem is that i get this error :
Image processing failed. Please verify that your server supports the
chosen protocol and that the path to your image library is correct.
here is my code
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] ="./assets/profile_pic.jpg";
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['x_axis'] = 300;
$config['y_axis'] = 300;
//$config['width'] = 650;
//$config['height'] = 353;
$config['new_image'] = "./assets/profile_pic.jpg";
$this->load->library('image_lib', $config);
//$this->image_lib->crop();
$this->image_lib->initialize($config);
if (!$this->image_lib->crop()){
echo $this->image_lib->display_errors();
}
So should i download imagemagick or something like that ?
thx guys !
Below has given me expected result. Hope you will also get same.
$this->load->library('image_lib');
//For resizing of image in size of dilog
$config['image_library'] = 'ImageMagick';
$config['library_path'] = 'C:\\ImageMagick\\';
$config['source_image'] = $source_filepath;
$config['new_image'] = $new_filepath;
$config['quality'] = '100%';
$config['maintain_ratio'] = TRUE;
$config['x_axis'] = 26;
$config['y_axis'] = 54;
$config['width'] = 100;
$config['height'] = 100;
$this->image_lib->initialize($config);
if (!$this->image_lib->crop()){
$error_msg = $this->image_lib->display_errors();
print_r($error_msg);
}
else {
echo "Done";
}
Here
$config['library_path'] = 'C:\\ImageMagick\\';
is the path for windows where your imageMagick application is installed.(Try to install in such a folder to which we can easily map it for library path).
Change the image library to :
$config['image_library'] = 'ImageMagick';
& other all configuration is remains same.

How to make CI image to thumbs

again I Throw a problems. How Can i find new_image (Thumb) name.
In Controller
//Insert image .............................
$config['upload_path'] = './img/placeholders/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
$this->upload->do_upload('blog_images');
//Thumb Image-----------------------------
$image_des = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './img/placeholders/blog/' . $image_des['file_name'];
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['new_image'] = './img/placeholders/blog/thumb/' . $image_des['file_name'];
$config['width'] = 220;
$config['height'] = 140;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image_dev = $this->upload->data();
// How Can i find new image name.
$data['blog_img'] = 'img/placeholders/' . $image_des['file_name'];
$data['blog_thumbs'] = ?
after convert,
blog_img name is "e7da891c90586f7c4e89ef5724891a15.jpg"
blog_thumbs is "e7da891c90586f7c4e89ef5724891a15_thumb.jpg"
How Can i find new_image (Thumb) name. like $image_des['file_name']
But you already have it right!
$image_des['raw_name'] = 'e7da891c90586f7c4e89ef5724891a15'; //File name without extension.
$image_des['file_ext'] = '.jpg'; //The file extension with period
$data['blog_thumbs'] = 'your/preceding/path/'.
$image_des['raw_name'].'_thumb'.$image_des['file_ext'];
Refer file uploading class for explanation about return array value of $this->upload->data().

how to create thumbnail from image datatype with codeigniter and sql server 2005 database?

I'm developing web application using latest codeigniter framework and SQL Server 2005. In my database, i have a table that have a column wth image datatype. From here I know how to retrieve that image with this :
$q = "Get_Picture_Test_SP #pk_rms_id=1443546";
$res = mssql_query($q);
$row = mssql_fetch_assoc($res);
$image = $row['picture'];
function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
return $r;
}
$image = hex2bin($image);
What i want to know is how can i make a thumbnail from that image to make the web loading more fast? If I use image from some path i know how to make the thumbnail (hope i not wrong) with this :
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
I still dont know how to creating thumbnail from image data type. Hope anyone can help. Thanks!
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
In your code your forget to initialize the config..So please use this code make sure the image path is correct..Hopefully this will work.

Resources