create_image_object
The create_image_object function allows you to create an image object from either an image URL/path or an array of image attributes.
You can use this to convert a hard-coded image URL in your template into an image object so that you can more easily modify the image tag HTML attributes or apply image pipeline changes.
This function is unique to Aptuitiv. It is not part of the core Twig functionality.
You can create the image object from an image path/URL.
{% set newImageObj = create_image_object('images/max-lissenden-tJxu4j4-T4o-unsplash.jpg') %}
Or, you can pass a hash of image attributes. You should at minimum pass the src value.
{% set newImageObj = create_image_object({src: 'images/max-lissenden-tJxu4j4-T4o-unsplash.jpg', alt: 'Some alt text', width: '3200', height: 1400}) %}
Arguments
The create_image_object function has the following signature.
create_image_object(pathOrAttributes)
Argument | Description |
---|---|
pathOrAttributes |
This will either be a string containing the path/URL for the image, or a hash of image tag attributes.
If you need other attributes then you can set them after the image object is created. |