~/drupal/drupal-image-style-maintain-aspect-ratio-handling-small-images.md

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

2 min readApril 18, 2020by Gorav Singal#drupal#drupal-8#drupal-8-image-styles
// 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.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

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…

Theming the Drupal 8 Comment Module From Ugly to Beautiful

Theming the Drupal 8 Comment Module From Ugly to Beautiful

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

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…