I want to resize the uploaded image as the ratio of 400 x 350. I use below code for this.
$config = array(
'image_library' => 'gd2',
'source_image' => $img_data['full_path'],
'new_image' => $this->upload_path. '/thumbs',
'maintain_ration' => FALSE,
'width' => 350,
'height' => 400
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Codeigniter give me the image as the ratio of 400 x 340.
see if this could help.
Here is the link to the class required to accomplish the task you looking for
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
Related
I am using CKFinder PHP 3.5.3.
When I upload an image, it creates only one default 150x150 thumbnail currently.
I tried the following configuration of a thumbnail on the config.php, but it doesn't work:
$config['thumbnails'] = array(
'enabled' => true,
'sizes' => array(
array('width' => '150', 'height' => '150', 'quality' => 80),
array('width' => '300', 'height' => '300', 'quality' => 80),
array('width' => '500', 'height' => '500', 'quality' => 80),
),
'bmpSupported' => true,
);
I would like to create multiple thumbnails such as 150x150, 300x300 and 500x500 when upload an image.
How can I generate multiple thumbnails when I upload an image?
In CKFinder 3 thumbnails of different sizes are generated lazily, on demand. This means the thumbnail of a given size will not be generated until requested by CKFinder's frontend. It's possible to change this behavior with a plugin. Here you can find a plugin I have created that enforces CKFinder to generate all the private thumbnails immediately after the file is uploaded.
I am using CI version 3.1.2 to process an image and using Windows 8. My problem is every time i process an image and trying to create a thumbnail, i am countering a message "Unable to save the image. Please make sure the image and file directory are writable". This is occurred when trying to save the copy of thumbnail into different location. Here are my codes processing the image:
$this->original_path = realpath(APPPATH.'../assets/galery/full');
// Uploading image
$config = [
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->original_path,
'max_size' => 2048,
'maintain_ratio' => true,
'file_name' => 'img_'.strtotime(date('d-m-y-H-i-s')), // miss type
'overwrite' => true,
'remove_spaces' => true
];
$this->load->library('upload', $config);
$this->upload->do_upload('galery');
$file_name = $this->upload->file_name;
// Processing image, success processing image
$this->load->library('image_lib');
$img = [
'image_library' => 'gd2',
'source_image' => $this->original_path.'/'.$file_name,
'maintain_ratio' => true,
'width' => 600
];
$this->image_lib->initialize($img);
$this->image_lib->resize();
// This is fail, with message "Unable to save the image... using(var_dump())
$thumb = [
'source_image' => $this->original_path.'/'.$file_name,
'new_image' => '/assets/galery/small',
'maintain_ratio' => true,
'width' => 200
];
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
I've tried different config, like using realpath(APPPATH.'../assets/galery/small'), but still no luck. It is success, by the way, when create config using 'create_thumb'.
But my purpose is, saving that thumbnail in another folder. How can i achieve that?
UPDATE
This change seem to work well. I change realpath(APPPATH.'../assets/galery/full') to realpath(APPPATH.'../assets/galery') and make several change in:
...
'upload_path' => $this->original_path.'/full'
...
'source_image' => $this->original_path.'/full/'.$file_name
...
'new_image' => $this->original_path.'/small/thumb_'.$file_name
Until now i'm still trying to make my code simple and put it in MY_Model. Thanks for all comment(s). If some one may have better solution, it would be nice to share here.
I'm running over this problem which I was trying for the last few hours
I'm having an image upload with some details to store in db.
I store the details and image path, working like a charm. Now comes the edit part.
I'm trying to check if the input file is empty, if so update just the details, else delete the image and reupload new image. The problem is this:
If the input file is empty it updates everything no problem, if is not empty it is updating the details, but the image is the same, doesn't get deleted or reuploaded.
here is the code
$image_input = $this->input->post('image');
if(isset($image_input) && !empty($image_input))
{
$this->db->where('id', $id);
$img = $this->db->get('menus_category', 1);
if($img->num_rows() > 0)
{
$row = $img->row();
$original_image = $row->image;
$desktop_image = $row->desk_img;
$mobile_image = $row->mob_img;
$thumb_image = $row->thumb;
$unlink_image = unlink('./uploads/menus/' . $original_image);
$unlink_desk = unlink('./uploads/menus/desk/' . $desktop_image);
$unlink_mob = unlink('./uploads/menus/mobile/' . $mobile_image);
$unlink_thumb = unlink('./uploads/menus/thumbs/' . $thumb_image);
if($unlink_desk && $unlink_image && $unlink_mob && $unlink_thumb)
{
$config = array(
'upload_path' => './uploads/menus',
'allowed_types' => 'gif|jpg|jpeg|png',
'max_size' => '15000'
);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$image_data = $this->upload->data();
$this->load->library('image_lib');
// thumb resize
$thumbnail = 'thumb_' . $image_data['file_name'];
$thumb = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'thumbs/' . $thumbnail,
'maintain_ratio' => TRUE,
'width' => '90',
'height' => '90'
);
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
$this->image_lib->clear();
// mobile resize
$mob = 'mob_' . $image_data['file_name'];
$thumb_mob = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'mobile/' . $mob,
'maintain_ratio' => FALSE,
'width' => '290',
'height' => '83'
);
$this->image_lib->initialize($thumb_mob);
$this->image_lib->resize();
$this->image_lib->clear();
// desktop resize
$desk = 'desk_' . $image_data['file_name'];
$thumb_desk = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'desk/' . $desk,
'maintain_ratio' => FALSE,
'width' => '700',
'height' => '200'
);
$this->image_lib->initialize($thumb_desk);
$this->image_lib->resize();
$this->image_lib->clear();
// insert path and details to database
$data = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['title']),
'description' => $input['description'],
'image' => $image_data['file_name'],
'desk_img' => $desk,
'mob_img' => $mob,
'thumb' => $thumbnail
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data);
}
else
{
echo $this->image_lib->display_errors();
}
}
}
}
else
{
$data2 = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['slug']),
'description' => $input['description']
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data2);
}
Note: the else statement works fine, but the first if the problem. Now I changed the if to just if(isset($image_input)) ... and if file input is not empty is reuploading the picture and updating the details fine, but if I update only the details with no picture, it is deleting the picture that is already uploaded and is not updating. (I think this is the problem but I can't figure out how to fix it).
If you will to give me some help or to put me on right direction I will be thankful.
Thanks guys
First, this would be a comment asking a question, but I don't have enough reputation do add one just yet...
If you could post your HTML form, that would be helpful, but without that...
1) I assume the form you are submitting has enctype="multipart/form-data" in the opening tag.
2) On line 2 of your code here, $this->input->post('image'), is 'image' from a form input of type file? If so, your statement won't work as 'image' is part of the $_FILES array and not $_POST anymore. Meaning when you go to check it, it is always going to be empty (or non-existent).
To verify that your form is submitting the way you think it is, do this just before the first line in the code you have in your post:
var_dump($_POST);
var_dump($_FILES);
exit; // don't let anything run after the dumps.
let me know if that puts you in the right direction or not.
I am using CodeIgniter to resize image, but the small image I have resized is low quality.
Here is my code:
$config = array(
'image_library' => 'gd2',
'quality' => '100',
'source_image' => $temp_full_path,
'new_image' => $full_temp_path."img_mobile/",
'maintain_ratio' => true,
'create_thumb' => false,
'width' => 50,
'height' => 50
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
How can make resize image with high quality like it's original image?
Here is original image, image resize generate from php code and image resize generate from codeigniter: Image
Looking at the source code, GD2 doesn't use the "quality" parameter.
But some of the other libraries do use it.
Give ImageMagick a try.
You should write 'quality' => '100%' instead of only '100'
Default:
$config['image_library'] = 'gd2';
Alternative:
$config['image_library'] = 'gd';
$config['quality'] = 100;
Best Quality:
$config['image_library'] = 'imagemagick';
$config['library_path'] = "/usr/bin";
$config['quality'] = 100;
Ref: https://forum.codeigniter.com/thread-17374.html
It's a small mistake or a pitfall.
Do:
'quality' => '100%',
instead of
'quality' => '100',
Should work, according to docs.
EDIT:
There seems to be another typo!
'maintain_ration'
should be
'maintain_ratio'
Also, how can new_image end with a /? you are doing something wrong there.
I have two resize functions that I would like to perform one after the other using codeigniters image manipulation class:http://codeigniter.com/user_guide/libraries/image_lib.html
At the moment when I try running the function only the first one works, it ignores the second.
I have added $this->image_lib->clear() which according to codeigniters user guide: 'resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.'
Why can't I run the two seperate resize functions? How would I go about doing this??
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $upload_path . '/thumbs/',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
$config = array(
'source_image' => $image_data['full_path'],
'maintain_ration' => true,
'width' => 620,
'height' => 410
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
If you don't want to use clear then store configs in two different arrays.
$this->image_lib->clear(); is used to flush previous settings & to add new.