Zebra_Image::rotate PHP Method

rotate() public method

include the Zebra_Image library require 'path/to/Zebra_Image.php'; instantiate the class $img = new Zebra_Image(); a source image $img->source_path = 'path/to/source.ext'; path to where should the resulting image be saved note that by simply setting a different extension to the file will instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext'; rotate the image 45 degrees, clockwise $img->rotate(45);
public rotate ( double $angle, mixed $background_color ) : boolean
$angle double Angle by which to rotate the image clockwise. Between 0 and 360. @param mixed $background_color (Optional) The hexadecimal color (like "#FFFFFF" or "#FFF") of the uncovered zone after the rotation. When set to -1 the script will preserve transparency for transparent GIF and PNG images. For non-transparent images the background will be white in this case. Default is -1. @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code.
$background_color mixed
return boolean
    public function rotate($angle, $background_color = -1)
    {
        // get function arguments
        $arguments = func_get_args();
        // if a third argument exists
        $use_existing_source = isset($arguments[2]) && $arguments[2] === false;
        // if we came here just to fix orientation or if image resource was successfully created
        if ($use_existing_source || $this->_create_from_source()) {
            // angles are given clockwise but imagerotate works counterclockwise so we need to negate our value
            $angle = -$angle;
            // if source image is PNG
            if ($this->source_type == IMAGETYPE_PNG && $background_color == -1) {
                // rotate the image
                // but if using -1 as background color didn't work (as is the case for PNG8)
                if (!($target_identifier = imagerotate($this->source_identifier, $angle, -1))) {
                    // we will be using #FFF as the color to fill the uncovered zone after the rotation
                    $background_color = imagecolorallocate($this->source_identifier, 255, 255, 255);
                    // rotate the image
                    $target_identifier = imagerotate($this->source_identifier, $angle, $background_color);
                }
                // if source image is a transparent GIF
            } elseif ($this->source_type == IMAGETYPE_GIF && $this->source_transparent_color_index >= 0) {
                // convert the background color to RGB values
                $background_color = $this->_hex2rgb($background_color);
                // allocate the color to the image identifier
                $background_color = imagecolorallocate($this->source_identifier, $background_color['r'], $background_color['g'], $background_color['b']);
                // rotate the image
                $this->source_identifier = imagerotate($this->source_identifier, $angle, $background_color);
                // get the width of rotated image
                $width = imagesx($this->source_identifier);
                // get the height of rotated image
                $height = imagesy($this->source_identifier);
                // create a blank image with the new width and height
                // (this intermediary step is for preserving transparency)
                $target_identifier = $this->_prepare_image($width, $height, -1);
                // copy the rotated image on to the new one
                imagecopyresampled($target_identifier, $this->source_identifier, 0, 0, 0, 0, $width, $height, $width, $height);
                // for the other cases
            } else {
                // convert the color to RGB values
                $background_color = $this->_hex2rgb($background_color);
                // allocate the color to the image identifier
                $background_color = imagecolorallocate($this->source_identifier, $background_color['r'], $background_color['g'], $background_color['b']);
                // rotate the image
                $target_identifier = imagerotate($this->source_identifier, $angle, $background_color);
            }
            // if we called this method from the _create_from_source() method
            // bacause we are fixing orientation
            if ($use_existing_source) {
                // make any further method work on the rotated image
                $this->source_identifier = $target_identifier;
                // update the width and height of the image to the values
                // of the rotated image
                $this->source_width = imagesx($target_identifier);
                $this->source_height = imagesy($target_identifier);
                return true;
                // write image otherwise
            } else {
                return $this->_write_image($target_identifier);
            }
        }
        // if script gets this far return false
        // note that we do not set the error level as it has been already set
        // by the _create_from_source() method earlier
        return false;
    }

Usage Example

Example #1
0
 // and if there is an error, show the error message
 if (!$image->flip_both()) {
     show_error($image->error, $image->source_path, $image->target_path);
 }
 // indicate a target image
 $image->target_path = 'results/crop.' . $ext;
 // crop
 // and if there is an error, show the error message
 if (!$image->crop(0, 0, 50, 50)) {
     show_error($image->error, $image->source_path, $image->target_path);
 }
 // indicate a target image
 $image->target_path = 'results/rotate.' . $ext;
 // rotate
 // and if there is an error, show the error message
 if (!$image->rotate(45)) {
     show_error($image->error, $image->source_path, $image->target_path);
 }
 // indicate a target image
 $image->target_path = 'results/filter.' . $ext;
 // apply some filters
 // (this combination produces the "sepia" filter)
 $image->apply_filter(array(array('grayscale'), array('colorize', 90, 60, 40)));
 ?>
 <p>Table has background so that transparency can be observed.</p>
 <table style="background:#ABCDEF; border: 2px solid #666">
 	<tr>
         <td width="100" align="center">Resized to 100x100</td>
         <td width="100" align="center">Flipped horizontally</td>
         <td width="100" align="center">Flipped vertically</td>
         <td width="100" align="center">Flipped both horizontally and vertically</td>
All Usage Examples Of Zebra_Image::rotate