Reputation: 345
I am using the upload class that codeigniter comes with:
$config['upload_path'] = getcwd() . '/public/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $error = array('error' => $this->upload->display_errors());
}
else
{
echo $data = array('upload_data' => $this->upload->data());
}
It works fine uploading the file but now I would like to trim all the extra white space from the image. I looked at the image manipulation class but it does not seem to do it. So I looked around and found this Crop whitespace from image in PHP. I am unsure though how to put the two together. Any ideas?
Upvotes: 0
Views: 916
Reputation: 1
To use the ImageMagick you should add the function to the image_lib.php:
public function trimimg()
{
$protocol = 'image_process_'.$this->image_library;
return $this->$protocol('trimimg');
}
and then add to the function
public function image_process_imagemagick($action = 'resize')
{
// Do we have a vaild library path?
if ($this->library_path === '')
{
$this->set_error('imglib_libpath_invalid');
return FALSE;
}
if ( ! preg_match('/convert$/i', $this->library_path))
{
$this->library_path = rtrim($this->library_path, '/').'/convert';
}
// Execute the command
$cmd = $this->library_path.' -quality '.$this->quality;
if ($action === 'crop')
{
$cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
}
elseif ($action === 'rotate')
{
$cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
? ' -flop'
: ' -rotate '.$this->rotation_angle;
}
elseif ($action === 'trimimg')
{
$cmd .= ' -trim';
}
the last elseif.
Then
$this->image_lib->trimimg())
Upvotes: 0
Reputation: 2378
You're right, the image manipulation class doesn't support doing that.
You can, however, extend the library (bottom of http://codeigniter.com/user_guide/general/creating_libraries.html) and add a new method based on the code from the link you found already.
Here, I was bored at work, extend CI's image manipulation lib and add this method:
public function trim_whitespace($color = 'FFFFFF')
{
//load the image
$img = $this->image_create_gd();
//find the size of the borders
$b_top = 0;
$b_btm = 0;
$b_lft = 0;
$b_rt = 0;
//top
for(; $b_top < imagesy($img); ++$b_top) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, $b_top) != '0x'.$color) {
break 2; //out of the 'top' loop
}
}
}
//bottom
for(; $b_btm < imagesy($img); ++$b_btm) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != '0x'.$color) {
break 2; //out of the 'bottom' loop
}
}
}
//left
for(; $b_lft < imagesx($img); ++$b_lft) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, $b_lft, $y) != '0x'.$color) {
break 2; //out of the 'left' loop
}
}
}
//right
for(; $b_rt < imagesx($img); ++$b_rt) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != '0x'.$color) {
break 2; //out of the 'right' loop
}
}
}
//copy the contents, excluding the border
$newimg = imagecreatetruecolor(
imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));
imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));
// Output the image
if ($this->dynamic_output == TRUE)
{
$this->image_display_gd($newimg);
}
else
{
// Or save it
if ( ! $this->image_save_gd($newimg))
{
return FALSE;
}
}
}
Usage:
// load extended image lib
$this->load->library('image_lib');
// configure image lib
$config['image_library'] = 'gd2';
$config['source_image'] = 'path/to/source.img';
$config['new_image'] = 'path/to/output.img';
$this->image_lib->initialize($config);
$this->image_lib->trim_whitespace('38ff7e'); // set colour to trim, defaults to white (ffffff)
$this->image_lib->clear();
It's worth noting that even though it will work, you really shouldn't use this as a dynamic_output, do the trim on save otherwise it will just slow everything down. Also, it is looking for just 1 colour value (although, that is a limitation from the code you posted, there may be better functions for this out there) so if it's a compressed jpg you might have trouble getting all of the whitespace.
Upvotes: 1