note 93151 added to function.imagerotate
| From: | dev at imglib dot endofinternet dot net | Date: | Tue, 25 Aug 2009 21:53:02 +0000 |
| Subject: | note 93151 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159889@lists.php.net to get a copy of this message | ||
Fixed function from previsios post.
Add png transparetn, and most popular angle as static case for best view.
Rotation change to clockwise.
<?php
if(!function_exists("imagerotate")) {
function imagerotate($srcImg, $angle, $bgcolor=0, $ignore_transparent = 0) {
return imagerotateEquivalent($srcImg, $angle, $bgcolor, $ignore_transparent);
}
}
/*
Imagerotate replacement. ignore_transparent is work for png images
Also, have some standard functions for 90, 180 and 270 degrees.
Rotation is clockwise
*/
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);
//Normalize angle
$angle %= 360;
//Set rotate to clockwise
$angle = -$angle;
if($angle == 0) {
if ($ignore_transparent == 0) {
imagesavealpha($srcImg, true);
}
return $srcImg;
}
// Convert the angle to radians
$theta = deg2rad ($angle);
//Standart case of rotate
if ( (abs($angle) == 90) || (abs($angle) == 270) ) {
$width = $srch;
$height = $srcw;
if ( ($angle == 90) || ($angle == -270) ) {
$minX = 0;
$maxX = $width;
$minY = -$height+1;
$maxY = 1;
} else if ( ($angle == -90) || ($angle == 270) ) {
$minX = -$width+1;
$maxX = 1;
$minY = 0;
$maxY = $height;
}
} else if (abs($angle) === 180) {
$width = $srcw;
$height = $srch;
$minX = -$width+1;
$maxX = 1;
$minY = -$height+1;
$maxY = 1;
} else {
// 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);
if ($ignore_transparent == 0) {
imagefill($destimg, 0, 0, imagecolorallocatealpha($destimg, 255,255, 255, 127));
imagesavealpha($destimg, true);
}
// 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: 82.177.164.100
Probable Submitter: 95.134.34.135
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- https://master.php.net/note/edit/93151
Del: integrated -- https://master.php.net/note/delete/93151/integrated
Del: useless -- https://master.php.net/note/delete/93151/useless
Del: bad code -- https://master.php.net/note/delete/93151/bad+code
Del: spam -- https://master.php.net/note/delete/93151/spam
Del: non-english -- https://master.php.net/note/delete/93151/non-english
Del: in docs -- https://master.php.net/note/delete/93151/in+docs
Del: other reasons-- https://master.php.net/note/delete/93151
Reject -- https://master.php.net/note/reject/93151
Search -- https://master.php.net/manage/user-notes.php