php imagick add round corners to a jpg image

One might think this would be an easy task, and it is, unless you want it to look good. Unfortunately, imagick really only performs well when processing images with an alpha channel. I’ve found that PNG format works best. So, to add rounded corners to a jpg image with php’s imagick object, there are a few steps involved.

First, you have to create an imagick object from the source image. Then, convert it to PNG format before applying the rounded corners. Once in PNG format, the script applies the rounded corners. If you dont care about having the large size of a png file, you can simply output the the browser at this point, but, if you want the smaller file size of a jpg, you must take a few more steps.

After adding the rounded corners to your now PNG file, you must create another imagick image object of the same size as your original image, with your desired background color, of image type jpg. Then, you composite the rounded corner png over the new solid background color image you just created.

Now that you have your rounded corner image over the correct background and in jpg format, you can set the image compression type and quality. Once this is done, you are ready to output the resulting jpg to the browser. There is cache code commented out if you would like to save the resulting jpg to disk.

<?
 
$background = "#FFFFFF";
$imagefile = "/path/to/your/image.jpg";
$cornerradius_x = 50;
$cornerradius_y = 50;
$quality = 70;
 
//$cachefile = '/path/to/your/cachedimage.jpg';
//if (!file_exists($cachefile)) {
 
 
	try{
 
		$canvas = new Imagick($imagefile);
 
		$canvas->setImageFormat('PNG');
		$canvas->roundCorners($cornerradius_x,$cornerradius_y);
 
		$canvas2 = new Imagick();
		$canvas2->newimage($canvas->getImageWidth(),  $canvas->getImageHeight(), $background, 'jpg');
 
		$canvas2->compositeImage($canvas, imagick::COMPOSITE_OVER, 0, 0);
 
		$canvas2->setImageFormat('JPG');
		$canvas2->setImageCompression(Imagick::COMPRESSION_JPEG);
		$canvas2->setImageCompressionQuality($quality); 
 
		//$canvas2->writeImage($cachefile);
 
		header( "Content-Type: image/jpg" );
		echo $canvas2;
 
		$canvas->clear();
		$canvas->destroy();
		$canvas2->clear();
		$canvas2->destroy();	
 
	}catch(Exception $e){
		echo 'Error: ',  $e->getMessage(), "";
	}
 
/*
}else{
 
	$canvas = new imagick($cachefile);
	header("Content-Type: image/png");
	echo $canvas;
 
}
*/
 
?>

NOTE: your “cachefile.jpg” folder must be writable by the server.

3 Comments to php imagick add round corners to a jpg image

  1. Aditya Sharma's Gravatar Aditya Sharma
    November 27, 2012 at 5:05 am | Permalink

    Hi thanks for a brilliant code, i really need your help i also wish to add a colored border to the round image file but somehow the entire round portion of both the png and the jpeg file are also getting colored

    I just want that the round image that is getting generated should also have a Black Border to it

    Can you please help me out over here

  2. james's Gravatar james
    December 7, 2012 at 3:40 pm | Permalink

    If you want to add a border, you will have to create a shape layer using a rounded rectangle object over your composite layer, then apply a stroke to that shape. Might take some tweaking to make the rounded corners match but it shouldnt be too difficult. Check out this post for details on a rounded corner rectangle:

    http://jamesroberts.name/blog/2010/05/31/php-imagick-button-with-round-corners-and-a-bezier-curve/

  3. January 11, 2017 at 8:23 pm | Permalink

    Awesome pist, i liked it

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>