note 57181 added to function.imageantialias

From: Date: Mon, 26 Sep 2005 14:06:27 +0000
Subject: note 57181 added to function.imageantialias
Groups: php.notes 
Request: Send a blank email to php-notes+get-95795@lists.php.net to get a copy of this message
I did a search in google and got following url: http://www.isocalc.com/tutorials/antialias.htm With this tutorial I was able to write a function to convert this algorithm into php, the result for a filled circel is this: <?php function imagefilledcircleantialiased(&$im, $cx, $cy, $r, $fgcolor, $bgcolor) { $fgcolors = imagecolorsforindex($im,$fgcolor); $bgcolors = imagecolorsforindex($im,$bgcolor); for ( $x = $cx - $r; $x <= $cx + $r; $x++ ) { for ( $y = $cy - $r; $y <= $cy + $r; $y++ ) { $rx = $x - $cx; $ry = $y - $cy; $ir = sqrt(( $rx == 0 ? 0 : pow($rx - 0.5*abs($rx)/$rx, 2) ) + ( $ry == 0 ? 0 : pow($ry - 0.5*abs($ry)/$ry, 2) )); $or = sqrt(( $rx == 0 ? 0 : pow($rx + 0.5*abs($rx)/$rx, 2) ) + ( $ry == 0 ? 0 : pow($ry + 0.5*abs($ry)/$ry, 2) )); if ( $or <= $r ) { imagesetpixel($im, $x, $y, $fgcolor); } elseif ( $ir < $r ) { $filled = 0; for ( $xx = $x - 0.45; $xx < $x + 0.5; $xx+=0.1 ) { for ( $yy = $y - 0.45; $yy < $y + 0.5; $yy+=0.1 ) { $rxx = $xx - $cx; $ryy = $yy - $cy; if ( sqrt(pow($rxx, 2) + pow($ryy, 2)) < $r ) $filled++; } } $red = round($bgcolors['red'] + ( $fgcolors['red'] - $bgcolors['red'] ) * $filled / 100); $green = round($bgcolors['green'] + ( $fgcolors['green'] - $bgcolors['green'] ) * $filled / 100); $blue = round($bgcolors['blue'] + ( $fgcolors['blue'] - $bgcolors['blue'] ) * $filled / 100); imagesetpixel($im, $x, $y, imagecolorclosest($im, $red, $green, $blue)); } } } } $width = 160; $height = 200; $r = 20; $bgc = "651713"; $fgc = "b12b2c"; $im = imagecreate($width, $height); $bgcolor = imagecolorallocate($im, hexdec(substr($bgc, 0, 2)), hexdec(substr($bgc, 2, 2)), hexdec(substr($bgc, 4, 2))); for( $i = 0; $i < 100; $i++ ) { imagecolorallocate($im, ( hexdec(substr($fgc, 0, 2)) + $i*hexdec(substr($bgc, 0, 2))) / ($i + 1), ( hexdec(substr($fgc, 2, 2)) + $i*hexdec(substr($bgc, 2, 2))) / ($i + 1), ( hexdec(substr($fgc, 4, 2)) + $i*hexdec(substr($bgc, 4, 2))) / ($i + 1)); } $fgcolor = imagecolorclosest($im, hexdec(substr($fgc, 0, 2)), hexdec(substr($fgc, 2, 2)), hexdec(substr($fgc, 4, 2))); imagefilledcircleantialiased($im, 80, 100, $r, $fgcolor, $bgcolor); header("Content-Type: image/png"); imagepng($im); ?> An improvement would be to draw the inner rectangle or more rectangles in the circle with the builtin rectangle function to reduce the usage of imagesetpixel() from (2*r)^2 to 2*Pi*(r + epsilon), in other words, the dependency on r would break down from square to linear. Another improvement would be to determine filled and unfilled triangles in the observed pixel and calculate their areas, so we can get rid of the inner loops for getting the fraction filled/unfilled. One can easily modify this function to solve other problems like lines, unfilled circles, etc. ---- Manual Page -- http://www.php.net/manual/en/function.imageantialias.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57181 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57181&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57181&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95795) next »