note 83547 added to function.imagerotate
| From: | pilot at myupb dot com | Date: | Sat, 31 May 2008 00:23:33 +0000 |
| Subject: | note 83547 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140022@lists.php.net to get a copy of this message | ||
Shortly after posting I was informed about a very nice algorithm for calculating the new pixel
position for a rotation and thought it would be a nice improvement. Hopes this helps anyone needing
a full implementation.
Note: I tested the function on a 3Mb photo and it seemed to work perfectly with no quality loss.
<?php
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);
$x2 = $x * cos($theta) - $y * sin($theta);
$y2 = $x * sin($theta) + $y * cos($theta);
// 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: 66.207.199.35
Probable Submitter: 24.78.45.213
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- https://master.php.net/note/edit/83547
Del: integrated -- https://master.php.net/note/delete/83547/integrated
Del: useless -- https://master.php.net/note/delete/83547/useless
Del: bad code -- https://master.php.net/note/delete/83547/bad+code
Del: spam -- https://master.php.net/note/delete/83547/spam
Del: non-english -- https://master.php.net/note/delete/83547/non-english
Del: in docs -- https://master.php.net/note/delete/83547/in+docs
Del: other reasons-- https://master.php.net/note/delete/83547
Reject -- https://master.php.net/note/reject/83547
Search -- https://master.php.net/manage/user-notes.php