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 ?
Related
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.
I am looking to accomplish this in CodeIgniter specifically.
The PHP App I am coding allows a user to upload either a jpg or an animated gif image. On the next step I want to allow the user to use jCrop to crop a few different size thumbnails. This would require me to convert a new copy of the animated gif to a jpg. My code works fine with uploaded jpg images, but creates a broken image for gif files. Is what I am trying to do possible?
My Code:
// Create image to crop
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $this->config->item('upload_dir_path') . $file_path . 'original.' . $file_ext;
chmod($config['source_image'], 0777);
$config['new_image'] = $this->config->item('upload_dir_path') . $file_path . 'crop-me.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
For those interested in my solution, I simply used the built in GD PHP functions. I have little experience dealing with gif files so I was expecting this to be difficult. The fact of the matter is the CodeIgniter Image_lib and extended library (Which I never got to work properly) is overkill for this.
Here is what I used:
$image = imagecreatefromgif($path_to_gif_image);
imagejpeg($image, $output_path_with_jpg_extension);
Very easy and worked perfectly for what I needed.
This is my image uploading and resizing code in codeigniter.But in my client's server the image is uploaded but not create a resized image into a folder
if($imgwidth >= 1025 && $imgheight >= 650)
{
$epld=explode('.',$ex);
$filename=date("mdyHis").".".$epld[1];
$uploaddir = './bg_images/';
$file = $uploaddir . basename($filename);
if(move_uploaded_file($_FILES['file']['tmp_name'],"./bg_images/".$filename))
{
$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('./bg_images/'.$filename,1025,650,'./bg_images/thumbs/'.$filename);
}
}
The bg_images is the folder name and the md_image is the library used for resizing
I can not spot any errors in your code.
Make sure you have gdlib installed by uploading a script with the following content:
<?php
echo phpinfo();
?>
The Script shows your php configuration.
Search for "gd" with your browser's search function. No mention of "gd" would mean that you are missing this library, but you need it for image manipulation.
does the ./bg_images/thumbs/ folder exist on the file system?
As far as I remember, gdlib would not create folder itself. If the folder is missing, create it yourself (remember to give it the right permissions, the server / php process needs write permission (most likely www-data))
If all this doesn't help, ask your client's admin for access to the error logs.
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
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