0

I saw this blog post about applying round corner to images using PHP: http://salman-w.blogspot.ca/2009/05/generate-images-with-round-corners-on.html

And the code is below

<?php
/*
 * Apply Round Corner PHP-GD
 * http://salman-w.blogspot.com/2009/05/generate-images-with-round-corners-on.html
 *
 * Adds rounded corners to the specified image
 */

/*
 * source: path or url of a gif/jpeg/png image -- php fopen url wrapper must be enabled if using url
 * radius: corner radius in pixels -- default value is 10
 * colour: corner colour in rgb hex format -- default value is FFFFFF
 */

$source = $_GET["source"];
$radius = isset($_GET["radius"]) ? $_GET["radius"] : 10;
$colour = isset($_GET["colour"]) ? $_GET["colour"] : "FFFFFF";

/*
 * open source image and calculate properties
 */

list($source_width, $source_height, $source_type) = getimagesize($source);
switch ($source_type) {
    case IMAGETYPE_GIF:
        $source_image = imagecreatefromgif($source);
        break;
    case IMAGETYPE_JPEG:
        $source_image = imagecreatefromjpeg($source);
        break;
    case IMAGETYPE_PNG:
        $source_image = imagecreatefrompng($source);
        break;
}

/*
 * create mask for top-left corner in memory
 */

$corner_image = imagecreatetruecolor(
    $radius,
    $radius
);

$clear_colour = imagecolorallocate(
    $corner_image,
    0,
    0,
    0
);

$solid_colour = imagecolorallocate(
    $corner_image,
    hexdec(substr($colour, 0, 2)),
    hexdec(substr($colour, 2, 2)),
    hexdec(substr($colour, 4, 2))
);

imagecolortransparent(
    $corner_image,
    $clear_colour
);

imagefill(
    $corner_image,
    0,
    0,
    $solid_colour
);

imagefilledellipse(
    $corner_image,
    $radius,
    $radius,
    $radius * 2,
    $radius * 2,
    $clear_colour
);

/*
 * render the top-left, bottom-left, bottom-right, top-right corners by rotating and copying the mask
 */

imagecopymerge(
    $source_image,
    $corner_image,
    0,
    0,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    0,
    $source_height - $radius,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    $source_width - $radius,
    $source_height - $radius,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    $source_width - $radius,
    0,
    0,
    0,
    $radius,
    $radius,
    100
);

/*
 * output the image -- revise this step according to your needs
 */

header("Content-type: image/png");
imagepng($source_image);
?>

My question is, how do I use this code on a image that I upload using a html form, so that the image is automatically saved with round corner already applied? ie. How do I:

  1. Apply the code to an uploading/uploaded image
  2. output this image to a local file instead of feeding to the browser
James Gu
  • 1,382
  • 4
  • 26
  • 39

2 Answers2

2

I would advise against applying this treatment to the user's images on the server, if your design requirements ever change there is no way to go back unless you are also save a copy of the original image. You can achieve the same effect using the css border-radius property.

 <img src='/path/to/image.png' style='border-radius: 5px;'/>
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • +1. Was going to suggest the same. You can even make images circle-shaped if you increase the radius. – James P. Jul 29 '13 at 05:20
  • 1
    @jamespoulson [triangles too](http://stackoverflow.com/questions/10969941/css3-triangle-shape-with-background-image) – Orangepill Jul 29 '13 at 05:21
0

try something like this. make sure your form tag looks like this:

<form action="..." method="post" enctype="multipart/form-data">

then your php code should look like this:

$data = file_get_contents($_FILES['uploadedfile']['tmp_name'])
$image = imagecreatefromstring($data)

then apply your process to $image

DAB
  • 1,303
  • 14
  • 23