on Fri Sep 22, 2006 * * Kudos to Ulrich Mierendorff for the ImageSmoothArc subroutine, which has been incorporated * into this script (see http://icewind.ic.funpic.de/index.php?content=ellipse) * * Use as you would an image URL, with query string parameters as follows: * * location * the location of this corner. For brevity, use TL, TR, BR, BL. For clarity, use any * combination of UPPER, TOP, LOWER, BOTTOM and LEFT, RIGHT. Alternatively, create four * symlinks to this script, using the same rules to define symlink basenames (they * should retain the .php filetype), and call those instead of always having to specify * the location parameter. * background (default CCCCCC) * the background colour (i.e. the background colour of the region bounded by this set * of rounded corners. Specified as 6-character hex triplets (NO leading # symbol). * outside (default FFFFFF) * the colour of the region outside your rectangle. Specified as 6-character hex triplets. * border * the colour of the border (if one exists). Specified as 6-character hex triplets. * borderwidth (default 0) * the width of the border (px). Should be +ve non-zero value if borders are important * to you. * radius (default 10) * the radius (px) of the corner. * smooth * Specified with any value (or none) if anti-aliasing is required. * * Examples: * * roundedcorner.php?location=upper-left&radius=15&borderwidth=2&border=000000&background=F0F0F0&outside=FFFFFF * * upper-left.php?outside=FFFFFF&background=FFFFFF&borderwidth=1&border=000000&smooth */ if ( array_key_exists('location', $_GET) ) { if ( isset($_GET['location']) ) $location = $_GET['location']; } if ( !isset($location) ) { preg_match('/([^\/\\:]+)\.\w+$/i', $_SERVER['PHP_SELF'], $match); $location = $match[1]; } if ( preg_match('/^TL|LT|UL$/i', $location) ) $arc_start = 180; else if ( preg_match('/^TR|RT|UR$/i', $location) ) $arc_start = 270; else if ( preg_match('/^BL|LB|LL$/i', $location) ) $arc_start = 90; else if ( preg_match('/^BR|RB|LR$/i', $location) ) $arc_start = 0; else { if ( preg_match('/UPPER|TOP/i', $location) ) { if ( preg_match('/LEFT/i', $location) ) $arc_start = 180; else if ( preg_match('/RIGHT/i', $location) ) $arc_start = 270; } else if ( preg_match('/LOWER|BOTTOM/i', $location) ) { if ( preg_match('/LEFT/i', $location) ) $arc_start = 90; else if ( preg_match('/RIGHT/i', $location) ) $arc_start = 0; } } if ( array_key_exists('radius', $_GET) ) { if ( isset($_GET['radius']) ) $radius = $_GET['radius']; } isset($radius) or $radius = 10; if ( isset($arc_start) ) { $arc_finish = $arc_start + 90; if ( array_key_exists('background', $_GET) ) { if ( isset($_GET['background']) ) $background['x'] = $_GET['background']; } isset($background['x']) or $background['x'] = 'cccccc'; hex2rgb($background); if ( array_key_exists('outside', $_GET) ) { if ( isset($_GET['outside']) ) $outside['x'] = $_GET['outside']; } isset($outside['x']) or $outside['x'] = 'ffffff'; hex2rgb($outside); if ( array_key_exists('border', $_GET) ) { if ( isset($_GET['border']) ) $border['x'] = $_GET['border']; } isset($border['x']) or $border['x'] = '000000'; hex2rgb($border); if ( array_key_exists('borderwidth', $_GET) ) { if ( isset($_GET['borderwidth']) ) $borderwidth = $_GET['borderwidth']; } isset($borderwidth) or $borderwidth = 0; $canvas = ImageCreateTrueColor($radius, $radius); ImageAlphaBlending($canvas, false); $color['background'] = ImageColorAllocateAlpha($canvas, $background['r'], $background['g'], $background['b'], 127); $color['border'] = ImageColorAllocateAlpha($canvas, $border['r'], $border['g'], $border['b'], 0); $color['outside'] = ImageColorAllocateAlpha($canvas, $outside['r'], $outside['g'], $outside['b'], 0); ImageAlphaBlending($canvas, true); ImageFilledRectangle($canvas, 0, 0, $radius, $radius, $color['outside']); if ( array_key_exists('smooth', $_GET) ) { if ( $arc_start == 0 ) { // BR $x = -1; $y = -2; $arc_start = 270; } else if ( $arc_start == 90 ) { // BL $x = $radius; $y = -2; $arc_start = 180; } else if ( $arc_start == 180 ) { // TL $x = $radius; $y = $radius; $arc_start = 90; } else if ( $arc_start == 270 ) { // TR $x = -1; $y = $radius; $arc_start = 0; } $arc_finish = deg2rad($arc_start + 90); $arc_start = deg2rad($arc_start); if ( $borderwidth > 0 ) { ImageSmoothArc($canvas, $x, $y, $radius * 2, $radius * 2, array($border['r'], $border['g'], $border['b']), $arc_start, $arc_finish); ImageSmoothArc($canvas, $x, $y, ($radius - $borderwidth) * 2, ($radius - $borderwidth) * 2, array($background['r'], $background['g'], $background['b']), $arc_start, $arc_finish); } else { ImageSmoothArc($canvas, $x, $y, $radius * 2, $radius * 2, array($background['r'], $background['g'], $background['b']), $arc_start, $arc_finish); } } else { if ( $arc_start == 0 ) { // BR $x = 0; $y = 0; } else if ( $arc_start == 90 ) { // BL $x = $radius - 1; $y = 0; } else if ( $arc_start == 180 ) { // TL $x = $radius - 1; $y = $radius - 1; } else if ( $arc_start == 270 ) { // TR $x = 0; $y = $radius - 1; } if ( $borderwidth > 0 ) { for ( $n = 0; $n < $borderwidth; ++$n ) { ImageArc($canvas, $x, $y, ($radius - $n) * 2, ($radius - $n) * 2, $arc_start, $arc_finish, $color['border']); } ImageFillToBorder($canvas, $x, $y, $color['border'], $color['background']); } else { ImageArc($canvas, $x, $y, $radius * 2, $radius * 2, $arc_start, $arc_finish, $color['background']); ImageFillToBorder($canvas, $x, $y, $color['background'], $color['background']); } } } else { $canvas = ImageCreateTrueColor($radius, $radius); $color['background'] = ImageColorAllocate($canvas, 255, 0, 0); ImageFilledRectangle($canvas, 0, 0, $radius, $radius, $color['background']); } ImageSaveAlpha($canvas, true); header('Content-Type: image/png'); ImagePNG($canvas); function hex2rgb(&$arr, $hex = '') { if ( $hex == '' ) $hex = $arr['x']; $arr['r'] = hexdec(substr($hex, 0, 2)); $arr['g'] = hexdec(substr($hex, 2, 2)); $arr['b'] = hexdec(substr($hex, 4, 2)); } /* ImageSmoothArc() created by Ulrich Mierendorff, Jun 2006 */ function ImageSmoothArc( &$img, $cx, $cy, $w, $h, $color, $start, $stop) { $fillColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 0 ); $w /= 2; $h /= 2; $cdx = $w * cos(M_PI/4); $cdy = $h * sin(M_PI/4); $xStart = $w * cos($start); $yStart = $h * sin($start); $xStop = $w * cos(min(M_PI,$stop)); $yStop = $h * sin(min(M_PI,$stop)); if ( $start < M_PI/2 ) { $yy = 0; for ( $x = 0; $x <= $xStart; $x += 1 ) { if ( $x < $xStop ) { $y1 = $x/$xStop*$yStop; } else { $y1 = $h * sqrt( 1 - pow( $x,2 ) / pow( $w,2 ) ); } $y2 = $x/$xStart*$yStart; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageLine($img, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx + $x, $cy - $y1 - 1, $diffColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d2*100 ); imageSetPixel($img, $cx + $x, $cy - $y2 + 1, $diffColor); for ( $yy; $yy <= $y1; $yy += 1 ) { if ( $yy < $yStart ) { $x1 = $yy/$yStart*$xStart; } else { $x1 = $w * sqrt( 1 - pow( $yy,2 ) / pow( $h,2 ) ); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx + $x1 + 1, $cy - $yy, $diffColor); if ($stop < M_PI/2) { $x2 = $yy/$yStop*$xStop; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d2*100 ); imageSetPixel($img, $cx + $x2, $cy - $yy, $diffColor); } } } } if ( $start < M_PI && $stop > M_PI/2 ) { $yy = 0; for ( $x = 0; $x >= $xStop; $x -= 1 ) { if ( $x > $xStart ) { $y1 = $x/$xStart*$yStart; } else { $y1 = $h * sqrt( 1 - pow( $x,2 ) / pow( $w,2 ) ); } $y2 = $x/$xStop*$yStop; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageLine($img, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx + $x, $cy - $y1 - 1, $diffColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d2*100 ); imageSetPixel($img, $cx + $x, $cy - $y2 + 1, $diffColor); for ( $yy; $yy <= $y1; $yy += 1 ) { if ( $yy < $yStop ) { $x1 = -$yy/$yStop*$xStop; } else { $x1 = $w * sqrt( 1 - pow( $yy,2 ) / pow( $h,2 ) ); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx - $x1 - 1, $cy - $yy, $diffColor); if ( $start > M_PI/2 ) { $x2 = $yy/$yStart*$xStart; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d2*100 ); imageSetPixel($img, $cx + $x2, $cy - $yy, $diffColor); } } } } $xStart = $w * cos(max(M_PI,$start)); $yStart = $h * sin(max(M_PI,$start)); $xStop = $w * cos($stop); $yStop = $h * sin($stop); if ( $start < 3*M_PI/2 && $stop > M_PI) { $yy = 0; for ( $x = 0; $x >= $xStart; $x -= 1 ) { if ( $x > $xStop) { $y1 = $x/$xStop*$yStop; } else { $y1 = -$h * sqrt( 1 - pow( $x,2 ) / pow( $w,2 ) ); } $y2 = $x/$xStart*$yStart; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageLine($img, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d1*100 ); imageSetPixel($img, $cx + $x, $cy - $y1 + 1, $diffColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d2*100 ); imageSetPixel($img, $cx + $x, $cy - $y2 - 1, $diffColor); for ( $yy; $yy >= $y1; $yy -= 1 ) { if ( $yy > $yStart ) { $x1 = -$yy/$yStart*$xStart; } else { $x1 = $w * sqrt( 1 - pow( $yy,2 ) / pow( $h,2 ) ); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx - $x1 - 1, $cy - $yy, $diffColor); if ($stop < 3*M_PI/2) { $x2 = $yy/$yStop*$xStop; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d2*100 ); imageSetPixel($img, $cx + $x2 + 1, $cy - $yy, $diffColor); } } } } if ( $start < 2*M_PI && $stop > 3*M_PI/2 ) { $yy = 0; for ( $x = 0; $x <= $xStop; $x += 1 ) { if ( $x < $xStart ) { $y1 = $x/$xStart*$yStart; } else { $y1 = -$h * sqrt( 1 - pow( $x,2 ) / pow( $w,2 ) ); } $y2 = $x/$xStop*$yStop; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageLine($img, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d1*100 ); imageSetPixel($img, $cx + $x, $cy - $y1 + 1, $diffColor); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d2*100 ); imageSetPixel($img, $cx + $x, $cy - $y2 - 1, $diffColor); for ( $yy; $yy >= $y1; $yy -= 1 ) { if ( $yy > $yStop ) { $x1 = $yy/$yStop*$xStop; } else { $x1 = $w * sqrt( 1 - pow( $yy,2 ) / pow( $h,2 ) ); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 100 - $d1*100 ); imageSetPixel($img, $cx + $x1 + 1, $cy - $yy, $diffColor); if ( $start > 3*M_PI/2 ) { $x2 = $yy/$yStart*$xStart; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $d2*100 ); imageSetPixel($img, $cx + $x2 , $cy - $yy, $diffColor); } } } } } ?>