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

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

In 2025, 72% of professional developers used AI-assisted coding tools daily. By…

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL injection has been on the OWASP Top 10 since the list was created in 200…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…