Blog/ How To Create And Use Drupal 7 Image Styles?

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

 4. 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);

Comments

By Alex Vallejo (not verified) Wednesday, September 17, 2014 - 19:39 Permalink

The args in your example are backwards, they should be $style, $path

By Jay (not verified) Sunday, January 4, 2015 - 01:57 Permalink

Newbie question, but even though you have selected Image Style to be Large, will the system still create thumbnails upon upload? If so, how do I access the thumbnails?

 
Ready to get started?REQUEST A QUOTE