From 5a74d0e63df34926824e3c9b2bc8c83d9e982dba Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 14 Sep 2007 01:52:50 +0000 Subject: [PATCH] mujma: BUGFIX: Added rotatePixelByPixel method to add rotate functionality on machines where imagerotate function it's not available Ubuntu. (merged from branches/gsoc) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41718 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- filesystem/GD.php | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/filesystem/GD.php b/filesystem/GD.php index e3e7f2edd..57bc3526f 100755 --- a/filesystem/GD.php +++ b/filesystem/GD.php @@ -135,13 +135,59 @@ class GD extends Object { function rotate($angle) { if(!$this->gd) return; - $newGD = imagerotate($this->gd, $angle,0); - + if(function_exists("imagerotate")) { + $newGD = imagerotate($this->gd, $angle,0); + } else { + //imagerotate is not included in PHP included in Ubuntu + $newGD = $this->rotatePixelByPixel($angle); + } $output = new GD(); $output->setGD($newGD); return $output; } + /** + * Rotates image by given angle. It's slow because makes it pixel by pixel rather than + * using built-in function. Used when imagerotate function is not available(i.e. Ubuntu) + * + * @param angle + + * @return GD + */ + + function rotatePixelByPixel($angle) { + $sourceWidth = imagesx($this->gd); + $sourceHeight = imagesy($this->gd); + if ($angle == 180) { + $destWidth = $sourceWidth; + $destHeight = $sourceHeight; + } else { + $destWidth = $sourceHeight; + $destHeight = $sourceWidth; + } + $rotate=imagecreatetruecolor($destWidth,$destHeight); + imagealphablending($rotate, false); + for ($x = 0; $x < ($sourceWidth); $x++) { + for ($y = 0; $y < ($sourceHeight); $y++) { + $color = imagecolorat($this->gd, $x, $y); + switch ($angle) { + case 90: + imagesetpixel($rotate, $y, $destHeight - $x - 1, $color); + break; + case 180: + imagesetpixel($rotate, $destWidth - $x - 1, $destHeight - $y - 1, $color); + break; + case 270: + imagesetpixel($rotate, $destWidth - $y - 1, $x, $color); + break; + default: $rotate = $this->gd; + }; + } + } + return $rotate; + } + + /** * Crop's part of image. *