i have a problem with resizing image in CI ( i'm new to CI by the way ).
so here is my code :
$config['image_library'] = 'gd2';
$config['source_image'] = base_url()."/uploads/test.png";
$config['maintain_ratio'] = TRUE;
$config['width'] = 800;
$config['height'] = 600;
$config['new_image'] = base_url().'/uploads/resized.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
echo $config['source_image'];
it give me this error :
The path to the image is not correct. Your server does not support the
GD function required to process this type of image.
i'm using MAMP as a server and i see GD enabled on phpinfo.
i also try to echo the image url , and yes its really there.
please help.
Thanks
You have to use relative path and you can try with this one
$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();
Please see this one
Related
we are creating an application which allows image upload, now we want to reduce the image size to reduce the page load time, now this is the code i'm using, it doesn't work, image size remains 100% it doesn't reduce, please help maybe i'm missing something or i'm doing something wrong
if(!empty($_FILES['image_field']['name']))
{
$config['upload_path'] = 'image_folder';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '262144';
$config['file_name'] = 'my_image_name';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image_field'))
{
$this->session->set_flashdata('error','<div class="alert alert-warning">Something went wrong...try again...</div>');
redirect('Controller/function_name');
}
else
{
$type = array('image/png'=>'png','image/jpg'=>'jpg','image/jpeg'=>'jpg');
$config['image_library'] = 'gd2';
$config['source_image'] = base_url().'image_folder'.$config['file_name'].'.'.$type[$_FILES['image_field']['type']];
$config['quality'] = '50%';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// save to database
}
}
i'm using codeigniter 3.0
You should use without % as integer like that:
$config['quality'] = 50;
$config['image_library'] = 'gd2';
$config['quality'] = 60;
works great adjust the quality and check the image file size it save. will be different.
I also had to set my image_library parameter to "gd" before the quality change started working. I wonder why it didn't work with "gd2"
$config['image_library'] = 'gd';
$config['quality'] = 40;
I found this would not work for me using the GD or GD2 library.
In order for the "quality" to be affected - I needed to use the "ImageMagick" Library.
As stated in the documentation - if you use either the "ImageMagick" or the "NetPBM" library - "you must supply the path": https://codeigniter.com/userguide3/libraries/image_lib.html
On my GoDaddy server the path was "/usr/bin"
$config['source_image'] = $source_image;
$config['library_path'] = '/usr/bin';
$config['image_library'] = 'ImageMagick';
$config['maintain_ratio'] = TRUE;
$config['quality'] = "20%"; // Maintained the aspect ratio and dimensions but reduced the file size by about 60%
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
echo "QUCKVIEW Re-sizing - ";
echo $this->image_lib->display_errors();
echo "Image Resize for Grooming Quickview Failed! Contact System Administrator.";
die();
}
for($i=0; $i< count($data['upload_data']); $i++){
//resize uploade image
$config['image_library'] = 'gd2';
$config['source_image'] = $data['upload_data'][$i]['full_path'];
$config['new_image'] = $data['upload_data'][$i]['full_path'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 700;
$config['height'] = 700;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
I want to resize the images in a loop, all the images are already in the database, but after running this script, only the first image is resized.
All the path are correct, anyone else encountering this problem?
After loading image_lib you might need to initialize it.
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
See: https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html
You will NOT need to use the $this->image_lib->initialize function if
you save your preferences in a config file.
Hi all : ) Thank you in advance for any assistance with this question.
I am using CodeIgniter and have images uploading, rotating and resizing successfully.
It is when I try to create multiple sizes fro an image that I am a little lost.
Here is my code:
// First I make two copies of the origional image - there is no problem here - these always work. I always have the successfully copied images.
$Copy_Mid_Size_File = "Some_Path_For_The_Newly_Copied_Mid_Size_Image"
if(!copy($source_image, $Copy_Mid_Size_File)){
echo "failed to copy $Copy_Mid_Size_File - Please contact the system administrator. \n";
die();
}
$Copy_Thumbnail_Size_File = "Some_Path_For_The_Newly_Copied_Thumbnail_Size_Image"
if(!copy($source_image, $Copy_Thumbnail_Size_File)){
echo "failed to copy $Copy_Thumbnail_Size_File - Please contact the system administrator. \n";
die();
}
// Now I resize for the mid size image
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Mid_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Mid Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Mid-Size Re-size Worked!";
}
// Now I try to resize for the Thumbnail
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Thumbnail_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Thumbnail Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Thumbnail Re-size Worked!";
}
I always end up with the proper number of images correctly named - The thumbnail image just does not re-size - there is no error reported. It always says that is succeeded.
If I put the thumbnail re-size code first - the thumbnail re-sizes correctly - but then the mid-size image does not.
I realize there are other ways to load the libraries - but I don't see why the first re-size works but the second does not.
Any ideas?
Thanks.
Regards,
Ken
You only need to load the library once, but then you need to initialize it with the new config each time. So your code will look like this:
// load the library
$this->load->library('image_lib');
// first image:
// set some config
$config['image_library'] = 'imagemagick';
...
// initialize the library with this config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
// clear the config:
$this->image_lib->clear();
// second image:
// set some config
$config['image_library'] = 'imagemagick';
...
// re-initialize the library with the new config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
I suspect you could even just update $config['width'] and $config['height'] without having to clear, but I've not tested that!
As a side note, you don't really have to copy the image into the correct directories first; the CodeIgniter image library can create a new one for you, if you set your config like this:
$config['new_image'] = '/path/to/new_image.jpg';
This will save the image into the new path without having to create copies all over the place.
Reference: http://codeigniter.com/user_guide/libraries/image_lib.html
I am working on a script that downloads a file from Dropbox, supposed to resize that image and then shoot it up to an S3 bucket.
For some reason, I can't get the image to resize.
I keep getting the following error:
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
Code Base:
public function resize_test() {
$postcard_assets = $this->conn->getPostcardDirContent("folder_name", "Photos", TRUE);
foreach($postcard_assets['contents'] as $asset) {
$file = pathinfo($asset['path']);
$original_file = $this->conn->downloadFile($asset['path']);
$raw_file = sha1($file['basename']);
$s3_file_name = "1_{$raw_file}.{$file['extension']}";
$this->resize_photo($original_file);
$this->s3->putObject($s3_file_name, $original_file, 's3-bucket-name', 'public-read');
$s3_check = $this->s3->getObjectInfo($s3_file_name, 's3-bucket-name');
if($s3_check['content-length'] > 0) {
krumo($s3_check);
exit();
}
}
}
private function resize_photo($photo) {
$config['image_library'] = 'imagemagick';
$config['source_image'] = $photo;
$config['maintain_ratio'] = TRUE;
$config['width'] = 640;
$config['height'] = 480;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
}
Dropbox API DownloadFile:
public function downloadFile($file) {
$this->setTokens();
return $this->conn->getFile($file);
}
Anyone know what I might be doing wrong?
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
I'm doing image resizing with CI using ImageMagick just like you are. You need the following to get this to work:
imagemagick should be installed. You can test it from the command line using the 'convert' command
imagick needs to be installed, this is the PHP library that binds to imagemagick
ImageMagick itself depends on various other libraries such as libjpeg and libpng. Make sure those are installed as well
Simpy do a phpinfo() and scroll down to 'imagick'. Check whether it is there and then check the 'supported file formats' heading to see if the file type you are wanting to resize is there.
If all of the above are correct and it still does not work, you should not forget to include the path to imagemagick in your code:
$config['library_path'] = '/usr/local/bin';
I went through all this pain before so I hope this helps you :)
For Multiple resize below code worked for me.
$config['create_thumb'] = FALSE; //to avoid _thumb prefixing
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;
$config['new_image'] = 'thumb_250x250_'.$file_name; // new name
$CI->load->library('image_lib', $config, 'abc'); //abc to avoid instance caching.
$CI->abc->resize();
unset($CI->abc); //unsetting instance.
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = 'thumb_100x100_'.$file_name; // new name
$CI->load->library('image_lib', $config, 'xyz'); // xyz to avoid instance caching.
$CI->xyz->resize();
unset($CI->xyz); // unsetting instance.
You need to use $config['new_image'] = '/path/to/new_image.jpg'; in your resize_photo function.
Read http://codeigniter.com/user_guide/libraries/image_lib.html
Actually you are trying to load the image library twice. Since you also initialize the config array on the very same line, the array never gets loaded into the library.
Change your code to this:
//this
$this->load->library('image_lib', $config);
//to this
$this->load->library('image_lib');
$this->image_lib->initialize($config);
and it will work perfectly.
See if you can actually open the original saved image before you try to resize it. I was decoding a base64 uploaded image while using a preg_replace. For some reason, which I still can't track down... it was removing like so
$file = preg_replace('/data.*base64,/', '', chunk_split($this->post('myimg'));
it would return this: [removed]/9....etc. which when base64 decoded... obviously isnt a valid image file.. so the resize wouldnt work. I had to add a
$file = substr($file,9);
to then remove the [removed]. extra work and took me while to figure out, but now I can resize images.
Side Question... Why is preg_replace adding [removed]??? Sigh... php.
//Make controller named "image.php"
class Image extends CI_Controller {
public function index($width, $height, $image_path)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/'.$image_path;
$config['dynamic_output'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
echo $this->image_lib->resize();
}
}
?>
//Call from view page
<img src="<?php echo ("index.php/image/index/150/150/".$luserdata[0]'profile_image']);?>" alt="resized mage1"/>
Following is the code which resize the image, but here i am not able to resize the image
function processHome(){
$this->load->library('image_lib');
$img_path = base_url().'img/image/50X50/ori.jpeg';
$config['image_library'] = 'gd2';
$config['source_image'] = $img_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
echo "No error";
exit;
$this->load->view('index', $data);
}
For a start, remove the
$this->load->library('image_lib');
at the beginning - you only need to load the library once, and only after you set the parameters.
Then, give the relative/server path to your image folder, instead of the url - as in, no base_url().
Finally, check the permissions for the folder the image is in - it must be readable/writable by all, and php should be allowed to create new files.
I guess that's all.