<?php
$imgpath = "source.jpg";
$corner = 20;
$image = open_image($imgpath);
image_roundcorners($image, $corner);
//display rounded image
imagepng($image,"output.png");
echo '<html><body bgcolor="#888888" style="text-align:center;padding-top:100px">';
echo '<img src="'.$imgpath.'" /> <img src="output.png" />';
echo '</body></html>';
imagedestroy($image);
// image_roundcorners() - takes a single image by reference and puts transparent rounded corners on it with radius $corner
// XXX currently does not support antialiasing :(
function image_roundcorners(&$image, $corner) {
$width = imagesx($image);
$height = imagesy($image);
imagealphablending($image, false); // we'd like the alpha we draw to stick
imagesavealpha($image,true);
// allocate an alpha transparent color
$trans = imagecolorallocatealpha($image, 255, 255, 255, 127);
//draw corners
imagearc($image, $corner-1, $corner-1, $corner*2, $corner*2, 180, 270, $trans);
imagefilltoborder($image, 0, 0, $trans, $trans);
imagearc($image, $width-$corner, $corner-1, $corner*2, $corner*2, 270, 0, $trans);
imagefilltoborder($image, $width, 0, $trans, $trans);
imagearc($image, $corner-1, $height-$corner, $corner*2, $corner*2, 90, 180, $trans);
imagefilltoborder($image, 0, $height, $trans, $trans);
imagearc($image, $width-$corner, $height-$corner, $corner*2, $corner*2, 0, 90, $trans);
imagefilltoborder($image, $height, $height, $trans, $trans);
imagealphablending($image, true); // turn blending back on
}
// open_image($path) will load an image into memory so we can work with it, and return an error if we fail
function open_image($path) {
if (!is_string($path)) return $path; // XXX assume binary data is a gd image already
$image = @imagecreatefromstring(file_get_contents($path));
if (!$image) die("could not open image ($path) make sure it exists");
imagealphablending($image,true); imagesavealpha($image,true); // preserve transparency
return $image;
}
?>