note 91505 added to function.imagerotate
| From: | anon at here dot com | Date: | Sat, 13 Jun 2009 20:00:15 +0000 |
| Subject: | note 91505 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156840@lists.php.net to get a copy of this message | ||
I was quite surprised to see that no one had done any working rotate function. And since I needed
one for my web hotel, I whipped this one up. Someone might want to expand it so that it matches
imagerotate.
Also, does imagerotate really rotate things counter clockwise? Otherwise this should also be
changed.
<?php
if(!function_exists("imagerotate")) {
function imagerotate(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0) {
return imagerotateEquivalent(&$srcImg, $angle, $bgcolor, $ignore_transparent);
}
}
/*
Unfinished imagerotate replacement. ignore_transparent is, well, ignored. :)
Also, should have some standard functions for 90, 180 and 270 degrees, since they are quite
easy to implement faster.
*/
function imagerotateEquivalent(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0)
{
function rotateX($x, $y, $theta){
return $x * cos($theta) - $y * sin($theta);
}
function rotateY($x, $y, $theta){
return $x * sin($theta) + $y * cos($theta);
}
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);
if($angle == 0) return $srcImg;
// Convert the angle to radians
$theta = deg2rad ($angle);
// Calculate the width of the destination image.
$temp = array ( rotateX(0, 0, 0-$theta),
rotateX($srcw, 0, 0-$theta),
rotateX(0, $srch, 0-$theta),
rotateX($srcw, $srch, 0-$theta)
);
$minX = floor(min($temp));
$maxX = ceil(max($temp));
$width = $maxX - $minX;
// Calculate the height of the destination image.
$temp = array ( rotateY(0, 0, 0-$theta),
rotateY($srcw, 0, 0-$theta),
rotateY(0, $srch, 0-$theta),
rotateY($srcw, $srch, 0-$theta)
);
$minY = floor(min($temp));
$maxY = ceil(max($temp));
$height = $maxY - $minY;
$destimg = imagecreatetruecolor($width, $height);
imagefill($destimg, 0, 0, imagecolorallocate($destimg, 0,255, 0));
// sets all pixels in the new image
for($x=$minX;$x<$maxX;$x++) {
for($y=$minY;$y<$maxY;$y++)
{
// fetch corresponding pixel from the source image
$srcX = round(rotateX($x, $y, $theta));
$srcY = round(rotateY($x, $y, $theta));
if($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY <
$srch)
{
$color = imagecolorat($srcImg, $srcX, $srcY );
}
else
{
$color = $bgcolor;
}
imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
}
}
return $destimg;
}
?>
----
Server IP: 80.67.2.2
Probable Submitter: 94.255.201.179
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- https://master.php.net/note/edit/91505
Del: integrated -- https://master.php.net/note/delete/91505/integrated
Del: useless -- https://master.php.net/note/delete/91505/useless
Del: bad code -- https://master.php.net/note/delete/91505/bad+code
Del: spam -- https://master.php.net/note/delete/91505/spam
Del: non-english -- https://master.php.net/note/delete/91505/non-english
Del: in docs -- https://master.php.net/note/delete/91505/in+docs
Del: other reasons-- https://master.php.net/note/delete/91505
Reject -- https://master.php.net/note/reject/91505
Search -- https://master.php.net/manage/user-notes.php