I'm trying to combine this script:
http://www.assemblysys.com/dataServices/php_roundedCorners.php
and this script:
http://www.hawkee.com/snippet/5022/
to produce rounded rectangle gradient buttons but having no luck.
It is just showing a top-left corner in black.
GD is not one of my strong points and I need some help.
BTW, I also want each button to have a colored border, haven't
figured out how to do that yet.
<?php
/*
* "start" (optional): color value to begin gradient
* "end" (optional): color value to end gradient
* "radius" (optionnal): value that represents (in pixels) the radius of the corners (used to resize the corner image)
// Samples:
* src='gradient_btn.php?radius=40'
* src='gradient_btn.php?start=FFFF40&end=000000&border=FF0000&radius=40'
*/
$start = isset($_GET["start"]) ? $_GET["start"] : '000000'; // default start color = black
$end = isset($_GET["end"]) ? $_GET["end"] : 'FFFFFF'; // defualt end color = white
$border_color = isset($_GET["border"]) ? $_GET["border"] : '000000'; // defualt border color = black
$height = isset($_GET["height"]) ? $_GET["height"] : 20; // default height = 20
$width = isset($_GET["width"]) ? $_GET["width"] : 100; // default width = 100
$corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 5; // default corner radius is set to 50px
$topleft = true; // Top-left rounded corner is shown by default
$bottomleft = true // Bottom-left rounded corner is shown by default
$bottomright = true; // Bottom-right rounded corner is shown by default
$topright = true; // Top-right rounded corner is shown by default
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);
for($y=0;$y<$height;$y++) {
for($x=0;$x<$width;$x++) {
if ($start_r == $end_r) {
$new_r = $start_r;
}
$difference = $start_r - $end_r;
$new_r = $start_r - intval(($difference / $height) * $y);
if ($start_g == $end_g) {
$new_g = $start_g;
}
$difference = $start_g - $end_g;
$new_g = $start_g - intval(($difference / $height) * $y);
if ($start_b == $end_b) {
$new_b = $start_b;
}
$difference = $start_b - $end_b;
$new_b = $start_b - intval(($difference / $height) * $y);
$row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
imagesetpixel($image, $x, $y, $row_color);
}
}
switch($corner_radius) {
case 5:
$corner_source = imagecreatefrompng('img/rounded_corner_05px.png');
break;
case 10:
$corner_source = imagecreatefrompng('img/rounded_corner_10px.png');
break;
case 20:
$corner_source = imagecreatefrompng('img/rounded_corner_20px.png');
break;
case 30:
$corner_source = imagecreatefrompng('img/rounded_corner_30px.png');
break;
case 40:
$corner_source = imagecreatefrompng('img/rounded_corner_40px.png');
break;
} // switch
$corner_width = imagesx($corner_source);
$corner_height = imagesy($corner_source);
$corner_resized = imagecreatetruecolor($corner_radius, $corner_radius);
imagecopyresampled($corner_resized, $corner_source, 0, 0, 0, 0, $corner_radius, $corner_radius, $corner_width, $corner_height);
$corner_width = imagesx($corner_resized);
$corner_height = imagesy($corner_resized);
$image = imagecreatetruecolor($corner_width, $corner_height);
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,0,0,0);
$size = array($height, $width);
// Top-left corner
if ($topleft == true) {
$dest_x = 0;
$dest_y = 0;
imagecolortransparent($corner_resized, $black);
imagecopymerge($image, $corner_resized, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
// Bottom-left corner
if ($bottomleft == true) {
$dest_x = 0;
$dest_y = $size[1] - $corner_height;
$rotated = imagerotate($corner_resized, 90, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
// Bottom-right corner
if ($bottomright == true) {
$dest_x = $size[0] - $corner_width;
$dest_y = $size[1] - $corner_height;
$rotated = imagerotate($corner_resized, 180, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
// Top-right corner
if ($topright == true) {
$dest_x = $size[0] - $corner_width;
$dest_y = 0;
$rotated = imagerotate($corner_resized, 270, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
// do the border here
header("Content-type: image/png");
// Output final image
imagepng($image);
imagedestroy($image);
imagedestroy($corner_source);
?>