note 26085 added to ref.image
| From: | juggler at webworkerinfo dot de | Date: | Thu, 17 Oct 2002 14:41:26 +0000 |
| Subject: | note 26085 added to ref.image | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38206@lists.php.net to get a copy of this message | ||
In the gd-library there is no function to rotate an image by a given angle.
So here it is:
<?php
function imagerotate(&$img, $ang, $bg=FALSE, $width=FALSE, $height=FALSE, $bench=FALSE) {
/*
(c) 2002 Jens Gottfried
Function to rotate an image
*/
if($bench) $time = microtime();
if(!$bg) $bg = imagecolorallocate($img, 255,255,255);
// size of the image
$src_w = imagesx($img);
$src_h = imagesy($img);
// reduce angle
while($ang > 180) $ang = $ang-360;
while($ang < -180) $ang = $ang+360;
// angles > 90°
if($ang > 90) {
imagerotate($img, 90, $bg, $src_h,$src_w);
$ang = $ang - 90;
}
elseif($ang < -90) {
imagerotate($img, -90, $bg, $src_h,$src_w);
$ang = $ang + 90;
}
// size of the image
$src_w = imagesx($img);
$src_h = imagesy($img);
if($ang != 0) {
// deg->rad
if($ang > 0) $wrad = deg2rad($ang);
else $wrad = deg2rad((-1*$ang));
$sin = sin($wrad); if($sin < 0) $sin*=-1;
$cos = cos($wrad); if($cos < 0) $cos*=-1;
// size of rotated image
$dst_w = (int) ( $sin*$src_h + $cos*$src_w );
$dst_h = (int) ( $cos*$src_h + $sin*$src_w );
if($dst_w < 0) $dst_w *= -1;
if($dst_h < 0) $dst_h *= -1;
$scale_x = (int) ( $width/$dst_w );
$scale_y = (int) ( $height/$dst_h );
// create image and set backgound-color
$dst_img = imagecreatetruecolor($dst_w, $dst_h);
imagefill($dst_img, 0,0, $bg);
for($i=0; $i<$src_h; $i++) {
for($j=0; $j<$src_w; $j++) {
// get new position of pixel
if($ang > 0) {
$dst_x = (int) ( $sin*($src_h-$i) + $cos*$j );
$dst_y = (int) ( $sin*$j + $cos*$i );
}
else {
$dst_x = (int) ( $sin*$i + $cos*$j );
$dst_y = (int) ( $sin*($src_w-$j) + $cos*$i );
}
if($scale_x != 0) $dst_x = $dst_x*$scale_x;
if($scale_y != 0) $dst_y = $dst_y*$scale_y;
// get color of actual pixel
$col = imagecolorat($img, $j, $i);
// draw pixel (draws 2 pixel at once to avoid gaps in rotated picture)
imagefilledrectangle($dst_img, $dst_x,$dst_y, $dst_x+1,$dst_y, $col);
}
}
// resampling needed
if($width!==FALSE && $height!==FALSE && ($width!=$dst_w || $height!=$dst_h)) {
$new_img = imagecreatetruecolor($width, $height);
imagefill($new_img, 0,0, $bg);
imagecopyresampled($new_img, $dst_img, 0,0,0,0, $width,$height, $dst_w,$dst_h);
imagedestroy($dst_img);
$img = $new_img;
}
else $img = $dst_img;
}
// no need to rotate but resample
elseif($width!==FALSE && $height!==FALSE && ($width!=$dst_w || $height!=$dst_h)) {
$dst_img = imagecreatetruecolor($width, $height);
imagefill($dst_img, 0,0, $bg);
imagecopyresampled($dst_img, $img, 0,0,0,0, $width, $height, $src_w, $src_h);
$img = $dst_img;
}
// nothing needs to be done
else $img = $dst_img;
if(!bench) return;
// show benchmark info
elseif($bench == "IMAGE") {
list($usec, $sec) = explode(" ",microtime());
$etime = (float)$usec + (float)$sec;
list($usec, $sec) = explode(" ",$time);
$etime -= (float)$usec + (float)$sec;
$etime = substr($etime, 0, 7);
imagefilledrectangle($img, 0,0, 42,12, imagecolorallocate($img, 255,255,255));
imagestring($img, 2, 0,0, $etime, imagecolorallocate($img, 0,0,0));
}
elseif($bench == "STRING") {
list($usec, $sec) = explode(" ",microtime());
$etime = (float)$usec + (float)$sec;
list($usec, $sec) = explode(" ",$time);
$etime -= (float)$usec + (float)$sec;
return $etime;
}
}
?>
imagerotate($img, $ang[, $bg[, $width, $height[, $bench]]]);
$img - is the image you want to rotate. NOTE: Its passed by reference, so maybe, you need to make a
copy yourself.
$ang - is the angle
$bg (optional) - is the index of the background-color (Default: white)
$width and $height (optional) - the Image will be resampled to this size when its not already the
same. Use always both!
$bench (optional)
"STRING" returns the time needed to execute the function.
"IMAGE" will put a small field with the time on the image.
On my testing system it takes ~1 sec to rotate an image (250x200px). ~2 seconds when the angle is
> 90°.
!This function only works on jpeg images!
Have fun
Juggler
--
http://www.php.net/manual/en/ref.image.php
http://master.php.net/manage/user-notes.php?action=edit+26085
http://master.php.net/manage/user-notes.php?action=delete+26085
http://master.php.net/manage/user-notes.php?action=reject+26085