1

I am using imageoptimize plugin to crop images, but I am unable to find cropping position setup in a template.

{% set optimzedImages = craft.imageOptimize.createOptimizedImages(
       entry.backgroundImage.one(),
          [
                    {
                        'width': 1440,
                        'useAspectRatio': true,
                        'aspectRatioX': 1.0,
                        'aspectRatioY': 0.35,
                        'retinaSizes': ['1'],
                        'quality': 82,
                        'format': entry.backgroundImage.one().extension,
                        'position' : 'top-center'
                    }
                ]
            ) %}

The above code is cropping image in center-center and I want to crop an image from top to aspect ratio height.

2

FWIW, I would not recommend doing image resizing via your Twig templates, because it will do them on page load time. Rather, I’d recommend doing them via an Optimized Images field as per: https://github.com/nystudio107/craft-imageoptimize/blob/v1/README.md#dynamically-creating-optimized-image-variants

But if you really want to do it, the parameter there is the same as you’d use for regular Craft transforms: https://docs.craftcms.com/v3/image-transforms.html#defining-transforms-in-your-templates

So you could do 'top-center' or such

But especially since you’re just creating one image variant, there’s no real advantage to doing this via ImageOptimize in your templates. Just use a regular Craft transform. With ImageOptimize installed, the resulting image will be optimized when done via Craft transforms, too.

The real benefit of using ImageOptimize to create image transforms is to make creating responsive image srcsets easier.

  • thanks for your answer, I did not know about all the features of the plugin, Optimized Images field is a cool feature in the plugin. – Girish Nov 30 '18 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.