Jump to content

Rounded rectangle with GD


evanct

Recommended Posts

What am I doing wrong here? This saves a plain rectangle without rounded corners to the specified directory. Turning on notices tells me nothing.

 

<?php

$tmpimg=imagecreatetruecolor(200,200);
$color=imagecolorallocate($tmpimg,0,0,0);
roundedRectangle($tmpimg,0,0,200,200,20,$color);
imagejpeg($tmpimg,$path.$filename);

function roundedRectangle($im,$x,$y,$end_x,$end_y,$radius,$color)
{
//create two rectangles to form a sort of "cross" shape

imagefilledrectangle($im,$x,$y+$radius,$end_x,$end_y-$radius,$color);
    imagefilledrectangle($im,$x+$radius,$y,$end_x-$radius,$end_y,$color);

    $dia = $radius*2;

//fill in the corners of the "cross" with ellipses to form a rounded rectangle

    imagefilledellipse($im, $x+$radius, $y+$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $x+$radius, $end_y-$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $end_x-$radius, $end_y-$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $end_x-$radius, $y+$radius, $radius*2, $dia, $color);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/159952-rounded-rectangle-with-gd/
Share on other sites

Your function isn't returning anything

try this

<?php

$tmpimg=imagecreatetruecolor(200,200);
$color=imagecolorallocate($tmpimg,0,0,0);
$tmpimg = roundedRectangle($tmpimg,0,0,200,200,20,$color);
imagejpeg($tmpimg,$path.$filename);

function roundedRectangle($im,$x,$y,$end_x,$end_y,$radius,$color)
{
//create two rectangles to form a sort of "cross" shape

   imagefilledrectangle($im,$x,$y+$radius,$end_x,$end_y-$radius,$color);
    imagefilledrectangle($im,$x+$radius,$y,$end_x-$radius,$end_y,$color);

    $dia = $radius*2;

//fill in the corners of the "cross" with ellipses to form a rounded rectangle

    imagefilledellipse($im, $x+$radius, $y+$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $x+$radius, $end_y-$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $end_x-$radius, $end_y-$radius, $radius*2, $dia, $color);
    imagefilledellipse($im, $end_x-$radius, $y+$radius, $radius*2, $dia, $color);
return $im;
}

?>

When an image is created with imagecreatetruecolor() it has a black background by default.  Try changing the color your rectangle is drawn in from black to white, and it should work.

 

That worked. I see what's going on: I was simply mistaken about the nature of imagefilledrectangle and imagefilledellipse. I thought they would create a rounded rectangle SHAPE, but all they do is create a rounded rectangle within the rectangle created with imagecreatetruecolor. what i'm ultimately trying to do here is dynamically round the corners of photos i upload. Is there even a way to accomplish that with GD? If not that's fine, it's only a design preference, not strictly necessary.

 

Essentially is there a way to create a transparent rectangle with imagecreatetruecolor?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.