{"id":498,"date":"2010-03-23T14:29:26","date_gmt":"2010-03-23T08:59:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=498"},"modified":"2011-06-16T12:20:07","modified_gmt":"2011-06-16T06:50:07","slug":"creating-image-thumbnails-through-imagetools-plugin","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-image-thumbnails-through-imagetools-plugin\/","title":{"rendered":"Creating image thumbnails through Grails ImageTools plugin"},"content":{"rendered":"<p>In one of the projects recently, we had to generate the thumbnails dynamically from the client-provided images. We decided to use the Grails plugin named: ImageTools. This is a very basic plugin but provided very quick and stable solution to our use-case. You can read more about the plugin and how to install it <a href=\"http:\/\/www.grails.org\/ImageTools+plugin\" target=\"_blank\"> here <\/a>.<\/p>\n<p>We decided to implement the solution in the form of a tag which gave us the ease to use it directly in a .gsp file. Here is how the code looked like in a taglib:<\/p>\n<blockquote>\n<div>\n<pre>\r\nimport org.grails.plugins.imagetools.ImageTool  \/\/ Import the ImageTool class in your taglib.\r\n\r\ndef thumbnail = { attrs -&gt;;\r\ndef imgSrc = attrs['imgSrc']\u00a0 \/\/ as the name suggests - the image source\r\ndef dimension = attrs['dimension'].toInteger()\u00a0 \/\/size of the image - giving 200 as a dimension gives you the thumbnail of around 200*200 in size.\r\ndef imageTool = new ImageTool()\r\nimageTool.load(imgSrc)\r\nimageTool.square()\r\nimageTool.thumbnail(dimension)\r\nimageTool.writeResult(thumbnailLocation,\"JPEG\")\u00a0 \/\/thumbnailLocation - provides the path to where you want to store the thumbnail image.\r\nout &lt;&lt; thumbnailLocation\r\n}\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>This is all you need to create the thumbnail programatically through ImageTools plugin. The code above always creates the new thumbnail; we tried to improvise the above solution by saving the thumbnail in a folder and checking for the existence of the thumbnail before actually creating the thumbnail. This is how we achieved it:<\/p>\n<blockquote>\n<div>\n<pre>def thumbnail = {attrs -&gt;;\r\ndef imgSrc = attrs['imgSrc']\r\ndef dimension = attrs['dimension'].toInteger()\r\ndef thumbLocation = \"\/path\/to\/store\/thumbnails\"\u00a0 + imgSrc\r\ndef file\r\n\r\n\/\/ if thumbnail already exists, return the path to the image\r\nif((file =\u00a0 new File(thumbLocation)).exists()){\r\nout &lt;&lt; thumbLocation\r\n}\r\nelse{\r\n\/\/ the code to generate the thumbnail comes here.\r\n}\r\n}\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Hope this helps.<\/p>\n<p>&#8212; Abhishek<\/p>\n<p>Intelligrape Software<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the projects recently, we had to generate the thumbnails dynamically from the client-provided images. We decided to use the Grails plugin named: ImageTools. This is a very basic plugin but provided very quick and stable solution to our use-case. You can read more about the plugin and how to install it here [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[4840,191],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/498"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=498"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/498\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}