Forum Moderators: coopster

Message Too Old, No Replies

creating rounded edges with php

neat:)

         

daniel_o

3:20 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Hi,

as the title indicates.. I need to create a rounded edge with php.
I've got GD2 installed, and I could use a function called imagearc:

imagearc($img, 20, 50, 40, 60, 0, 90, $color);

but that doesn't look well.

Does anyone know if it's possible to create such a neat, good looking image with php/gd ?

thanks.

robognome

4:14 am on Oct 15, 2008 (gmt 0)

10+ Year Member



What's wrong with it the results from imagearc? Aliasing (jagged curves)?

I don't know gd, but does it have any options for "anti-aliasing". Research that. BTW, his is why most designer use pre-created images for rounded corners in site design. (That and probably the fact that many don't want to write programs.)

tomda

5:36 am on Oct 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let me drop this for you to facilitate your task.
This code make arcs, then draw lines and fill the border with the same color. To lastly, cut that color to make transparent corner (PNG) or white corner (JPEG/GIF). The only issue, PNG tend to be much heavier than JPEG/GIF.

But most of us will tell you that rounded edge is usually done with CSS.

// function make rounded-corner rectangle on any picture
function make_rounded_corner($file) {
global $pp_options;
if (file_exists($file)) {
// 1 = GIF, 2 = JPEG, 3 = PNG
$type = getimagesize($file);
$size = getimagesize($file);
// if the associated function doesn't exist - then it's not handle.
if (!function_exists('imagegif') && $type[2] == 1) {
$error = __('Filetype not supported.','photopress');
} elseif (!function_exists('imagejpeg') && $type[2] == 2) {
$error = __('Filetype not supported.','photopress');
} elseif(!function_exists('imagepng') && $type[2] == 3) {
$error = __('Filetype not supported.','photopress');
} else {
switch ($type['2']) {
case '1':
$image = imagecreatefromgif($file);
break;
case '2':
$image = imagecreatefromjpeg($file);
break;
case '3':
$image = imagecreatefrompng($file);
break;
default;
return $error = __('Filetype not supported.','photopress');
}

$h_radius = intval($size[0]*10/100);
$v_radius = intval($size[1]*10/100);

switch ($type['2']) {
case '2':
// jpeg compression quality of output image. 100 is best quality, but fairly large in filesize. 75 is default GD value.
$image_quality = $pp_options['quality'];
// colormagenta is white since we don't apply transparency, we fill the borders
$colorMagenta = imageColorAllocate($image, 255, 255, 255);
break;
default:
$colorMagenta = imageColorAllocate($image, 255, 255, 255);
break;
}

// draw rounded corner
imageArc($image, $h_radius/2, $v_radius/2, $h_radius, $v_radius, 180, -90, $colorMagenta);
imageArc($image, $h_radius/2, ($size[1]-($v_radius/2))-1, $h_radius, $v_radius, 90, 180, $colorMagenta);
imageArc($image, ($size[0]-($h_radius/2))-1, ($size[1]-($v_radius/2))-1, $h_radius, $v_radius, 0, 90, $colorMagenta);
imageArc($image, ($size[0]-($h_radius/2))-1, $v_radius/2, $h_radius, $v_radius, 270, 0, $colorMagenta);
// draw lines
imageLine($image, $h_radius/2, 0, ($size[0]-($h_radius/2))-1, 0, $colorMagenta);
imageLine($image, 0, $v_radius/2, 0, ($size[1]-($v_radius/2))-1, $colorMagenta);
imageLine($image, $h_radius/2, $size[1]-1, ($size[0]-($h_radius/2))-1, $size[1]-1, $colorMagenta);
imageLine($image, $size[0]-1, $v_radius/2, $size[0]-1, ($size[1]-($v_radius/2))-1, $colorMagenta);
// fill the borders
imageFillToBorder($image, 0, 0, $colorMagenta, $colorMagenta);
imageFillToBorder($image, $size[0], $size[1], $colorMagenta, $colorMagenta);
imageFillToBorder($image, 0, $size[1], $colorMagenta, $colorMagenta);
imageFillToBorder($image, $size[0], 0, $colorMagenta, $colorMagenta);

switch ($type['2']) {
case '1':
imageColorTransparent($image, $colorMagenta);
imagegif($image, $file);
break;
case '2':
imagejpeg($image, $file, $image_quality);
//imageColorTransparent($image, $colorMagenta);
//imagepng($image, $file);
break;
case '3':
imageColorTransparent($image, $colorMagenta);
imagepng($image, $file);
break;
}

@imagedestroy($image);
}
}

if (!empty($error)) {
return $error;
} else {
return 1;
}
}