{"id":103,"date":"2010-05-30T01:28:12","date_gmt":"2010-05-30T06:28:12","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=103"},"modified":"2010-05-30T16:53:46","modified_gmt":"2010-05-30T21:53:46","slug":"php-imagick-simple-font-rendering-with-automatic-image-canvas-size","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2010\/05\/30\/php-imagick-simple-font-rendering-with-automatic-image-canvas-size\/","title":{"rendered":"php imagick simple font rendering with automatic image canvas size"},"content":{"rendered":"<p>I have been working with the image imagick library for PHP (<a href=\"http:\/\/us.php.net\/manual\/en\/book.imagick.php\" target=\"_blank\">ImageMagick<\/a>) for some time now.  I figure its time to share what I have discovered and figured out.  There is very little documentation on this amazing library for PHP, but hopefully my hours of trial and error can help those who are interested in using it.  For my first post, I will share a script I wrote which will render a png image for text using a ttf font.  <\/p>\n<p>The script uses imagick&#8217;s ImagickDraw object to first load the font, set the font size, orientation (GRAVITY_CENTER in this example, but you could set it to GRAVITY_NORTHWEST, or GRAVITY_SOUTHEAST), and fill color.  Then the script gets the proper canvas size for the Imagick image object before the image is created, so you don&#8217;t need to worry about having to &#8220;know&#8221; what size the image needs to be.  I have found that the metrics ImagickDraw outputs is not always perfect, so I have added a bit of padding to the image size that is calculated.<\/p>\n<p>Next, the script creates a new Imagick image object and sets it&#8217;s size to what was calculated in the first step.  It then &#8220;draws&#8221; the font that was set up in the first step.  Once this is done, the script sets the image format (&#8220;PNG&#8221; in this example, but it could be a gif, or jpg if you want).  The script finally saves the cache file, then sets the content type header and outputs the image to the browser.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\r\n$fontpath = \"path\/to\/your\/fontfile.ttf\";\r\nif(!file_exists($fontpath)) die(\"Error!  Font file does not exist at '\".$fontpath.\"'\");\r\n\r\n$cachefile = \"path\/to\/your\/cachefile.png\";\r\n$text = \"ABCD abcd 1234\";\r\n$fillcolor = \"#999999\";\r\n\r\n\r\nif (!file_exists($cachefile)) {\r\n\r\n\ttry{\r\n\t\r\n\t\t$draw = new ImagickDraw();\r\n\t\t$draw->setFont($fontpath);\r\n\t\t$draw->setFontSize(20);\r\n\t\t$draw->setGravity(Imagick::GRAVITY_CENTER);\r\n\t\t$draw->setFillColor($fillcolor);\r\n\t\t\r\n\t\t$canvas = new Imagick();\r\n\t\t\r\n\t\t$metrics = $canvas->queryFontMetrics($draw,$text);\r\n\t\t\r\n\t\t$canvas->newImage($metrics['textWidth'], $metrics['textHeight'], \"transparent\", \"png\");\r\n\t\t$canvas->annotateImage($draw,0,0,0,$text);\r\n\t\t\r\n\t\t$canvas->setImageFormat('PNG');\r\n\t\t$canvas->writeImage($cachefile);\r\n\t\r\n\t\theader(\"Content-Type: image\/png\");\r\n\t\techo $canvas;\r\n\t\r\n\t\t$canvas->clear();\r\n\t\t$canvas->destroy();\r\n\t\t\r\n\t\t$draw->clear();\r\n\t\t$draw->destroy();\r\n\t\r\n\t}catch(Exception $e){\r\n\t\techo 'Error: ',  $e->getMessage(), \"\";\r\n\t}\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\t\r\n}\r\n\r\ndie();\r\n\r\n?>\r\n<\/pre>\n<p><b>NOTES:<\/b> Your cache file path must be writable by the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been working with the image imagick library for PHP (ImageMagick) for some time now. I figure its time to share what I have discovered and figured out. There is very little documentation on this amazing library for PHP, but hopefully my hours of trial and error can help those who are interested in [&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,62],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/103"}],"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=103"}],"version-history":[{"count":6,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions\/115"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}