{"id":47847,"date":"2017-05-15T16:20:06","date_gmt":"2017-05-15T10:50:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=47847"},"modified":"2017-05-16T11:26:13","modified_gmt":"2017-05-16T05:56:13","slug":"drupal-responsive-image-style","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/drupal-responsive-image-style\/","title":{"rendered":"Drupal Responsive Image Style"},"content":{"rendered":"<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-47915\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/banner.jpg\" alt=\"banner\" width=\"887\" height=\"377\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/banner.jpg 887w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/banner-300x127.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/banner-624x265.jpg 624w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/span><\/p>\n<h3><span style=\"color: #000000;\">What is this?<\/span><\/h3>\n<p><span style=\"color: #000000;\">In <a href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\">Drupal 8<\/a>, image style is used to set the presets for image processing. We can set properties like crop, rotate, and\u00a0scale. If we display an image with any image style, a new image file is created and being used instead of an original image, and no changes are done in the original image.<\/span><\/p>\n<p><span style=\"color: #000000;\">Responsive image style is the combination of more than one image style. To fulfill this requirement we have a module <strong>&#8220;Responsive image&#8221;<\/strong> in drupal core.<\/span><\/p>\n<h3><span style=\"color: #000000;\">Why We need this?<\/span><\/h3>\n<p><span style=\"color: #000000;\">Nowadays, most of the traffic comes to a site via mobile or other portable devices, a developer should optimize web pages to cater these devices smartly, by using less amount of internet data. Responsive image style adds a step into this.<br \/>\n<\/span><br \/>\n<span style=\"color: #000000;\">Suppose a situation where we have a multi-image banner on top of an image. For this we are using images of dimension 1600px X 450px. This is fine with the desktop view, but when we view this page on small devices, do we still need this high-resolution image? The answer is &#8220;No&#8221; as small devices do not have that much of width. So when we display the same image, the image is being scaled by device width itself, in that case, we have achieved the proper dimension according to the device&#8217;s width, but the file size is still the same that of an original. Now the idea came that why we need a 1600px wide image on small devices which likely have a size of around 320px to 640 px. High resolution for desktop is ok but shouldn&#8217;t we use small resolution image for small devices?<\/span><\/p>\n<h3><span style=\"color: #000000;\">How to implement it?<\/span><\/h3>\n<p><span style=\"color: #000000;\">As said earlier we have a module\u00a0<strong>&#8220;Responsive image&#8221;<\/strong> in drupal as a core module. We can use this module. Responsive image module works with <strong><a style=\"color: #000000;\" title=\"Breakpoints\" href=\"https:\/\/www.drupal.org\/project\/breakpoints\" target=\"_blank\">Breakpoints<\/a><\/strong> Module. First, we need to enable this module:<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Goto: \/admin\/modules <\/strong>and enable this.<\/span><\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-47885 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/install-module.jpg\" alt=\"\" width=\"1007\" height=\"132\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/install-module.jpg 1007w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/install-module-300x39.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/install-module-624x81.jpg 624w\" sizes=\"(max-width: 1007px) 100vw, 1007px\" \/><\/span><\/p>\n<table class=\"table-bordered\" width=\"100%\" cellpadding=\"10\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><strong>Devices<\/strong><\/span><\/td>\n<td><span style=\"color: #000000;\"><strong>Image size<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Desktop<\/span><\/td>\n<td><span style=\"color: #000000;\">1600px X 450px<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Tablets<\/span><\/td>\n<td><span style=\"color: #000000;\">850px X 300px<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Mobiles<\/span><\/td>\n<td><span style=\"color: #000000;\">640px X 250px<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">So we will create 3 Image styles for given three presets.<\/span><\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-47884\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/image-styles.jpg\" alt=\"image-styles\" width=\"1030\" height=\"316\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/image-styles.jpg 1030w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/image-styles-300x92.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/image-styles-1024x314.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/image-styles-624x191.jpg 624w\" sizes=\"(max-width: 1030px) 100vw, 1030px\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\">Now we need to create a Responsive image style:<\/span><br \/>\n<span style=\"color: #000000;\">If we look into Media section under configuration page, we have now <strong>\u00a0\u201cResponsive image styles\u201d<\/strong> menu available there. So let\u2019s create a responsive image style:<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Create a responsive image style by any name.<\/span><\/li>\n<li><span style=\"color: #000000;\">Select the breakpoints you want to use, I\u2019m using bartik, (we can also make our own <a style=\"color: #000000;\" href=\"https:\/\/www.drupal.org\/docs\/8\/theming-drupal-8\/working-with-breakpoints-in-drupal-8\">breakpoints<\/a><span style=\"font-weight: 400;\">).<\/span><\/span><\/li>\n<li><span style=\"color: #000000;\">Now select the image style from the option you want for each breakpoint.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-47887\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/select-image-style.jpg\" alt=\"select-image-style\" width=\"1030\" height=\"516\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/select-image-style.jpg 1030w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/select-image-style-300x150.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/select-image-style-1024x512.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/select-image-style-624x312.jpg 624w\" sizes=\"(max-width: 1030px) 100vw, 1030px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400; color: #000000;\">If your user is on an old browser that does not support &lt;picture&gt; tag, you can set the smallest image style as a fallback image style.<br \/>\n<\/span><br \/>\n<span style=\"color: #000000;\">Now click on <strong>&#8220;Save&#8221;<\/strong> button.<\/span><\/p>\n<p><span style=\"color: #000000;\">Now we are ready to use this created Responsive image Style.<\/span><\/p>\n<h3><span style=\"color: #000000;\">How to use this?<\/span><\/h3>\n<p><span style=\"color: #000000;\">We can use this responsive image style for any content type.<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Go to manage display and select the <strong>&#8220;Responsive image&#8221;<\/strong>\u00a0for <strong>&#8220;Format&#8221;<\/strong> of the image field.<\/span><\/li>\n<li><span style=\"color: #000000;\">Once done then click on the gear icon and select that \u201cresponsive image style\u201d which we created recently.<\/span><\/li>\n<li><span style=\"color: #000000;\">Click on &#8220;Update&#8221; button and then <strong>&#8220;Save&#8221;<\/strong>\u00a0it.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-47899\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/activate-image-style.jpg\" alt=\"activate-image-style\" width=\"1025\" height=\"318\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/04\/activate-image-style.jpg 1025w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/activate-image-style-300x93.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/activate-image-style-1024x317.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/04\/activate-image-style-624x193.jpg 624w\" sizes=\"(max-width: 1025px) 100vw, 1025px\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\">Now go to the page and resize your browser to see the magic.You will see that each breakpoint is using different image style what we have configured.<\/span><\/p>\n<h3><span style=\"color: #000000;\">Wondering how it works?<\/span><\/h3>\n<p><span style=\"color: #000000;\">Responsive image module results\u00a0in\u00a0HTML\u00a0which can hold multiple image source for different breakpoints(devices).<\/span><\/p>\n<p><span style=\"color: #000000;\"> Tag allow the browser to load only that image which is explicitly stated for use for defined device&#8217;s size.<\/span><\/p>\n<p>[java]<br \/>\n&lt;picture&gt;<br \/>\n&lt;source srcset=&quot;large.jpg&quot; media=&quot;(min-width: 851px)&quot;&gt;<br \/>\n&lt;source srcset=&quot;medium.jpg&quot; media=&quot;(min-width: 560px) and (max-width: 850px)&quot; alt=&quot;My default image&quot;&gt;<br \/>\n&lt;source srcset=&quot;small.jpg&quot;&gt;&lt;img srcset=&quot;default_image.jpg&quot; alt=&quot;My default image&quot;&gt;<br \/>\n&lt;\/picture&gt;<br \/>\n[\/java]<\/p>\n<p><span style=\"color: #000000;\">Hope this information will be useful to you.If you have any questions or comments about this blog, please leave them below.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is this? In Drupal 8, image style is used to set the presets for image processing. We can set properties like crop, rotate, and\u00a0scale. If we display an image with any image style, a new image file is created and being used instead of an original image, and no changes are done in the [&hellip;]<\/p>\n","protected":false},"author":1143,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"link","meta":{"iawp_total_views":11},"categories":[3602,3429,1],"tags":[4862,3601,3738,4842,4556],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/47847"}],"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\/1143"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=47847"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/47847\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=47847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=47847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=47847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}