{"id":128,"date":"2010-05-31T21:55:04","date_gmt":"2010-06-01T02:55:04","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=128"},"modified":"2010-05-31T21:55:04","modified_gmt":"2010-06-01T02:55:04","slug":"php-imagick-round-corner-box-with-dropshadow","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2010\/05\/31\/php-imagick-round-corner-box-with-dropshadow\/","title":{"rendered":"php imagick round corner box with dropshadow"},"content":{"rendered":"<p>In this post I will show you how to create a rounded corner box with a drop shadow.  First set your background color, foreground color, and the color of the drop shadow.<\/p>\n<p>Once you&#8217;ve set up your colors, the script will create two identical imagick objects, one for the background, and one for the foreground.  The background is just a base file where you will composite over the drop shadow image and the foreground rounded rectangle.<\/p>\n<p>First the script creates a rounded rectangle within the bounds of the background image with space enough to not cut off the drop shadow.  Once this rectangle is drawn on the second &#8220;over&#8221; image, the script applies a gaussian blur to the image to create the effect of a drop shadow.  Once this is done, the script draws another identical rounded rectangle over the drop shadow, only this time with the foreground color rather than the drop shadow color.<\/p>\n<p>After thats all taken care of, the script composites the drop shadow\/round rectangle image over the background image that was created first.  And, its done.  Uncomment the caching code to write the resulting file to disk.<\/p>\n<pre lang=\"php\">\r\n<?\r\n$background = \"#FFFFFF\";\r\n$foreground = \"#EAEAEA\";\r\n$shadow = \"#666666\";\r\n\r\n\/\/$cachefile = '.\/path\/to\/your\/cachefile.png';\r\n\/\/if (!file_exists($cachefile)) {\r\n\r\n\ttry{\r\n\t\t\t\r\n\t\t$canvas = new Imagick();\r\n\t\t$canvas->newImage(200, 200, $background, \"png\" );\r\n\t\r\n\t\t$box_over = new Imagick();\r\n\t\t$box_over->newImage(200, 200,  $background, \"png\" );\r\n\t\r\n\t\t$draw = new ImagickDraw();\r\n\t\t$draw->setFillColor($shadow);\r\n\t\t$draw->roundRectangle(10, 10, 190, 190, 10, 10);\r\n\t\t\r\n\t\t$box_over->drawImage($draw);\r\n\t\t$box_over->blurImage(8, 5);\r\n\t\t\r\n\t\t$canvas->compositeImage($box_over, imagick::COMPOSITE_OVER, 0, 0);\r\n\t\r\n\t\t$draw = new ImagickDraw();\r\n\t\t$draw->setFillColor($foreground);\r\n\t\t$draw->roundRectangle(10, 10, 190, 190, 10, 10);\r\n\t\r\n\t\t$box_over->drawImage($draw);\r\n\t\t\r\n\t\t$canvas->compositeImage($box_over, imagick::COMPOSITE_OVER, 0, 0);\r\n\t\r\n\t\t$canvas->setImageFormat('jpeg'); \t\r\n\t\r\n\t\t\/\/$canvas->writeImage($cachefile);\r\n\t\r\n\t\theader( \"Content-Type: image\/jpg\" );\r\n\t\techo $canvas;\r\n\t\r\n\t\t$canvas->clear();\r\n\t\t$canvas->destroy();\r\n\t\r\n\t\r\n\t}catch(Exception $e){\r\n\t\techo 'Error: ',  $e->getMessage(), \"\";\r\n\t}\r\n\r\n\/*\r\n}else{\r\n\r\n\t$canvas = new imagick($cachefile);\r\n\theader(\"Content-Type: image\/png\");\r\n\techo $canvas;\r\n\r\n}\r\n*\/\r\n\r\n?>\r\n<\/pre>\n<p><b>NOTE:<\/b> the cachefile folder must be writable by the server. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I will show you how to create a rounded corner box with a drop shadow. First set your background color, foreground color, and the color of the drop shadow. Once you&#8217;ve set up your colors, the script will create two identical imagick objects, one for the background, and one for the foreground. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[64,61,62],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/128"}],"collection":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":1,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/128\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}