Giter VIP home page Giter VIP logo

statamic-responsive-img's Introduction

ResponsiveImg

Never think about creating responsive images again.

This addon makes it super simple to output an img with multiple srcset sizes defined to ensure browsers only ever download the size it needs based on the viewport. Additionally, a base64-encoded SVG will be inlined to show a tiny, blurred image while the real image is loading -- this will ensure the image renders at the correct size on initial page load without an extra network request.

For more info on how this works under the hood, read Spatie's Responsive Image docs.

Getting started

To get started and for a list of available options, check out the docs.

Example

Below is a simple example of a banner image with a width of 2500px:

---
banner: /assets/img/hero-banner.jpg
---
<div>
  {{ responsive_img:banner attr="class:w-full" }}
</div>

Rendered HTML:

<div>
  <img class="w-full" srcset="/img/containers/main/img/banner.jpg/3a679c0fc2a494827cd78b9facbaf696.jpg 2500w, /img/containers/main/img/banner.jpg/86eb8f18c80bc4adb298f7d4aba23d98.jpg 2091w, /img/containers/main/img/banner.jpg/c43a27815f3003963dfe551aa80c088e.jpg 1749w, /img/containers/main/img/banner.jpg/ac56820bdeeb5e996abc606b40945d83.jpg 1464w, /img/containers/main/img/banner.jpg/f5c1a4ee11018fcafe1f07f41d2da07a.jpg 1224w, /img/containers/main/img/banner.jpg/cd8d32990fb26bde830403f19d0a0662.jpg 1024w, /img/containers/main/img/banner.jpg/6561461f81d6540cd5a635bf5d44dde6.jpg 857w, /img/containers/main/img/banner.jpg/019b9c8a02a7dc1291f9e60b29a2fc15.jpg 717w, /img/containers/main/img/banner.jpg/3613eed0a83dcb41766e8afdc425a59b.jpg 600w, /img/containers/main/img/banner.jpg/5495e24e576ba9b9cc2b49a4b4a25957.jpg 502w, /img/containers/main/img/banner.jpg/ffb73621c90e0fe4e7c7d4901dc151cb.jpg 420w, /img/containers/main/img/banner.jpg/6960fb223088098ca0c9b02fc114a087.jpg 351w, /img/containers/main/img/banner.jpg/baaebfbfd6755aa87317a224f9be302a.jpg 294w, /img/containers/main/img/banner.jpg/36bb43d811cccde3d1d5cd63909c48b1.jpg 246w, /img/containers/main/img/banner.jpg/5d6d9b6e1caa09376b1a5b6198a57df4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NoWVhHQmthSlNZbktDa3FORFUyTnpnNU9rTkVSVVpIU0VsS1UxUlZWbGRZV1ZwalpHVm1aMmhwYW5OMGRYWjNlSGw2ZzRTRmhvZUlpWXFTazVTVmxwZVltWnFpbzZTbHBxZW9xYXF5czdTMXRyZTR1YnJDdzhURnhzZkl5Y3JTMDlUVjF0ZlkyZHJoNHVQazVlYm42T25xOGZMejlQWDI5L2o1K3YvRUFCOEJBQU1CQVFFQkFRRUJBUUVBQUFBQUFBQUJBZ01FQlFZSENBa0tDLy9FQUxVUkFBSUJBZ1FFQXdRSEJRUUVBQUVDZHdBQkFnTVJCQVVoTVFZU1FWRUhZWEVUSWpLQkNCUkNrYUd4d1Frak0xTHdGV0p5MFFvV0pEVGhKZkVYR0JrYUppY29LU28xTmpjNE9UcERSRVZHUjBoSlNsTlVWVlpYV0ZsYVkyUmxabWRvYVdwemRIVjJkM2g1ZW9LRGhJV0doNGlKaXBLVGxKV1dsNWlabXFLanBLV21wNmlwcXJLenRMVzJ0N2k1dXNMRHhNWEd4OGpKeXRMVDFOWFcxOWpaMnVMajVPWG01K2pwNnZMejlQWDI5L2o1K3YvYUFBd0RBUUFDRVFNUkFEOEFvd2FPOG82VmNqMEFoaG5wVGRKMTJOeUZiQXJjT3B3SGdNSytFeEN4Vk9mSTRuMUV1SVhVMWl4YkN4VzJYQXJUMmdyaXNoYjc1dU9sWElyc01Pb3J6YXRPbzNkbkhQR0tzN3Rua2R0SzZuaHNWbzI5ek1aQjg1b29yOW14TkdtNVhjVnNmQ2N6WFU2T3lsZGdNbW0zbHpMRko4akVVVVY4Zk9sRDJ6VmtkMUtVcmJuLzJRPT0iPjwvaW1hZ2U+Cjwvc3ZnPgo= 32w" onload="this.onload=null;this.sizes=Math.ceil(this.getBoundingClientRect().width/window.innerWidth*100)+'vw';" sizes="1px">
</div>

Credits

A big thank you to Spatie for the idea and the recursive size calculation in their Laravel Medialibrary package.

statamic-responsive-img's People

Contributors

aryehraber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

wrklst

statamic-responsive-img's Issues

FR: Add lazy-loading option (using data-attributes)

In some cases it is crucial to be able to iterate over an array of assets. For instance if you want to retrieve a title or alt tag. As well I mostly need more control over the img tag. For instance to add custom data-attributes for lazy-loading. Is this possible?

My current implementation for responsive images looks something like this.

---
slider:
  - /assets/images/image-1.jpg
  - /assets/images/image-2.jpg
---

{{ assets:slider }}
    <img data-src="{{ glide:url preset="1000" }}" class="swiper-lazy"
        data-srcset="
            {{ glide:url preset="1400" }} 1400w,
            {{ glide:url preset="1200" }} 1200w,
            {{ glide:url preset="1000" }} 1000w,
            {{ glide:url preset="800" }} 800w,
            {{ glide:url preset="600" }} 600w,
            {{ glide:url preset="400" }} 400w"
        sizes="
            (min-width: 1921px) 30vw,
            (min-width: 1800px) 40vw,
            (min-width: 1200px) 50vw,
            (min-width: 940px) 60vw,
            (min-width: 640px) 85vw,
            100vw"
        title="{{ title }}"
        alt="{{ alt }}">
{{ /assets:slider }}

As well I wonder if you could add a settings file for image quality, min and max sizes and the like.

Parentheses not allowed

If you select an image with parentheses, it throws the following error:

Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: file_get_contents(/home/[webserver]/public_html/img/containers/main/./abstract-ai-art-373543-%281%29.jpg/cc2541df24434bbae9bae97dd2c96867.jpg): failed to open stream: No such file or directory (View: /home/[webserver]/public_html/site/addons/ResponsiveImg/resources/views/img.blade.php)

Improve docs

  • Example of using inside arrays
  • Quality option
  • Data attribute option (once released )

error filename empty... mhhh

I set up everything according to docs, also serve cached assets directly.
But receiving an error:


FatalErrorException in Parser.php line 789:
Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: getimagesize(): Filename cannot be empty (View: /home/vagrant/code/XXXXXXXXXX/site/addons/ResponsiveImg/resources/views/img.blade.php)

The cached image exists and the original file path is screenshots/unnamed.jpg

No support for PNG images?

Great addon! Though it doesn't seem to work for PNG images (since it gets transformed to JPGs in the output), is that a bug or just not supported (yet)?

We use transparent PNGs massively on our site, so the addon will only be usable when this is supported.

Thanks in advance!

FR: Use with <picture> tag

Is it possible to somehow use this plugin with the picture tag for art direction? My current implementation looks like this:

<picture>
   <source media="(min-width: 940px)"
      srcset="
         {{ glide:url preset="2500x1000" }} 2500w,
         {{ glide:url preset="2000x800" }} 2000w,
         {{ glide:url preset="1800x720" }} 1800w,
         {{ glide:url preset="1600x640" }} 1600w,
         {{ glide:url preset="1400x560" }} 1400w,
         {{ glide:url preset="1200x480" }} 1200w,
         {{ glide:url preset="1000x400" }} 1000w"
      sizes="100vw">
  <source media="(min-width: 0px)"
      srcset="
         {{ glide:url preset="1600x1600" }} 1600w,
         {{ glide:url preset="1000x1000" }} 1000w,
         {{ glide:url preset="800x800" }} 800w,
         {{ glide:url preset="600x600" }} 600w,
         {{ glide:url preset="400x400" }} 400w"
      sizes="100vw">
  <img src="{{ glide:url preset="1800x1000" }}" title="{{ title }}" alt="{{ alt }}">
</picture>

This defines two different art directions of the same image. The mobile source starting at 0px has a different aspect ratio than the source starting at 940px. The images provided in srcset are cropped to the appropriate aspect ratio and size using Glide. This is to eliminate unnecessary picture excess to optimize file size.

There's two issues I see here. Number one being the ability to crop the images using Glide. This is related to issue #7 and pull request #11.

Issue number two is to make it possible to use the picture tag instead of the img tag. Maybe this can be done in a simple way with a parameter which disables the output of the full img tag and only outputs the srcset and sizes. And for the fallback img outside of source, you can manually define a fallback.

Something like this:

<picture>
   <source media="(min-width: 940px)"
       {{ responsive_img:image img-tag="false" glide="{'w':2500,'h':1000,'fit':'crop_focal'}" }}>
  <source media="(min-width: 0px)"
       {{ responsive_img:image img-tag="false" glide="{'w':1600,'h':1600,'fit':'crop_focal'}" }}>
  <img src="{{ glide:url preset="1800x1000" }}" title="{{ title }}" alt="{{ alt }}">
</picture>

How does this sound? Is this something that's doable?

View::__toString() must not throw an exception

I'm trying to display an image from a bard field with the following code:

{{ responsive_img:image }}

Serve Cached Images directly is enabled and when opening the page it does generate the images in the right folder. I still get the following error.

Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: file_get_contents(/work/blog/https://myblog.test/img/containers/main/./office-view.jpg/0798d4bb7bb33faeb1795ccd290e88f4.jpg): failed to open stream: No such file or directory (View: /work/blog/site/addons/ResponsiveImg/resources/views/img.blade.php)

As highlighted, somehow the domain name https://myblog.test get's added in between the path which i think could be the error. I don't know why this happens. Am I missing something? I'm running the page locally on Laravel Valet

Images dont load, dont even creates the HTML code

Hi.
Im trying to use ResponsiveIMG but im having a issue, i cant get to load the assets that are choose when we edit the page on the backoffice.
This is my responsive code
{{ responsive_img:image_bundleitem }}

I can load the image normally with the img tag
and if i place for example this on top of the template file

---
banner: /assets/services/destaquefatkiller.jpg
---

{{ responsive_img:banner }}

and if i use then this on the code it works

Can someone help me please

Sizes attribute

Maybe I'm missing something, but I've got a project that has a container size of 1152px.

I've then split that into 3 columns, each 394px wide. I'm seeing the Responsive Image plugin set a sizes attribute of 53vw. Maybe my understanding of viewport width is off, but isn't that too much?

I'm loading an image that is 1333px wide, for a column that is less than 400px wide.

Thanks for the hard work, great addon!

Suggestion: Remove the default 'fm' => 'jpg' parameter

I just updated the plugin to the latest version and didn't pay attention, that my colleagues manually adjusted this line

$default = ['fm' => 'jpg', 'q' => $this->quality];

like this

- $default = ['fm' => 'jpg', 'q' => $this->quality];
+ $default = ['q' => $this->quality];

this first resulted in a very slow page, as dozens of images were regenerated and afterwards I had a lot of images with white backgrounds instead of transparency.

I think it would make sense to either remove that default setting or make it configurable in a config file which is not overwritten on update.

FR: Global settings

Add global settings for certain options:

  • data-attr
  • max-width (once released)
  • scaling-rate (once released)

Doesn't seem to work with S3-based assets

I have a site configured to use an S3 bucket to manage assets. Serving cached images directly works just fine with a standard glide tag, but for some reason, the responsive_img tag doesn't generate anything. No markup whatsoever. Is this a known issue with S3-based assets?

FR: Add Glide Parameters

It would be amazing to have access to the glide parameters. The most common use in my case would be to set the width and height of a picture and use the Statamic focal point feature.

As well it would be amazing to have presets (similar to the glide presets) where you could set everything from width, crop, quality, scaling-rate etc.

FR: Generate presets

This addon requires image_manipulation_cached: true. Because the cache gets generated the first time an image is viewed, it is possible to run into a server timeout. Especially if you have a lot of new images on a page. There should be a way to generate the assets cache in advance. With Glide you can just call php please assets:generate-presets.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.