/**
  * Rotate image
  *
  * @param  string   $path               image file
  * @param  int      $degree             rotete degrees
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author nao-pon
  * @author Troex Nevelin
  **/
 protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->rotateImage(new ImagickPixel($bgcolor), $degree);
             $result = $img->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             $img = self::gdImageCreate($path, $s['mime']);
             $degree = 360 - $degree;
             list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
             $bgcolor = imagecolorallocate($img, $r, $g, $b);
             $tmp = imageRotate($img, $degree, (int) $bgcolor);
             $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
             imageDestroy($img);
             imageDestroy($tmp);
             return $result ? $path : false;
             break;
     }
     return false;
 }
 /**
  * Rotate image
  *
  * @param  string   $path               image file
  * @param  int      $degree             rotete degrees
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author nao-pon
  * @author Troex Nevelin
  **/
 protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->rotateImage(new ImagickPixel($bgcolor), $degree);
             $result = $img->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             if ($s['mime'] == 'image/jpeg') {
                 $img = imagecreatefromjpeg($path);
             } elseif ($s['mime'] == 'image/png') {
                 $img = imagecreatefrompng($path);
             } elseif ($s['mime'] == 'image/gif') {
                 $img = imagecreatefromgif($path);
             } elseif ($s['mime'] == 'image/xbm') {
                 $img = imagecreatefromxbm($path);
             }
             $degree = 360 - $degree;
             list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
             $bgcolor = imagecolorallocate($img, $r, $g, $b);
             $tmp = imageRotate($img, $degree, (int) $bgcolor);
             if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') {
                 $result = imagejpeg($tmp, $path, 100);
             } else {
                 if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') {
                     $result = imagegif($tmp, $path, 7);
                 } else {
                     $result = imagepng($tmp, $path, 7);
                 }
             }
             imageDestroy($img);
             imageDestroy($tmp);
             return $result ? $path : false;
             break;
     }
     return false;
 }
 /**
  * Rotate image
  *
  * @param  string   $path               image file
  * @param  int      $degree             rotete degrees
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author nao-pon
  * @author Troex Nevelin
  **/
 protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false || $degree % 360 === 0) {
         return false;
     }
     $result = false;
     // try lossless rotate
     if ($degree % 90 === 0 && in_array($s[2], array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000))) {
         $count = $degree / 90 % 4;
         $exiftran = array(1 => '-9', 2 => '-1', 3 => '-2');
         $jpegtran = array(1 => '90', 2 => '180', 3 => '270');
         $quotedPath = escapeshellarg($path);
         $cmds = array('exiftran -i ' . $exiftran[$count] . ' ' . $path, 'jpegtran -rotate ' . $jpegtran[$count] . ' -copy all -outfile ' . $quotedPath . ' ' . $quotedPath);
         foreach ($cmds as $cmd) {
             if ($this->procExec($cmd) === 0) {
                 $result = true;
                 break;
             }
         }
         if ($result) {
             return $path;
         }
     }
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->rotateImage(new ImagickPixel($bgcolor), $degree);
             $result = $img->writeImage($path);
             $img->destroy();
             return $result ? $path : false;
             break;
         case 'gd':
             $img = self::gdImageCreate($path, $s['mime']);
             $degree = 360 - $degree;
             list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
             $bgcolor = imagecolorallocate($img, $r, $g, $b);
             $tmp = imageRotate($img, $degree, (int) $bgcolor);
             $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
             imageDestroy($img);
             imageDestroy($tmp);
             return $result ? $path : false;
             break;
     }
     return false;
 }