note 28052 added to function.imagerotate

From: Date: Tue, 31 Dec 2002 01:20:20 +0000
Subject: note 28052 added to function.imagerotate
Groups: php.notes 
Request: Send a blank email to php-notes+get-41502@lists.php.net to get a copy of this message
I wrote a function that will rotate an image by 90, 180 or 270 degrees without chopping off part of the picture if it's not square. If you've tried to use imagerotate() on a rectangular image, you'll know what I'm talking about. It may not be the best way to do this, but it gets the job done. Use it like so: $dst_img = rotate(src_img, 90); where $src_img is an image resource and 90 is the number if degrees to rotate. Note: This function ONLY works with 90, 180, or 270 degrees. function rotate($src_img, $degrees = 90) { $degrees %= 360; if ($degrees == 0) { $dst_img = $src_img; } Elseif ($degrees == 180) { $dst_img = imagerotate($src_img, $degrees, 0); } Else { $width = imagesx($src_img); $height = imagesy($src_img); if ($width > $height) { $size = $width; } Else { $size = $height; } $dst_img = imagecreatetruecolor($size, $size); imagecopy($dst_img, $src_img, 0, 0, 0, 0, $width, $height); $dst_img = imagerotate($dst_img, $degrees, 0); $src_img = $dst_img; $dst_img = imagecreatetruecolor($height, $width); if ((($degrees == 90) && ($width > $height)) || (($degrees == 270) && ($width < $height))) { imagecopy($dst_img, $src_img, 0, 0, 0, 0, $size, $size); } if ((($degrees == 270) && ($width > $height)) || (($degrees == 90) && ($width < $height))) { imagecopy($dst_img, $src_img, 0, 0, $size - $height, $size - $width, $size, $size); } } return $dst_img; } -- http://www.php.net/manual/en/function.imagerotate.php http://master.php.net/manage/user-notes.php?action=edit+28052 http://master.php.net/manage/user-notes.php?action=delete+28052 http://master.php.net/manage/user-notes.php?action=reject+28052

« previous php.notes (#41502) next »