Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Codeigniter Image Manipulation Class : Resize and Crop on multiple files
- resize_img()
- crop_img().
- <?PHP
- class Imagetest extends MY_Controller {
- function __construct()
- {
- parent::__construct();
- $this->load->library('image_lib');
- }
- function index()
- {
- $testimg1 = 'uploads/test/1.png';
- $testimg2 = 'uploads/test/2.png';
- $testimg3 = 'uploads/test/3.png';
- $this->resize_img($testimg1);
- $this->crop_img($testimg1);
- $this->resize_img($testimg2);
- $this->crop_img($testimg2);
- $this->resize_img($testimg3);
- $this->crop_img($testimg3);
- }
- function image_thumb_name($img = null)
- {
- if(!empty($img)) {
- $exploded = explode('.', $img);
- return $exploded['0'] . '_thumb.' . $exploded['1'];
- }
- }
- function get_axis($img)
- {
- // Default values
- $output['x_axis'] = 0;
- $output['y_axis'] = 0;
- // Settings
- $config['height'] = 132;
- $config['width'] = 176;
- if ($img_dim = getimagesize($img)) {
- list($thumb_width, $thumb_height) = $img_dim;
- } else {
- echo '<h1> ERROR HERE TOO</h1>';
- return false;
- }
- if ($thumb_width > $config['width']) {
- $output['x_axis'] = (($thumb_width - $config['width']) / 2) ;
- } else if ($thumb_height > $config['height']) {
- $output['y_axis'] = (($thumb_height - $config['height']) / 2);
- }
- return $output;
- }
- function resize_img($img)
- {
- $config = array();
- echo 'Processing: '. $img .'<br/>'; // Debug
- if ($img_dim = getimagesize($img)) {
- list($image_width, $image_height) = $img_dim;
- } else {
- echo '<h1> ERROR HERE </h1>';
- }
- // create a thumbnail
- $config['image_library'] = 'GD2';
- $config['source_image'] = $img;
- $config['quality'] = 100;
- $config['height'] = 132;
- $config['width'] = 176;
- $config['create_thumb'] = TRUE;
- $config['maintain_ratio']= TRUE;
- $config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
- $this->image_lib->initialize($config);
- if (!$this->image_lib->resize()) {
- echo $this->image_lib->display_errors();
- }
- echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug
- $this->image_lib->clear();
- unset($config);
- }
- function crop_img($img)
- {
- $config2 = array();
- // Crop that thumbnail
- $config2['image_library'] = 'GD2';
- $config2['quality'] = 100;
- $config2['height'] = 132;
- $config2['width'] = 176;
- $config2['source_image'] = $this->image_thumb_name($img);
- $axis = $this->get_axis($config2['source_image']);
- $config2['x_axis'] = $axis['x_axis'];
- $config2['y_axis'] = $axis['y_axis'];
- $config2['maintain_ratio'] = FALSE;
- $config2['create_thumb'] = FALSE;
- $this->image_lib->initialize($config2);
- if (!$this->image_lib->crop()) {
- echo $this->image_lib->display_errors();
- }
- echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug
- $this->image_lib->clear();
- unset($config2);
- }
- }
- $config['create_thumb'] = FALSE;
- $config['new_image'] = $this->image_thumb_name($img);
- var_dump($config['source_image']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement