An image object returns information about an image. Image objects are usually attributes of other objects, such as product, variant, collection and article.
To render an image, use the image_tag filter.
The image object has the following attributes:
image.alt
Returns the alt text of the image, set on the products page in the admin.
image.aspect_ratio
Returns the aspect ratio (width / height) of the image.
image.attachedtovariant?
Returns true if the image is attached to a variant, otherwise false. Use it, for example, to build a gallery of images that are not attached to variants.
attached_to_variant? is only returned if the image belongs to a product. It is not available when accessed through product.media.
image.height
Returns the height of the image in pixels.
image.id
Returns the ID of the image.
No ID is returned for preview images of generic_file and media objects.
image.media_type
Returns the type of the object (image).
Use media_type to filter the product.media array for all media of a given type.
{% assign images = product.media | where: "media_type", "image" %}
{% for image in images %}
{{ image.alt }}
{% endfor %}
Image of the black stroller
Image of the grey stroller
media_type is only returned if the image belongs to a product and is accessed through product.media, or if it is accessed through a file_reference metafield.
image.position
Returns the position of the image in the media array of the product object.
Only returned if the image belongs to a product.
image.preview_image
Returns a preview image for the image.
preview_image is only returned when accessed through a media attribute of the product object, for example product.featured_media.preview_image.
image.product_id
Returns the ID of the image's product.
product_id is only returned if the image belongs to a product.
image.src
Returns the relative path of the product image. This is the same as the output of {{ image }}.
{% for image in product.images %}
{{ image.src }}
{{ image }}
{% endfor %}
products/my_image.jpg
products/my_image.jpg
To return the URL of the image on the VersaCommerce content delivery network (CDN), use the matching URL filter.
See the image size parameters for all available image sizes.
This example loads a product image with the image_url filter.
{{ image | image_url: width: 500 }}
//cdn.versacommerce.de/s/files/1/0087/0462/products/shirt14.jpeg?v=1309278311&width=500
image.variants
Returns an array of the variants the image is attached to.
{% for image in product.images %}
{% for variant in image.variants %}
{{ image.src }} - used for the variant: {{ variant.title }}
{% endfor %}
{% endfor %}
products/red-shirt.jpeg - used for the variant: Red
products/green-shirt.jpg - used for the variant: Green
products/yellow-shirt.jpg - used for the variant: Yellow
variants is only returned if the image belongs to a product. It is not available when accessed through product.media.
image.width
Returns the width of the image in pixels.
More properties
VersaCommerce also provides these properties on the image object:
| Properties | Returns |
|---|---|
presentation, url, description, content_type, filename, exists?, size, updated_at |
Focal point, image metadata and URL aliases |