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
- Navigate to "Configuration" -> "Media" -> "Image Styles". Here you can see a list of default image styles.
- Click on "Add Style" to create an image style or click on "Edit" to edit the existing style.
- 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:
- Navigate to "Structure" -> "Content types" -> "<content_type>" -> Manage Display.
- Click on the settings icon.
- Select the image style from the dropdown. You can also link an image to the content or file.
- Save the settings.
Views Field Display Setting:
- Navigate to "Structure" -> "Views"
- Edit your view.
- Click on your image field and select an image style as shown below:
- 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);
The args in your example are backwards, they should be $style, $path
That's absolutely correct! Thank you for pointing this out. We have fixed the article. :)
Newbie question, but even
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?
Yes, system will still create thumbnails on upload. If you want the URL of the thumbnail, you can right click on the thumbnail and copy the image URL.
Images are not created in the filesystem
Will custom image styles create new files like "Thumbnail" style does? If so, how can I do this.
Create new files
Yes, custom image styles will create new files. Can you explain the question "how can I do so"? I didn't understand it.