php - Codeigniter Trim Image -


i using upload class 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 file trim white space image. looked @ image manipulation class not seem it. looked around , found crop whitespace image in php. unsure though how put 2 together. ideas?

you're right, image manipulation class doesn't support doing that.

you can, however, extend library (bottom of http://codeigniter.com/user_guide/general/creating_libraries.html) , add new method based on code link found already.

here, bored @ work, extend ci's image manipulation lib , add method:

public function trim_whitespace($color = 'ffffff') {     //load image     $img = $this->image_create_gd();      //find size of 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 '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 '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 '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 'right' loop             }         }     }      //copy contents, excluding 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 image     if ($this->dynamic_output == true)     {         $this->image_display_gd($newimg);     }     else     {         // or save         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 trim, defaults white (ffffff) $this->image_lib->clear(); 

it's worth noting though work, shouldn't use dynamic_output, trim on save otherwise slow down. also, looking 1 colour value (although, limitation code posted, there may better functions out there) if it's compressed jpg might have trouble getting of whitespace.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -