image resize in runtime with codeigniter(php) - php

I have uploaded an image with the resolution 1600x1200. How can I show users 60x60 and 200x200 without creating thumbnails and uploading again. (like phpthumb library) in CodeIgniter?

http://codeigniter.com/forums/viewthread/155086/
That's the timThumb controller for CI.

Codeigniter comes with its own image library which can resize images. This example is from the documentation, just edit the $config variable:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['new_image'] = '/path/to/new/image.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
You can also create thumbnails dynamically (that is, without saving them to the hard drive) but that requires a lot of memory so I would discourage that.
The image class can also crop the image if you need that. I use it to create thumbnails in the application I am currently working on...
Visit the documentation: http://codeigniter.com/user_guide/libraries/image_lib.html

Related

Codeigniter how to upload the resize image or thumb ONLY

I am new with CodeIgniter and want to resize the image first before saving to my database and local storage using move_uploaded_file i was able to resize using below code but how to I access the resized image and upload it using move_uploaded_file on PHP
$uploaddir = './images/post/';
$uploadfile = $uploaddir . basename($value1."-".$date."-".$_FILES['file']['name']);
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadfile;
$config['create_thumb'] = TRUE;
$config['width'] = 350;
$config['height'] = 1;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
Any suggestion would be great.
You should understand that essentially what you are doing is uploading the file to the temporary directory and using the locally stored file to generate a resized image.
So for better or worse your image is already on your server. Thus:
Your order of operations is wrong. You need to use the upload library to first bring the image on to your server, and then you can use that file location instead of the temporary name, to resize the image. With Codeigniters image lib you can make it so that the resized image overwrites the original. To do so you change $config['create_thumb'] = false; and resize will target the source/original image.
More on that setting here: https://www.codeigniter.com/userguide3/libraries/image_lib.html#CI_Image_lib::resize
If you really want to go your route (which isn't great because you don't have the protections the upload library offers) turn $config['create_thumb'] to false as I've said before OR note that the thumb file should be the temp name + _thumb + whatever extension.
See thumb marker here: https://www.codeigniter.com/userguide3/libraries/image_lib.html#preferences
When user submits a file(form must have enctype="multipart/form-data" attribute for this to work):
file is being uploaded to configured server directory upload_tmp_dir
uploaded file properties are stored in $_FILE array: original name, mime type, temporary name (documentation)
after script ends temporary file is being deleted
So, if you want to store only resized image, you can process directly temporary file using absolute path in $_FILES['file']['tmp_name'] property.

How can i select whole folder with pictures in codeigniter?

I have a project in which i need to select whole folder with pictures inside it, and then do a transformation on all of the pictures inside the folder. Something similar like scripts in photoshop for massive pictures adjustment.
Check PHP's scandir() function
Loop through the results of your directory and do your image manipulation on each item.
Well, CodeIgniters Manual does have some helper functions, but I'm not using them myself.
Personally, I'm more comfortable using the DirectoryIterator PHP supplies you with. Combining this with CodeIgniters Image Manipulation Class you can achieve some pretty nifty handling of your image processing:
$images = array("jpg", "png", "bmp"); // Just to make sure that we get image files
foreach(new DirectoryIterator("/path/to/images/") as $file)
{
if( $file->isFile() && in_array($file->getExtension(), $images) )
{
$config['image_library'] = 'imagemagick';
$config['library_path'] = 'usr/bin/local/';
$config['source_image'] = $file->getPathname();
$config['new_image'] = $file->getPathname(); //Overwriting the source image
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
Or do some image processing of your own choice.
If you are using linux, you can mass edit the images using ImageMagick's mogrify command.
Example: mogrify -resize 150x150 *.jpg
Result: all images were resized to 150x150
There is also a PHP class that deals with ImageMagick functionality in PEAR.
I don't think there is a function that could perform multiple image processing. But you could select files with glob() and then do your process in a loop ?

How can i remove black background color when creating thumbnail

When I am creating thumbnail in codeigniter I get an image with black background color.
Why did it happen?
This is the code in controller:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $file ;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->md_image->resize_image('./images/logo_images/'.$filename,380,80,'./images/logo_images/thumbs/'.$filename);
It is a PHP and GD2 problem, and not taken into account in the Image Lib in CodeIgniter.
Have a look here in the PHP manual to be able to keep transparency on resize :
http://www.php.net/manual/en/function.imagecolortransparent.php
Stéph

Codeigniter Blue Hue

UPDATE: THE ANSWER IS USE THE GD2 LIBRARY, DOH!
I am working with Codeigniter's image manipulation library to resize some photos. Unfortunately, they are producing a blue tint or hue to the photos. Not sure why this is and needed to see if it was something I am doing. Here is the code I am using to create the thumb's. Let me know if you want to see image links, and I will upload them somewhere.
$this->load->library('image_lib');
$config['image_library'] = 'GD';
$config['source_image'] ="images/IMG_0007.jpg";
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = '450';
$config['height'] = '450';
$this->image_lib->initialize($config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
I strongly recommend you to use ImageMagick for image resizing. It respects color profiles, is slightly faster, less memory hungry and generally produces better quality. See this question:
How to stop GD2 from washing away the colors upon resizing images?
If you do not have ImageMagick installed, this may be of help:
http://ferdychristant.com/blog//archive/DOMM-8GAFGL

Resizing Images with Codeigniter

Hey :) I want to resize an image when I address it in a form(save to database). I dont think it will be a problem [pointing to the image etc, however the space I am going display the thumb in is 228 x 228. I know I can maintain the ratio and determine the size in the controller, yet how is it done on images that are frequently different sizes?
Is the best way to address this is to put pre-calculated values(make my own ratio) in my form so that the controller can use those values to determine max height and width? See code.
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75; // use $this->input->post('width') instead??
$config['height'] = 50; // use $this->input->post('height')
Or is there a way to tell the function no bigger than 228 and no wider than 228?
Thanks
I am not sure what you want exactly, but by using the following settings you can make sure either the width or the height of the image is 228px. If width > height, width will be 228px. And vice versa.
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto' // auto is default, so you can leave this out
$config['width'] = 228;
$config['height'] = 228;
master_dim
Specifies what to use as the master
axis when resizing or creating thumbs.
For example, let's say you want to
resize an image to 100 X 75 pixels. If
the source image size does not allow
perfect resizing to those dimensions,
this setting determines which axis
should be used as the hard value.
"auto" sets the axis automatically
based on whether the image is taller
then wider, or vice versa.
If this is not what you mean, please add a bit more explanation.
getImageSize()
I think you'd have to throw some logic in your controller to handle this. Using the function above you can test the height/width of the uploaded image and resize if necessary.

Categories