Codeigniter file upload class - image

What would be a better way.
I have a form which accepts a certain width and height.
Say,
$config['max_width'] = '270';
$config['max_height'] = '280';
Would it be better to accept larger sized images and then just resize them to those after uploading rather than restricting them in the first place?

CI have their own library for image re-size. So you should accept any image size. Then you can resize it with below code.
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/img_name.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 270;
$config['height'] = 280;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
source: http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Yes it would be better to give your users more flexibility in file upload options.
This is a great class for resizing images https://github.com/Nimrod007/PHP_image_resize

Related

resizing image using gd2 in codeigniter

I am trying to resize an uploaded image from Codeigniter panel to the server, in this process when i try to overwrite the image i get a black background with the resized image in it (image attached) , please help me with this
My code is as follows :
move_uploaded_file($_FILES['img']['tmp_name'],"img".".".$extension);
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = False;
$config['overwrite'] = TRUE;
$config['width'] = 1000;
$config['height'] = 1000;
$config['quality'] = 100;
$config['source_image'] = "img".".".$extension;
$config['new_image'] = "img".".".$extension;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
Also is there any other way i can directly take the temp image and save with compression?
Thank You,
Rebecca
Please go through below, It will help your issue.
You can create another folder or new name for resize image. You are facing issue because while resizing image, the source image already in use. Hence either create new folder or change filename.
Create New Folder
$config['new_image'] = "path/to/new/folder/img".".".$extension;
Change New Image(Resized) name
$config['new_image'] = "newimg".".".$extension;
Let me know if it not works to you.
Please go through below running solution as per your requirement.
if ($this->input->server('REQUEST_METHOD') === 'POST'):
$this->load->library('image_lib');
$path_parts = pathinfo($_FILES["file"]["name"]);
$extension = $path_parts['extension'];
move_uploaded_file($_FILES['file']['tmp_name'], "./uploads/img" . "." . $extension);
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = False;
$config['overwrite'] = TRUE;
$config['width'] = 200;
$config['height'] = 200;
$config['quality'] = 100;
$config['source_image'] = "./uploads/img" . "." . $extension;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
endif;

CodeIgniter not saving image

I am trying to fetch a picture of a bear from the URL and save it as aabear.jpg. However, my code is failing. What is wrong?
$config['image_library'] = 'gd2';
$config['source_image'] = 'http://thejuxtapositionape.blog.com/files/2012/03/polar-bear-pup.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = 'aabear.jpg';
$this->load->library('image_lib', $config);
Seems like the image library only takes local files as inputs.
Using polar-bear-pup.jpg worked out

Codeigniter Image resize issue

I can't re-size a large uploaded image into tow copies one in another size and the other is in another size.
I can only resize only one image.
What I want to ask is. I want to creat a thumbnail as well as slighter larger image of a user profile photo.
if(move_uploaded_file($_FILES['profilepic']['tmp_name'], './profile_pix/'. $profile_pic_name1)){
$config['image_library'] = 'gd2';
$config['source_image'] = './profile_pix/'. $profile_pic_name1;
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 190;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$thumb_name = './profile_pix/thumb_'.$profile_pic_name1;
copy('./profile_pix/'. $profile_pic_name1, $thumb_name);
$config2['image_library'] = 'gd2';
$config2['source_image'] = $thumb_name;
$config2['create_thumb'] = false;
$config2['maintain_ratio'] = false;
$config2['width'] = 50;
$config2['height'] = 50;
$this->load->library('image_lib', $config2);
$this->image_lib->resize();
}
It seems that your image library is already loaded. so call the initialize() function instead of load()

Codeigniter - Create Thumbnail Woes

I am trying to create a thumbnail of a watermarked image, but can't get it to work. The script always seems to resize the original watermarked image without making the thumbnail.. anyway to accomplish both?
Here is my code
$config = array();
$config['source_image'] = $data['json']->{'file_path'};
$config['new_image'] = 'copy_' . $data['json']->{'file_path'};
$config['image_library'] = 'gd2';
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = getcwd() . '/design/new_transparency.png';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_vrt_offset'] = 20;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 125;
$config['height'] = 125;
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$this->image_lib->clear();
$this->image_lib->resize();
is there something I am doing wrong?
In the image manipulation class (user guide):
http://codeigniter.com/user_guide/libraries/image_lib.html
I need to make several sizes of thumbnails in my application, so instead of using the built in thumbnail functionality I just use:
$this->image_lib->clear();
And then use the resize command.

How can I pad an image with CodeIgniter?

My user is uploading an image of any size XX x YY. I want to find the larger dimension and shrink the image to a square of 250x250 with transparent padding being added to make up the difference. Is there any way to accomplish this with CI's Image Lib?
Try this:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = FALSE;
$config['width'] = 250;
$config['height'] = 250;
$this->image_lib->initialize($config);
$this->image_lib->crop();
For details, see the Image Manipulation Class from the CodeIgniter API
Although to be quite honest, I don't find CI's image manipulation library to be adequate. It isn't very flexible, it isn't very fast for repeated or combined actions (like the one above), and it uses an atrocious syntax (that may come in handy for certain types of applications, but I personally prefer something like PHP Thumb):
In fact, I recently put together a simple wrapper library for PHP Thumb (download) to use with CodeIgniter, and it works fabulously. Using the PHP Thumb library instead of CodeIgniter's own, you could get the job done like so:
$this->load->library('phpthumb_lib');
$thumb = phpthumb_lib::create('/path/to/image/mypic.jpg');
$thumb->adaptiveResize(250, 250);
$thumb->save();
More details on PHP Thumb's GitHub page.
Only this doesn't pad the image with transparent pixels (which I would consider a bad practice), it merely resizes and crops the image to fit the whole 250x250 pixel area without stretching the image.
Alternatively, you could make it resize the image to fit inside a 250x250 pixel box (by using resize rather than adaptiveResize), and then place the images inside 250x250px DIV containers. I'm guessing this is probably the solution you'll want.
I do not crop the image, rather place it in the background of a div. Here is my code.
1) Inside the helper place this.
function getSmallerDimension($img_path, $width, $height) {
list($wid, $hei, $type, $attr)= getimagesize($img_path);
$ratio = $wid / $hei;
if( $height < $width/$ratio ) {
return 'width';
}
else if( $width < $height * $ratio ) {
return 'height';
}
else {
return 'auto';
}
}
2) Use this to resize.
$config['image_library'] = 'gd2';
$config['source_image'] = 'source-path/source.jpg';
$config['create_thumb'] = true;
$config['thumb_marker'] = '-thumb';
$config['maintain_ratio'] = true;
$config['width'] = 250;
$config['height'] = 250;
$config['master_dim'] = getSmallerDimension('source-path/source.jpg', 250, 250);
$this->load->library('image_lib', $config);
$this->image_lib->resize();

Resources