Rotating images

CodeIgniter allows for the rotation of images; this is useful if you need to flip something vertically or in any other direction. Here's how it's done.

Getting ready

We're going to use a library of our own for this. If you haven't already done so (in the other recipes in this chapter), create the following file:

  • /path/to/codeigniter/application/libraries/image_manip.php
  1. Ensure that the image_manip library class is defined as follows:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'),
    class Image_manip {
    }
  2. Also ensure that you have the image library, ImageMagik, installed that you have this chapter's "base" recipe—that is Uploading images with CodeIgniter—already copied and ready to go, as this recipe uses the code from Uploading images with CodeIgniter as a base recipe.

How to do it...

We're going to amend the following two files:

  • /path/to/codeigniter/application/controllers/upload.php
  • /path/to/codeigniter/application/libraries/image_manip.php
  1. Add the following function to the image_manip.php library file:
      function rotate($data) {
        $CI = & get_instance();
        $CI->load->library('image_lib', $data);
    
        $CI->image_lib->initialize($data); 
    
        if ($CI->image_lib->rotate()) {
          return true;
        } else {
          echo $CI->image_lib->display_errors();
        }
      }
  2. Add the following line (in bold) to the constructor so that the entire constructor looks like this:
    function __construct() {
      parent::__construct();
      $this->load->helper('form'),
      $this->load->helper('url'),
      $this->load->library('image_manip'),
    }
  3. Alter the do_upload() function, changing it to reflect the following:
    if ( ! $this->upload->do_upload()) {
    	$error = array('error' => $this->upload->display_errors());
    	$this->load->view('upload/upload', $error);
    } else {
    	$data = array('upload_data' => $this->upload->data());
    
    	$result = $this->upload->data();
    	$original_image = $result['full_path'];
    
    	$data = array(
    		'image_library' => 'imagemagick',
    		'library_path' => '/opt/ImageMagick/bin',
    		'source_image' => $original_image,			
    		'rotation_angle' => 'vrt'
    		);
    		if ($this->image_manip->rotate($data)) {
    		echo 'Image successfully rotated<br /><pre>';
    		var_dump($result);
    		echo '</pre>';
    	} else {
    		echo 'There was an error with the image processing.';
    	}
    }

How it works...

When the upload controller is run in the browser the user is presented with the form (which is in the view views/ipload/upload.php file) . The user selects an image and presses the Submit button, public function do_uplaod() is then called. We immediately define some settings against which the image being uploaded is checked, such as allowed image types, maximum size, and dimensions—just as we would in the Uploading images with CodeIgniter recipe. Assuming that the upload was successful and there were no errors, we fetch full_path from the upload data:

	$original_image = $result['full_path'];

Assign it as a local variable $original_image. We then define an array ($data) with all the configuration settings which CodeIgniter requires to crop the image (be sure to get the library_path correct). We pass this $data array to the library function, rotate:

	$this->image_manip->rotate($data);

This performs the rotate operation on the image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.186