CodeIgniters Image Library
by Phil Hawthorne on Jul.01, 2010, under Development, PHP
Frameworks can make a developers life and job a lot simpler, but that doesn’t mean they don’t come with their own headaches and hours of frustration.
Tonight I’ve been coding an image uploading script using the CodeIgniter PHP Framework. This was the first time I’ve used the built-in image library to help me cut down my coding time.
Following the user guide at http://codeigniter.com/user_guide I was attempting to load an image that had been saved to the server, from the file uploading class. The example on the website uses the following example code:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
This code actually doesn’t work, and will result in an error “Your server does not support the GD function required to process this type of image”.
Why doesn’t it work?
As posted on the CodeIgniter Forums by user ‘gunter’, you shouldn’t loop through
$this->load->library('image_lib', $config);
Instead what you should do is:
$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();
(courtesy of a.somervell)
A lesson to show you that not even the documentation can always be correct. Hopefully this saves some people hours of pulling their hair out.
Tweet
May 7th, 2011 on 4:03 am
This is a well written article that I have bookmarked for future reference. Have a good one.
January 31st, 2012 on 11:22 pm
You know one would think they would update the docs, But the funny thing is now I have it creating the thumbs and still shows the error. I really thought I was enjoying Codigniter until I ran into this and file uploading. Anyway thanks for sharing much appreciated.