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;
}