note 48725 modified in function.imagerotate by techtonik
| From: | techtonik@php.net | Date: | Thu, 01 Sep 2005 18:59:25 +0000 |
| Subject: | note 48725 modified in function.imagerotate by techtonik | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94619@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, $y, $srcX-$x-1, $x, $y, 1, 1);
break;
}
return( $imgDest );
}
?>
--was--
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 );
}
?>
http://php.net/manual/en/function.imagerotate.php