Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

How To Create And Use Drupal 7 Image Styles?

Neerav Mehta

Founder & CEO

Image styles are used to set presets for image processing. Using image styles, we can crop, desaturate, resize, rotate and scale images. We can also add various effects before an image is displayed.

When an image is displayed with a style, a new image file is created and the original image is left unchanged. By default, Drupal has three image styles: thumbnail, medium and large.

Creating image styles using Drupal UI

  1. Navigate to "Configuration" -> "Media" -> "Image Styles". Here you can see a list of default image styles.
  2. Click on "Add Style" to create an image style or click on "Edit" to edit the existing style.
  3. Add an effect from the available drop down and click on Add button. You can add the effect as per your requirement. The available effects are listed below:
  • Crop: Cropping will remove portions of an image to make it the specified dimensions.

  • Desaturate: Desaturation will convert the image into a grayscale image.

  • Resize: Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.

  • Rotate: Rotating an image may cause the dimensions of an image to increase to fit the diagonal.

  • Scale: Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.

  • Scale and Crop: Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the ima

  1. You can add multiple effects on a single image style. Multiple Effects will be applied as per the row weight. If you are looking for more effects than the ones provided by Drupal core, then install ImageCache Actions module.

Once you are done with creating an Image style, you need to assign it to an image.

Assigning image styles to image fields

Image styles can be assigned in "Content Types Display" or "Views Field Display".

Content Types Display Settings:

  1. Navigate to "Structure" -> "Content types" -> "<content_type>" -> Manage Display.
  2. Click on the settings icon.
  3. Select the image style from the dropdown. You can also link an image to the content or file.
  4. Save the settings.
Views Field Display Setting:

  1. Navigate to "Structure" -> "Views"
  2. Edit your view.
  3. Click on your image field and select an image style as shown below:
  4. Apply the settings and then save your view.

Adding Image Style manually in your theme template file

Sometimes, we may need to display the styled image manually in our template file. We can use image_style_url($style_name, $path) function in such cases. This function returns the absolute URL where a style image can be downloaded, suitable for use in an <img> tag. Requesting the URL will cause the image to be created. Here is an example:

// Original image uri.
$path = 'private://myimagefolder/imgfilename.jpg';
// Machine-readable name of the style.
$style = 'thumbnail';
// URL of the image style.
// Use this within your template file by assigning it to $vars['style_url'].
$style_url = image_style_url($style, $path);

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.