aelvan / Imager-Craft

This plugin has been DEPRECATED. Check out Imager X instead.
MIT License
342 stars 69 forks source link

Srcset - include original size? #258

Closed sam327 closed 5 years ago

sam327 commented 5 years ago

I've got image building out srcsets, and it's working out well. But I've got a question / request.

So, my code's pretty standard... looks something like this:

{% set img = entry.mainImage.one %}
{% set firstImage = craft.imager.transformImage(img, { width: 480 }, { allowUpscale: false, position: img.getFocalPoint() } ) %}
{% set transformedImages = craft.imager.transformImage(img, [
      { width: 1600 },
      { width: 1200 },
      { width: 720 },
      { width: 480 },
    ] 
  ) 
%}
<img src="{{ firstImage }}" alt="{{ entry.title }}"  sizes="100vw" srcset="{{ craft.imager.srcset(transformedImages) }}">

But I'd like to be able to put in another variable to the srcset, like { width: original }, that would add the original size at the top of the list with its width, so that if the screen gets really wide, it has the ability to pull the original size.

So, if my image is 2237px wide, the set would have _/imager/images/image.jpg 2237w, /imager/images/image_1600.jpg 1600w, /imager/images/image1200.jpg 1200w, ... and so on

That all make sense?

aelvan commented 5 years ago

Not sure if that's a good idea, but you can easily achieve this by getting the width from the asset like:

{% set transformedImages = craft.imager.transformImage(img, [
      { width: img.getWidth() },
      { width: 1600 },
      { width: 1200 },
      { width: 720 },
      { width: 480 },
    ] 
  ) 
%}

Don't see any reason to add a special case for this inside Imager.

sam327 commented 5 years ago

I'm not 100% sure either. But I'm willing to test it out, if only to show before and after comparisons to the boss.

At any rate, that one line was exactly I was looking for. Thank you.