note 50487 added to function.imagerotate

From: Date: Tue, 01 Mar 2005 21:22:48 +0000
Subject: note 50487 added to function.imagerotate
Groups: php.notes 
Request: Send a blank email to php-notes+get-85748@lists.php.net to get a copy of this message
I liked the rotateImageBicubic function implemented by darren at lucidtone dot com. But it just snipped off the parts of the image that were outside the original image. I fixed this, even though I admit that my solution is a bit naive. But it might come in handy for somebody. Also his bicubic implementation was broken on my machine so I left it out, if you need it just copy and paste it from above. <?php // $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 imageRotate($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); $cosangle = cos($angle); $sinangle = sin($angle); $corners=array(array(0,0), array($src_x,0), array($src_x,$src_y), array(0,$src_y)); foreach($corners as $key=>$value) { $value[0]-=$center_x; //Translate coords to center for rotation $value[1]-=$center_y; $temp=array(); $temp[0]=$value[0]*$cosangle+$value[1]*$sinangle; $temp[1]=$value[1]*$cosangle-$value[0]*$sinangle; $corners[$key]=$temp; } $min_x=1000000000000000; $max_x=-1000000000000000; $min_y=1000000000000000; $max_y=-1000000000000000; foreach($corners as $key => $value) { if($value[0]<$min_x) $min_x=$value[0]; if($value[0]>$max_x) $max_x=$value[0]; if($value[1]<$min_y) $min_y=$value[1]; if($value[1]>$max_y) $max_y=$value[1]; } $rotate_width=round($max_x-$min_x); $rotate_height=round($max_y-$min_y); $rotate=imagecreatetruecolor($rotate_width,$rotate_height); imagealphablending($rotate, false); imagesavealpha($rotate, true); //Reset center to center of our image $newcenter_x = ($rotate_width)/2; $newcenter_y = ($rotate_height)/2; for ($y = 0; $y < ($rotate_height); $y++) { for ($x = 0; $x < ($rotate_width); $x++) { // rotate... $old_x = round((($newcenter_x-$x) * $cosangle + ($newcenter_y-$y) * $sinangle)) + $center_x; $old_y = round((($newcenter_y-$y) * $cosangle - ($newcenter_x-$x) * $sinangle)) + $center_y; if ( $old_x >= 0 && $old_x < $src_x && $old_y >= 0 && $old_y < $src_y ) { $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); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagerotate.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50487 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50487&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50487&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50487&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50487&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50487&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50487&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85748) next »