[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/imageapi/ -> imagerotate.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * provides the imagerotate function for php-gd extensions compiled with the
   6   * upstream libgd instead of the libgd bundled with php.
   7   */
   8  
   9  
  10  // Define as included.
  11  define('IMAGEAPI_IMAGEROTATE_PHP', 1);
  12  
  13  function imagerotate(&$im, $angle, $bgcolor) {
  14    if ($angle === 0) {
  15      return $im;
  16    }
  17    // imagerotate() in php's libgd rotates the image counterclockwise,
  18    // this implementation rotates clockwise. The angle needs to be
  19    // inverted to give the same behaviour between these implementations.
  20    $angle = 360 - $angle;
  21  
  22    $width  = imagesx($im);
  23    $height = imagesy($im);
  24    // background color.
  25    list($r, $g, $b, $a) = imageapi_hex2rgba($bgcolor);
  26  
  27    switch ($angle) {
  28      case 270:
  29      case 90:
  30        // flip dimensions.
  31        $rot_width = $height;
  32        $rot_height = $width;
  33        break;
  34      case 180:
  35        // maintain dims.
  36        $rot_width = $width;
  37        $rot_height = $height;
  38        break;
  39  
  40      default:
  41        // well it won't be easy but we'll actually rotate this
  42        // puppy ourselves.. gonna require a little trig.
  43        // @todo: convert to a polar equation and use 1/2 length of hypoteneus.
  44        $center_x = floor($width/2);
  45        $center_y = floor($height/2);
  46  
  47        // convert to radians and precompute ...
  48        $cosangle = cos(deg2rad($angle+180));
  49        $sinangle = sin(deg2rad($angle+180));
  50  
  51        // caluculate new width and height.
  52        $corners=array(array(0, 0), array($width, 0), array($width, $height), array(0, $height));
  53        $max_x = $min_x = $max_y = $min_y = 0;
  54        foreach ($corners as $key => $value) {
  55          $value[0] -= $center_x;        //Translate coords to center for rotation
  56          $value[1] -= $center_y;
  57          $x = $value[0] * $cosangle + $value[1] * $sinangle;
  58          $y = $value[1] * $cosangle - $value[0] * $sinangle;
  59          $max_x = max($x, $max_x);
  60          $min_x = min($x, $min_x);
  61          $max_y = max($y, $max_y);
  62          $min_y = min($y, $min_y);
  63        }
  64        $rot_width = (int)$max_x - $min_x;
  65        $rot_height = (int)$max_y - $min_y;
  66        $rot_center_x = floor($rot_width/2);
  67        $rot_center_y = floor($rot_height/2);
  68    }
  69  
  70    $rotate = imagecreatetruecolor($rot_width, $rot_height);
  71    $bg = imagecolorallocatealpha($rotate, $r, $g, $b, $a);
  72    imagefilledrectangle($rotate, 0, 0, $rot_width, $rot_height, $bg);
  73    imagealphablending($rotate, FALSE);
  74    imagesavealpha($rotate, TRUE);
  75  
  76    switch ($angle) {
  77      case 90:
  78        $rot_width--;
  79        for ($y = 0; $y < $height; ++$y)
  80          for ($x = 0; $x < $width; ++$x)
  81            imagesetpixel($rotate, $rot_width - $y, $x, imagecolorat($im, $x, $y));
  82        break;
  83      case 270:
  84        $rot_height--;
  85        for ($y = 0; $y < $height; ++$y)
  86          for ($x = 0; $x < $width; ++$x)
  87            imagesetpixel($rotate, $y, $rot_height - $x, imagecolorat($im, $x, $y));
  88        break;
  89      case 180:
  90        $rot_width--;
  91        $rot_height--;
  92        for ($y = 0; $y < $height; ++$y)
  93          for ($x = 0; $x < $width; ++$x)
  94            imagesetpixel($rotate, $rot_width - $x, $rot_height - $y, imagecolorat($im, $x, $y));
  95        break;
  96  
  97      default:
  98        for ($y = 0; $y < $rot_height; ++$y) {
  99          for ($x = 0; $x < $rot_width; ++$x) {
 100            $mod_y = $rot_center_y-$y;
 101            $mod_x = $rot_center_x-$x;
 102            $old_x = $mod_x * $cosangle + $mod_y * $sinangle + $center_x;
 103            $old_y = $mod_y * $cosangle - $mod_x * $sinangle + $center_y;
 104            if ($old_x >= 0 && $old_x < $width && $old_y >= 0 && $old_y < $height) {
 105              $color = imagecolorat($im, $old_x, $old_y);
 106              imagesetpixel($rotate, $x, $y,  $color);
 107            }
 108          }
 109        }
 110    }
 111    return $rotate;
 112  }
 113  


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7