Created
January 23, 2011 19:57
-
-
Save craigrodway/792382 to your computer and use it in GitHub Desktop.
Crops an image square.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function _process_image($data){ | |
| if($data['is_image'] == TRUE){ | |
| // It's definitely an image... | |
| // Work out essential dimensions | |
| $ori = ($data['image_height'] > $data['image_width']) ? 'portrait' : 'landscape'; | |
| $ratio = ($ori == 'portrait') | |
| ? $data['image_height'] / $data['image_width'] | |
| : $data['image_width'] / $data['image_height']; | |
| $thumb_size = 162; | |
| //$max_height = 344; | |
| //$max_width = 516; | |
| $this->load->library('image_lib'); | |
| // Step 1 - Shrink it to thumb size (one dimension) | |
| $config['source_image'] = $data['full_path']; | |
| $config['new_image'] = "./web/media/thumbnails/" . $data['file_name']; | |
| // Save filename for later cropping | |
| $sq = $config['new_image']; | |
| $config['maintain_ratio'] = TRUE; | |
| $config['quality'] = 90; | |
| if($ori == 'landscape'){ | |
| $config['height'] = $thumb_size; | |
| $config['width'] = $thumb_size * $ratio; | |
| } | |
| if($ori == 'portrait'){ | |
| $config['width'] = $thumb_size; | |
| $config['height'] = $thumb_size * $ratio; | |
| } | |
| $this->image_lib->initialize($config); | |
| if(!$this->image_lib->resize()){ | |
| $resize_errors[] = 'Resize: ' . $this->image_lib->display_errors('', ''); | |
| } | |
| $this->image_lib->clear(); | |
| unset($config); | |
| // Step 2 - Crop the shrunken image so it's square | |
| if($ori == 'portrait'){ | |
| $height = ($thumb_size * $ratio); | |
| $config['x_axis'] = 0; | |
| $config['y_axis'] = ($height - $thumb_size) / 2; | |
| } else { | |
| $width = ($thumb_size * $ratio); | |
| $config['y_axis'] = 0; | |
| $config['x_axis'] = ($width - $thumb_size) / 2; | |
| } | |
| $config['height'] = $thumb_size; | |
| $config['width'] = $thumb_size; | |
| $config['source_image'] = $sq; | |
| $config['maintain_ratio'] = FALSE; | |
| $this->image_lib->initialize($config); | |
| if(!$this->image_lib->crop()){ | |
| $resize_errors[] = 'Crop: ' . $this->image_lib->display_errors('', ''); | |
| } | |
| if(isset($resize_errors) && count($resize_errors) > 0){ | |
| // Errors - we failed. | |
| $this->_resize_errors = $resize_errors; | |
| return FALSE; | |
| } else { | |
| // All OK. | |
| return TRUE; | |
| } | |
| } else { | |
| // Not an image. How can that be?? Good job we checked. | |
| return FALSE; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment