note 43227 deleted from function.imagerotate by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 07:07:01 +0000 |
| Subject: | note 43227 deleted from function.imagerotate by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77889@lists.php.net to get a copy of this message | ||
Note Submitter: damien dot accorsi at free dot fr
----
Here comes a function wich *really* works with php 4.3.1 for 90°,180°, 270°.
function rotate ( $pFile, $pAngle, $pDestFile ) {
// 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 );
}
// exemple
$FileIn = "/var/www/html/images/1.jpeg";
$FileOut = "/var/www/html/images/1.jpeg"; // the one to create
rotate ( $FileIn, 90, $FileOut );