Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.