March 04th, 2010
Round image corners with php
Topics: Howto, Php, CodeI needed to round the corners of a banner image dynamically in PHP and I couldn’t find any explanations. Anyway it turns out the easiest way is to make an image of a rounded corner, like this rounded-corner.png and then stamp it on all 4 corners. You have to rotate it each time of course.Anyway here’s a snipplet of code to give you an idea:
<?php
$banner = '/path/to/banner-image.png';
$corner = '/path/to/rounded-corner.png';
// Load up the images
$banner = open_image($banner);
$corner = open_image($corner);
// Round the corner
imagecopy($banner, $corner, 0, 0, 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, 0, imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), 0, 0, 0, imagesx($corner), imagesy($corner));
// Output the image
header("Content-type: image/gif");
imagegif($banner);
// Tidy up
imagedestroy($banner);
imagedestroy($corner);
?>