Written by davidonzo on 10/09/2008, Filled in OpenSource, Web, Tutorial
WARNING!
This article has been written 552 days ago.
The informations may be out of dated!

php at davidonzo.comThe GD library released by the ubuntu team and available in their repository, is not compiled with all options. The imagerotate() function doesn't work at all.

 

In this case you have two possibilities. Download the sources from the GD official web site and compile it on your own, or use the following function.

 

<?
// $src_img - a GD image resource
// $angle - degrees to rotate clockwise, in degrees
// returns a GD image resource
// USAGE:
// $im = imagecreatefrompng('test.png');
// $im = imagerotate($im, 15);
// header('Content-type: image/png');
// imagepng($im);
function imageRotateBicubic($src_img, $angle, $bicubic=false) {
  
    // convert degrees to radians
    $angle = $angle + 180;
    $angle = deg2rad($angle);
  
    $src_x = imagesx($src_img);
    $src_y = imagesy($src_img);
  
    $center_x = floor($src_x/2);
    $center_y = floor($src_y/2);
  
    $rotate = imagecreatetruecolor($src_x, $src_y);
    imagealphablending($rotate, false);
    imagesavealpha($rotate, true);

    $cosangle = cos($angle);
    $sinangle = sin($angle);
  
    for ($y = 0; $y < $src_y; $y++) {
      for ($x = 0; $x < $src_x; $x++) {
    // rotate...
    $old_x = (($center_x-$x) * $cosangle + ($center_y-$y) * $sinangle)
      + $center_x;
    $old_y = (($center_y-$y) * $cosangle - ($center_x-$x) * $sinangle)
      + $center_y;
  
    if ( $old_x >= 0 && $old_x < $src_x
         && $old_y >= 0 && $old_y < $src_y ) {
      if ($bicubic == true) {
        $sY  = $old_y + 1;
        $siY  = $old_y;
        $siY2 = $old_y - 1;
        $sX  = $old_x + 1;
        $siX  = $old_x;
        $siX2 = $old_x - 1;
      
        $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
        $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
        $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
        $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
      
        $r = ($c1['red']  + $c2['red']  + $c3['red']  + $c4['red']  ) << 14;
        $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
        $b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >> 2;
        $a = ($c1['alpha']  + $c2['alpha']  + $c3['alpha']  + $c4['alpha'] ) >> 2;
        $color = imagecolorallocatealpha($src_img, $r,$g,$b,$a);
      } else {
        $color = imagecolorat($src_img, $old_x, $old_y);
      }
    } else {
          // this line sets the background colour
      $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
    }
    imagesetpixel($rotate, $x, $y, $color);
      }
    }
    return $rotate;
}
?>

 

This function has been taken by the official man page of the imagerotate php funciont.

Did you find interesting this article?
Subscribe my feed to be advised of any new post!
 
.Comments rss
# 1
If you want to get your own function back you can also recompile the gd.so library by following these simple steps:

http://info.momeunier.fr/node/20
By Marcool  (Sent 16/10/2008 @ 15:31:24)
# 2
what is "blabla" on that link?
By matt  (Sent 19/03/2009 @ 22:48:20)
# 3
blabla is a typical italian slang to say "foo" or "foobar", but where did you read it?
By davidonzo  (Sent 19/03/2009 @ 23:17:01)
# 4
Thanks for the manual function...
By sutriadi  (Sent 21/09/2009 @ 23:36:54)
# 5
The function works except it is cropping out part of the image when I use it,
for example if the image is 96x110 before rotating, then it is still 96x110 after even though the picture in the image is rotated. it should be 110x96 afterwards.
I just pasted it into my application but did not change anything except:
changed from
function imageRotateBicubic(
to
function imageRotate(
so it would be compatible with my existing script.
By jbrown  (Sent 29/10/2009 @ 21:15:43)


Comments could be moderated.
If you don't see it immediately published, please avoid to insert it one more time.
Thanks for your patient.