note 44267 added to function.imagerotate
| From: | ericpatsheparddotcom at rn2 dot php dot net | Date: | Tue, 20 Jul 2004 02:59:47 +0000 |
| Subject: | note 44267 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73666@lists.php.net to get a copy of this message | ||
Here's an easy to set up, working HTML example of the function submitted by damien dot accorsi
at free dot fr.
<?
# PHP Rotate Example (up and running working example in 15 seconds)
// Drop this script into a *writable directory* with any jpeg image named "pic.jpg".
// Use at your own risk. Send comments to ericp at shepard dot com.
# HTML form
echo '<title>PHP Rotate</title>' ;
echo '<form method=post action='.$PHP_SELF.'>' ;
If ( $pAngle == 90 ) { $selected_90 = 'selected' ; }
If ( $pAngle == 270 ) { $selected_270 = 'selected' ; }
If ( $pAngle == 180 ) { $selected_180 = 'selected' ; }
If ( !$pAngle ) { $none_selected = 'selected' ; }
echo "<select name=pAngle>
<option value=\"\" $none_selected>Original Image</option>
<option value=270 $selected_270>Original -> Clockwise</option>
<option value=90 $selected_90>Original -> Counter-Clockwise</option>
<option value=180 $selected_180>Original -> Vertical Flip</option>
</select>
" ;
echo '<input type=hidden size=25 name=p1 value=pic.jpg>';
echo '<input type=hidden size=25 name=p2 value=pic2.jpg>';
echo '<input type=submit value=" GO "><br>';
echo '</form>';
If ( $p2 AND $pAngle ) {
rotate ( $p1, $p2, $pAngle ) ;
}
If ( $p2 AND $pAngle ) {
echo '<br><img src="'.$p2.'">' ;
}else{
echo '<br><img src="pic.jpg">' ;
}
# Rotate Function
// credit: damien dot accorsi at free dot fr
function rotate ( $pFile, $pDestFile, $pAngle) {
// remove all useless 360 degrees
$pAngle = $pAngle % 360;
// create an image ressource from $pFile
$ImgIn = imagecreatefromjpeg ( $pFile );
// case of flip
if ( $pAngle == 180 ) {
$ImgOut = ImageRotate ( $ImgIn, $pAngle, 180 );
}
else {
// other cases (90 or 270°)
$x = imagesx ( $ImgIn );
$y = imagesy ( $ImgIn );
$MaxXY = max ($x, $y);
// prepare intermediate picture, full size in order to loose nothing
$SquareImg = imagecreatetruecolor($MaxXY, $MaxXY);
imagecopy($SquareImg, $ImgIn, 0, 0, 0, 0, $x, $y);
$SquareImg = ImageRotate ( $SquareImg, $pAngle, 0 );
if ( $pAngle == 90 ) {
// we are in 90° rotation
$ImgOut = imagecreatetruecolor($y, $x);
imagecopy ( $ImgOut, $SquareImg, 0, 0, 0, $MaxXY - $x, $y, $x );
}
elseif ( $pAngle == 270 ) {
// we are in 270° rotation
$ImgOut = imagecreatetruecolor($y, $x);
imagecopy ( $ImgOut, $SquareImg, 0, 0, $MaxXY-$y, 0, $y, $x );
}
}
ImageJPEG ( $ImgOut, $pDestFile );
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44267
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44267&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44267&report=yes
Search -- http://master.php.net/manage/user-notes.php