note 50105 added to function.imagerotate
| From: | jonathanATsharpmediaDOTnet at osu1 dot php dot net | Date: | Fri, 18 Feb 2005 06:28:03 +0000 |
| Subject: | note 50105 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85192@lists.php.net to get a copy of this message | ||
This method rotates an image in 90 degree increments (eg count should be between 1 and 3) and avoids
the problems of image scaling that imageRotate has...
<?php
function rotateImage($src, $count = 1, $quality = 95)
{
if (!file_exists($src)) {
return false;
}
list($w, $h) = getimagesize($src);
if (($in = imageCreateFromJpeg($src)) === false) {
echo "Failed create from source<br>";
return false;
}
$angle = 360 - ((($count > 0 && $count < 4) ? $count : 0 ) * 90);
if ($w == $h || $angle == 180) {
$out = imageRotate($in, $angle, 0);
} elseif ($angle == 90 || $angle == 270) {
$size = ($w > $h ? $w : $h);
// Create a square image the size of the largest side of our src image
if (($tmp = imageCreateTrueColor($size, $size)) == false) {
echo "Failed create square trueColor<br>";
return false;
}
// Exchange sides
if (($out = imageCreateTrueColor($h, $w)) == false) {
echo "Failed create trueColor<br>";
return false;
}
// Now copy our src image to tmp where we will rotate and then copy that to $out
imageCopy($tmp, $in, 0, 0, 0, 0, $w, $h);
$tmp2 = imageRotate($tmp, $angle, 0);
// Now copy tmp2 to $out;
imageCopy($out, $tmp2, 0, 0, ($angle == 270 ? abs($w - $h) : 0), 0, $h, $w);
imageDestroy($tmp);
imageDestroy($tmp2);
} elseif ($angle == 360) {
imageDestroy($in);
return true;
}
imageJpeg($out, $src, $quality);
imageDestroy($in);
imageDestroy($out);
return true;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50105
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50105&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50105&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50105&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50105&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50105&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50105&report=yes
Search -- http://master.php.net/manage/user-notes.php