note 57226 added to function.imagerotate
| From: | simon_nuttall at hotmail dot com | Date: | Tue, 27 Sep 2005 17:06:46 +0000 |
| Subject: | note 57226 added to function.imagerotate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95888@lists.php.net to get a copy of this message | ||
The following is potentially useful. It extracts the central largest circle of an image into a
square of specified size, and optionally rotates it. The rest of the square is made transparent, so
useful for drawing over other images. I've named it after binocular effect because on some old
TV shows whenever they show someone looking through binoculars the screen shows a big circular image
with black edges.
<?php
function image_binocular_effect($src, $bearing, $out_square) {
// the source image is resampled to fit within the specified square, and rotated clockwise by
bearing.
// the largest circle within the image is retained, the rest made transparent.
$out = imagecreatetruecolor($out_square, $out_square);
$width=imagesx($src);
$height=imagesy($src);
$square=min($width, $height);
imagecopyresampled($out, $src, 0, 0, ($width - $square)/2 , ($height - $square)/2, $out_square,
$out_square, $square, $square);
$mask = imagecreatetruecolor($out_square, $out_square);
$black = ImageColorAllocate ($mask, 0, 0, 0);
$white = ImageColorAllocate ($mask, 255, 255, 255);
imagefilledrectangle($mask , 0, 0, $out_square, $out_square, $white);
$centrexy=$out_square / 2;
imagefilledellipse($mask, $centrexy, $centrexy, $out_square, $out_square, $black);
ImageColorTransparent($mask, $black);
imagecopymerge($out, $mask, 0, 0, 0, 0, $out_square, $out_square, 100);
if ($bearing != 0) {
$rotated_img=imagerotate($out , 360-$bearing, $white);
// take off only the rotated width
$rotated_map_width = imagesx($rotated_img);
$rotated_map_height = imagesy($rotated_img);
imagecopy($out, $rotated_img, 0, 0, ($rotated_map_width - $out_square) / 2, ($rotated_map_height -
$out_square) / 2, $out_square, $out_square);
}
ImageColorTransparent($out, $white);
return $out;
}
// Create a sample image to demonstrate the effect, but looks much better on real photos.
$src = imagecreatetruecolor(200, 50);
imagefilledrectangle($src, 0, 0, 200, 50, imagecolorallocate($src, 255, 255, 255));
ImageString($src, 3, 10, 10, "This is a sample image to illustrate the binocular effect",
imagecolorallocate($im, 192, 0, 0));
$img=image_binocular_effect($src, 72, 50);
ImagePNG($img,"test.png");
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagerotate.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57226
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57226&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57226&report=yes
Search -- http://master.php.net/manage/user-notes.php