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.
Related
I'm trying to convert a user-uploaded PDF file to .jpg, to create some sort of thumbnail. I want the thumbnail to be displayed on the same page once they upload their PDF.
I'm using Imagick.
Here's some of my code:
$filePath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$pdfThumb = new Imagick();
$pdfThumb->setResolution(300,300);
$pdfThumb->readImage($filePath . '[0]');
$pdfThumb->setImageFormat('jpeg');
$fp = $fileName . '.jpg';
$pdfThumb->writeImage($fp);
I don't feel like anything is really happening, and I want to make sure it saves the thumbnail to my server (possibly temporarily) and show it on a div. Any tips would be much appreciated!
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'm letting users upload image files from their desktop and then using the PHP image routines to create thumbnails and resize images for the user's directory. But this doesn't seem to work at all for an animated gif. imagecreatefromgif(), for example, seems to peel off the first image of the animation and only work with it. Do I need to completely bypass these functions to get the complete uploaded animated gif to the user's directory?
Thanks
You cannot resize an animated gif image in the way you easily do with jpeg, bmp or other image formats. The function imagecreatefromgif() is not going to help you by any means, so take a look at the below link.
http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html
It should help you solve your problem.
To upload animated gif images I use this script:
$fileinfo = PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"../avatar/" . $newFilename);
$location="avatar/" . $newFilename;
It is simple and may not be a very functional one, but with animated gifs I have no problems.
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 ?
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.