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.
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
image_manip
library class is defined as follows:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'), class Image_manip { }
We're going to amend the following two files:
/path/to/codeigniter/application/controllers/upload.php
/path/to/codeigniter/application/libraries/image_manip.php
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(); } }
function __construct() {
parent::__construct();
$this->load->helper('form'),
$this->load->helper('url'),
$this->load->library('image_manip'),
}
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.'; } }
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.
18.191.157.186