I have the feeling I'm the only one with such complicated code for my images.
With so many devices with different view ports and landscape / portrait...
How do you all handle your picture tag art-direction responsive stuff?
Now i want some images to be rotated if the device is in portrait mode.
Imager has no transform for rotate. So i need to upload the rotated image and set it as alternative to the other image.
Edit:
The code i have now after changing it:
{% if entry.textImages | length %}
{% for row in entry.textImages.all() %}
{% set image = row.textImage.one() %}
{% set imageWidth = row.textImageWidth %}
{% set imageHeight = row.textImageHeight %}
{% set imageClass = row.textImageClass %}
{% set imagePortrait = row.textImagePortrait.one() %}
{% set imageType = row.textImageType.value %}
{% set fileName = image.filename|trim('.' ~ image.extension, 'right') %}
{% set tempImage = craft.imager.transformImage(image, { width: 2000 },
{
allowUpscale: false,
resizeFilter: 'lanczos',
effects: { unsharpMask: [0, 0.55, 0.55, 0.008] },
interlace: false,
convertToRGB: true
}
) %}
{% if imageHeight is not empty %}
{% set tempHeight = tempImage.getHeight() %}
{% set imageSizes = [{ height: tempHeight },
{ height: imageHeight * 4.5 },
{ height: imageHeight * 4.25 },
{ height: imageHeight * 4 },
{ height: imageHeight * 3.75 },
{ height: imageHeight * 3.5 },
{ height: imageHeight * 3.25 },
{ height: imageHeight * 3 },
{ height: imageHeight * 2.75 },
{ height: imageHeight * 2.5 },
{ height: imageHeight * 2.25 },
{ height: imageHeight * 2 },
{ height: imageHeight * 1.75 },
{ height: imageHeight * 1.5 },
{ height: imageHeight * 1.25 },
{ height: imageHeight }] %}
{% else %}
{% set tempWidth = tempImage.getWidth() %}
{% set imageSizes = [{ width: tempWidth },
{ width: imageWidth * 4.5 },
{ width: imageWidth * 4.25 },
{ width: imageWidth * 4 },
{ width: imageWidth * 3.75 },
{ width: imageWidth * 3.5 },
{ width: imageWidth * 3.25 },
{ width: imageWidth * 3 },
{ width: imageWidth * 2.75 },
{ width: imageWidth * 2.5 },
{ width: imageWidth * 2.25 },
{ width: imageWidth * 2 },
{ width: imageWidth * 1.75 },
{ width: imageWidth * 1.5 },
{ width: imageWidth * 1.25 },
{ width: imageWidth }] %}
{% endif %}
{% set transformedImages = craft.imager.transformImage(tempImage, imageSizes,
{
format: imageType,
allowUpscale: false,
resizeFilter: 'lanczos',
effects: { unsharpMask: [0, 0.55, 0.55, 0.008] },
interlace: false,
convertToRGB: true,
filenamePattern: fileName ~ '_{fullname|hash}.{extension}'
}
) %}
{% set transformedImagesWidth = transformedImages[15].getWidth() %}
{% set imagePortraitSizes = [{ width: transformedImagesWidth * 4.5 },
{ width: transformedImagesWidth * 4.25 },
{ width: transformedImagesWidth * 4 },
{ width: transformedImagesWidth * 3.75 },
{ width: transformedImagesWidth * 3.5 },
{ width: transformedImagesWidth * 3.25 },
{ width: transformedImagesWidth * 3 },
{ width: transformedImagesWidth * 2.75 },
{ width: transformedImagesWidth * 2.5 },
{ width: transformedImagesWidth * 2.25 },
{ width: transformedImagesWidth * 2 },
{ width: transformedImagesWidth * 1.75 },
{ width: transformedImagesWidth * 1.5 },
{ width: transformedImagesWidth * 1.25 },
{ width: transformedImagesWidth }] %}
{% set transformedPortraitImages = craft.imager.transformImage(imagePortrait, imagePortraitSizes,
{
format: imageType,
allowUpscale: false,
resizeFilter: 'lanczos',
effects: { unsharpMask: [0, 0.55, 0.55, 0.008] },
interlace: false,
convertToRGB: true
}
) %}
<picture>
{% if imagePortrait is not empty %}
<source media="(orientation: portrait)"
sizes="80vw"
srcset="{{ craft.imager.srcset(transformedPortraitImages) }}">
{% endif %}
<img class="anchor {% if imageClass is not empty %}{{ imageClass }}{% endif %}"
src="{{ transformedImages[15].getUrl() }}"
sizes="{% if row.textImageVW is not empty %}
(orientation: portrait) {{ row.textImageVW }}vw,
(orientation: landscape) and (max-width: 799px) {{ row.textImageVW }}vw,
{% else %}
(orientation: portrait) 80vw,
(orientation: landscape) and (max-width: 799px) 60vw,
{% endif %}
{% if imageHeight is not empty %}{{ transformedImagesWidth }}{% else %}{{ imageWidth }}{% endif %}px"
srcset="{{ craft.imager.srcset(transformedImages) }}"
title="{{ image.title }}"
alt="{{ image.altText }}">
</picture>
{% endfor %}
{% endif %}
To calculate the width for the portrait view image i use the width of the transformed landscape image (transformedImagesWidth). The reason is that the images should align.
You can see it here:
Phantasialand Hörspiel & Videokassetten
On a PC you see the case and two tapes in one row.
On a phone they are one below the other and the tapes rotate.
Only thing i don't like right now is that if i rotate the phone to landscape that i get the landscape image. But below 800px it should still show the portrait image.
.rotate-90
as required.<img
, no reflow, no .js required.