drupal|April 18, 2020|2 min read

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

TL;DR

Combine multiple image style effects like Scale, Crop, and Manual Crop to handle both horizontally and vertically long images while maintaining aspect ratio.

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement

I’ve been using image styles, and heavily used “Scale and crop” style, which worked for me in almost 95% of the cases. Recently, I’ve been getting images which are like horizontally long or vertically long. Default image styles could not work for any of combination.

Below is one example image, and its result (Prior to my solution)

Original Image

Drupal Image Style

Poor Result

Drupal Image Style

Lets take a look at how I solve this problem.

Solution to Correctly styling images

I found the two hero modules:

After installing them, Please follow the steps:

  1. Goto /admin/config/media/crop, and create a new crop type

Drupal Image Style

In aspect ratio: put 16:9, or you can put your desired one. Leave rest of the field as it is. Save it.

  1. Goto /admin/config/media/image-styles and create a new image style
  2. Add a new effect: Manual Crop, select your crop type you created in above step.
  3. Add another effect: Scale, and put following:
  • Height = 500. You can give your desired height
  • Leave width field as it is.
  • Uncheck Allow Upscaling

Scaling effect configuration

Drupal Image Style

Final Manual Crop configuration

Drupal Image Style

  1. Goto your content type, and click on Manage Display and select your style you created in above step.

Drupal Image Style

Now, see your content. It should work as expected.

Note

You might have doubt for above 2nd effect (Scaling) in image style. If you leave that image style with only Manual Crop effect. Your images might display in expected size. I encountered a case where image was vertically long. For that, I had to put a scaling configuration as well. And if you noted I did not specify the width. It will automatically maintain aspect ratio. And, the max height for all images will be the value I specified.

Result

See the results:

For Horizontally long image

This example image I have put on top.

Original Image

Drupal Image Style

After manual crop configuration

Drupal Image Style

For vertically long image

Original Image

Drupal Image Style

After manual crop configuration

Drupal Image Style

Let me know if you have any doubts in comments.

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Introduction Drupal provides a powerful comment module, which comes as a part of…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…