Optimizing image performance on your site in Drupal

Josh Fabean
September 21, 2015
Coffee cup, note pads, keys and a glass bottle on a wooden surface

Keep your site performing even after handing it to Clients.

As a developer you can do everything in your power to make your site really slim & fast but when you hand it to the Client there is still a possibility that the Client could make changes that would adversely affect the final product. There are a plethora of things you need to do to keep this from happening, like handling uploaded images better.

I'm able to accomplish this for our Clients with a couple very simple modules that you should be using on most (if not on all) of your sites, they are:

Picture & Breakpoints

These two modules work together to handle responsive images. The picture module uses Picturefill to do it's magic. If you don't know about Picturefill it is a polyfill that allows you to use the Picture element today even though only Firefox, Chrome (+ Android Browser) & Opera support it as of writing this.

Setting up these modules is pretty straightforward:

  1. Enable Breakpoints & Picture
  2. Set up your breakpoints by going to /admin/config/media/breakpoints.
  • These are setup just like CSS breakpoints and look like @media(min-width: 640px) Image Breakpoints setup
  1. Configure your new Image Styles breakpoints just made for you.
  • If you breakpoint is a max width of 600px you should scale your image to at max that width.
  1. Edit your display on the content you want to use responsive images to use the Image with sizes format instead of just standard Image. Image with Styles as an option
  2. Then you'll have to choose your sizes which will look like this: (min-width: 0px) (min-width: 640px) (min-width: 1025px) (min-width: 1441px).
  3. Under that you choose which Image Styles you'd like to use. You'll want to choose the ones that breakpoints created for us based on the breakpoints you made.
  4. Choose a fallback image size. I have been using my mid size pictures that are at max ~900px wide. I personally feel like that's large enough it should work for people as a fallback even if it's a little pixelated, that's gotta be better than a 1MB image.
  • When all said and done it should look like this: Image with Styles settings finished
  1. Now when you go to your content it should be switching images based on screen size.
Image API Optimize

Image API Optimize lets you modify the default Drupal image optimization settings. You can choose your compression method and compression amount for your images. This is all helpful things, but there is a patch out there that lets you use this module with TinyPNG. If you've never used Tiny PNG, let me just say it's magic and will make your PNGs crazy small.

Once you have patched and enabled Image API Optimize you will have the options to choose which service you want to use. You can now choose TinyPNG

TinyPNG as option for compression

Once you have that setup you will need to clear your image-cache using drush drush image-flush --all. Then the next time you hit a page with an image it will take a minute because it needs to go out can compress these images, but you should notice they're significantly smaller like 60% smaller at times. Now you don't need to worry about your Clients uploading so many PNGs because they need transparencies.

With that very small amount of work we have significantly future proofed the performance of your Client's site by making sure all user uploaded pictures are handled properly.

Want to talk about how we can work together?

Ryan can help

Ryan Wyse