note 48725 added to function.imagerotate
| From: | Borszczuk at osu1 dot php dot net | Date: | Wed, 05 Jan 2005 13:21:04 +0000 |
| Subject: | note 48725 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82782@lists.php.net to get a copy of this message | ||
Here's a function that implements right angle (multiplicity of 90 degs - 90, 180, 270) rotation
if you need one but lacks native imagerotate() or you don't want non-square images to be scaled
down as with imagerotate(). As you probably noticed it's not self contained function, as 180
rotation is handled by ImageFlip() function to gain the performance. The ImageFlip() function used
is published here: http://php.net/imagecopy in the comment of
mine placed on 05-Jan-2005 04:30
Please note: that in case of 0 degrees rotation handle to imgSrc is returned which may lead to
problems if you imagedestroy() it undonditionaly. To solve that you shall add imagecopy($imgDest,
$imgSrc, 0,0, 0,0,$srcX, $srcY) in proper place which I have intentionally ommited to save memory
resources
<?php
// $imgSrc - GD image handle of source image
// $angle - angle of rotation. Needs to be positive integer
// angle shall be 0,90,180,270, but if you give other it
// will be rouned to nearest right angle (i.e. 52->90 degs,
// 96->90 degs)
// returns GD image handle of rotated image.
function ImageRotateRightAngle( $imgSrc, $angle )
{
// ensuring we got really RightAngle (if not we choose the closest one)
$angle = min( ( (int)(($angle+45) / 90) * 90), 270 );
// no need to fight
if( $angle == 0 )
return( $imgSrc );
// dimenstion of source image
$srcX = imagesx( $imgSrc );
$srcY = imagesy( $imgSrc );
switch( $angle )
{
case 90:
$imgDest = imagecreatetruecolor( $srcY, $srcX );
for( $x=0; $x<$srcX; $x++ )
for( $y=0; $y<$srcY; $y++ )
imagecopy($imgDest, $imgSrc, $srcY-$y-1, $x, $x, $y, 1, 1);
break;
case 180:
$imgDest = ImageFlip( $imgSrc, IMAGE_FLIP_BOTH );
break;
case 270:
$imgDest = imagecreatetruecolor( $srcY, $srcX );
for( $x=0; $x<$srcX; $x++ )
for( $y=0; $y<$srcY; $y++ )
imagecopy($imgDest, $imgSrc, $srcY-$y-1, $srcX-$x-1, $x, $y, 1, 1);
break;
}
return( $imgDest );
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48725
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48725&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48725&report=yes
Search -- http://master.php.net/manage/user-notes.php