note 83530 added to function.imagerotate

From: Date: Fri, 30 May 2008 14:33:53 +0000
Subject: note 83530 added to function.imagerotate
Groups: php.notes 
Request: Send a blank email to php-notes+get-140002@lists.php.net to get a copy of this message
Thanks to the people who contributed the code for the 90 180 and 270 rotations. I needed a full implementation however so I wrote one. By no mean do I think this is the best way of doing it, I just whipped this together for myself. Seems to work good for me. Note: I didn't want the image to be shrunk when rotating so this implementation will keep the size of the original image but just rotate it. if(!function_exists("imagerotate")) { function imagerotate(&$srcImg, $angle, $transparentColor = null) { $srcw = imagesx($srcImg); $srch = imagesy($srcImg); if($angle == 0) return $srcImg; // Convert the angle to radians $pi = 3.141592654; $theta = $angle * $pi / 180; // Get the origin (center) of the image $originx = $srcw / 2; $originy = $srch / 2; // The pixels array for the new image $pixels = array(); $minx = 0; $maxx = 0; $miny = 0; $maxy = 0; $dstw = 0; $dsth = 0; // Loop through every pixel and transform it for($x=0;$x<$srcw;$x++) { for($y=0;$y<$srch;$y++) { list($x1, $y1) = translateCoordinate($originx, $originy, $x, $y, false); $theta1 = 0; $noTranslate = false; // Determine the angle of original point if($x1 > 0 && $y1 > 0) { // Quadrant 1 $theta1 = atan($y1/$x1); } elseif($x1 < 0 && $y1 > 0) { // Quadrant 2 $theta1 = $pi - atan($y1/abs($x1)); } elseif($x1 < 0 && $y1 < 0) { // Quadrant 3 $theta1 = $pi + atan($y1/$x1); } elseif($x1 > 0 && $y1 < 0) { // Quadrant 4 $theta1 = 2 * $pi - atan(abs($y1)/$x1); } elseif($x1 == 0 && $y1 > 0) { $theta1 = $pi / 2; } elseif($x1 == 0 && $y1 < 0) { $theta1 = 3 * $pi / 2; } elseif($x1 > 0 && $y1 == 0) { $theta1 = 0; } elseif($x1 < 0 && $y1 == 0) { $theta1 = $pi; } else { // Only case left should be $x1 == 0 && $y1 == 0 $noTranslate = true; } // Translate the position if(!$noTranslate) { // Calculate the new angle $theta2 = $theta1 + $theta; // Make sure theta2 is in between 0 - 2pi while($theta2 < 0) $theta2 += 2 * $pi; while($theta2 > (2 * $pi)) $theta2 -= 2 * $pi; $radius = sqrt($x1*$x1 + $y1*$y1); $x2 = ($radius * cos($theta2)); $y2 = ($radius * sin($theta2)); } else { $x2 = $x1; $y2 = $y1; } // Store the pixel color $pixels[] = array($x2, $y2, imagecolorat($srcImg, $x, $y)); // Check our boundaries if($x2 > $maxx) $maxx = $x2; if($x2 < $minx) $minx = $x2; if($y2 > $maxy) $maxy = $y2; if($y2 < $miny) $miny = $y2; } } // Determine the new image size $dstw = $maxx - $minx + 1; $dsth = $maxy - $miny + 1; // Create our new image $dstImg = imagecreatetruecolor($dstw, $dsth); // Fill the background with our transparent color if($transparentColor == null) $transparentColor = imagecolorallocate($dstImg, 1, 2, 3); imagecolortransparent($dstImg, $transparentColor); imagefilledrectangle($dstImg, 0, 0, $dstw + 1, $dsth + 1, $transparentColor); // Get the new origin $neworiginx = -$minx; $neworiginy = -$miny; // Fill in the pixels foreach($pixels as $data) { list($x, $y, $color) = $data; list($newx, $newy) = translateCoordinate($neworiginx, $neworiginy, $x, $y); imagesetpixel($dstImg, $newx, $newy, $color); } return $dstImg; } /** * Translates from mathematical coordinate system to computer coordinate system using * origin coordinates from the computer system or visa versa * * @param int $originx * @param int $originy * @param int $x * @param int $y * @param bool $toComp * @return array(int $x, int $y) */ function translateCoordinate($originx, $originy, $x, $y, $toComp=true) { if($toComp) { $newx = $originx + $x; $newy = $originy - $y; } else { $newx = $x - $originx; $newy = $originy - $y; } return array($newx, $newy); } } ---- Server IP: 216.235.15.211 Probable Submitter: 205.200.22.48 ---- Manual Page -- http://www.php.net/manual/en/function.imagerotate.php Edit -- https://master.php.net/note/edit/83530 Del: integrated -- https://master.php.net/note/delete/83530/integrated Del: useless -- https://master.php.net/note/delete/83530/useless Del: bad code -- https://master.php.net/note/delete/83530/bad+code Del: spam -- https://master.php.net/note/delete/83530/spam Del: non-english -- https://master.php.net/note/delete/83530/non-english Del: in docs -- https://master.php.net/note/delete/83530/in+docs Del: other reasons-- https://master.php.net/note/delete/83530 Reject -- https://master.php.net/note/reject/83530 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140002) next »