note 91709 added to function.imagerotate
| From: | krteczek01 at gmail dot com | Date: | Tue, 23 Jun 2009 22:21:13 +0000 |
| Subject: | note 91709 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157292@lists.php.net to get a copy of this message | ||
Hi, I've rewrote function to class with static methods. Advantage of this solution is ability
to be called several times during script execution.
<?php
/*
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.
*/
if(!function_exists("imagerotate")) {
function imagerotate(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0) {
return imagerotateEquivalent::rotate(&$srcImg, $angle, $bgcolor, $ignore_transparent);
}
}
class imagerotateEquivalent {
static private function rotateX($x, $y, $theta){
return $x * cos($theta) - $y * sin($theta);
}
static private function rotateY($x, $y, $theta){
return $x * sin($theta) + $y * cos($theta);
}
public static function rotate(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0) {
$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 ( self::rotateX(0, 0, 0-$theta),
self::rotateX($srcw, 0, 0-$theta),
self::rotateX(0, $srch, 0-$theta),
self::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 ( self::rotateY(0, 0, 0-$theta),
self::rotateY($srcw, 0, 0-$theta),
self::rotateY(0, $srch, 0-$theta),
self::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(self::rotateX($x, $y, $theta));
$srcY = round(self::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: 212.24.129.51
Probable Submitter: 217.77.165.45
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- https://master.php.net/note/edit/91709
Del: integrated -- https://master.php.net/note/delete/91709/integrated
Del: useless -- https://master.php.net/note/delete/91709/useless
Del: bad code -- https://master.php.net/note/delete/91709/bad+code
Del: spam -- https://master.php.net/note/delete/91709/spam
Del: non-english -- https://master.php.net/note/delete/91709/non-english
Del: in docs -- https://master.php.net/note/delete/91709/in+docs
Del: other reasons-- https://master.php.net/note/delete/91709
Reject -- https://master.php.net/note/reject/91709
Search -- https://master.php.net/manage/user-notes.php