简体   繁体   English

带有圆角的图像填充矩形的函数

[英]function for imagefilledrectangle with rounded corners

I'm willing to create an image with certain text using the following code我愿意使用以下代码创建带有特定文本的图像

<?PHP
header('Content-Type: image/png');

$im = imagecreatetruecolor(320, 80);

$blue = imagecolorallocate($im, 59, 89, 152);
$sky = imagecolorallocate($im, 219, 241, 255);

imagefilledrectangle($im, 0, 0, 399, 79, $sky);

$font = 'arial.ttf';

$text = "Hello world"; 

imagettftext($im, 15, 0, 10, 20, $blue, $font, $text); 
imagepng($im);
imagedestroy($im);
?>

Now the out put will be as following image现在输出将如下图

this is rectangle这是矩形

矩形图像

Now What if i want to made to it a rounded corner by refer to php manual about function imagefilledrectangle I've found a good comment with function said to be able to make it rounded corner现在,如果我想通过参考关于函数 imagefilledrectangle 的php 手册将它变成一个圆角怎么办我发现了一个很好的评论,函数说能够使它变成圆角

<?

function ImageRectangleWithRoundedCorners(&$im, $x1, $y1, $x2, $y2, $radius, $color)
{
// draw rectangle without corners
imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
// draw circled corners
imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
}

?>

But didn't mention how to use !但是没说怎么用! ~ anyhelp ~ 任何帮助

In case anybody needs the background behind the radius to be transparent, this function can be used (I modified the one posted by OP) =>如果有人需要半径后面的背景是透明的,可以使用这个功能(我修改了OP发布的那个)=>

function image_rectangle_w_rounded_corners(&$im, $x1, $y1, $x2, $y2, $radius, $color) {
    $alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
    
    // draw rectangle without corners
    imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
    imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
    
    // draw circled corners
    imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
    
    // alpha radius bg
    $width = imagesx($im);
    $height = imagesy($im) - 0.01;
    
    imagefill($im, 0, 0, $alpha);
    imagefill($im, $width, 0, $alpha);
    imagefill($im, 0, $height, $alpha);
    imagefill($im, $width, $height, $alpha);
}

Of course - transparency can be switched for any other color.当然 - 透明度可以切换为任何其他颜色。

Its an old question, but may be someone will benefit from this answer.这是一个老问题,但可能有人会从这个答案中受益。 This is a very nice function!这是一个非常好的功能! Here is how to use it:以下是如何使用它:

x1 = the horizontal x point where you want rectangle to start
x2 = the horizontal x point where you want rectangle to end
y1= the vertical y point where rectangle starts
y2 = the vertical y point where rectangle ends

Now the radius.现在半径。 Set the radius to "half" of difference between y2 and y1将半径设置为 y2 和 y1 之间差异的“一半”

Suppose your y2 is 100, and y1 is 50, then radius will be:假设您的 y2 为 100,y1 为 50,那么半径将为:

( y2 - y1 )/2
( 100 - 50 )/2 = 25

Couple of examples are here: http://vagminetech.com/rect.htm这里有几个例子: http : //vagminetech.com/rect.htm

For a wide rectangle: x1=50 x2=250 y1=50 y2=100 r=25对于宽矩形:x1=50 x2=250 y1=50 y2=100 r=25

For a tall rectangle x1=50 x2=150 y1=50 y2=150 r=50对于高矩形 x1=50 x2=150 y1=50 y2=150 r=50

Regards问候

let me try让我试试

probably大概

$x1 = top left point's x coordinate
$y1 = top left point's y coordinate
$x2 = bottom right point's x coordinate
$y2 = bottom right point's y coordinate
$radius = radius of the curve of rounded corner

set this data and try... good luck :)设置此数据并尝试...祝你好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM