PhotoSwipe icon indicating copy to clipboard operation
PhotoSwipe copied to clipboard

Serving images of unknown size - suggestion/solution

Open gincius opened this issue 11 years ago • 31 comments

How to serve images with unknown size - my solution:

var items = [ 
  { src: 'http://........', w:0, h:0 },
  { src: 'http://........', w:0, h:0 }
];

var gallery = new PhotoSwipe( ..... );
gallery.listen('gettingData', function(index, item) {
        if (item.w < 1 || item.h < 1) { // unknown size
        var img = new Image(); 
        img.onload = function() { // will get size after load
        item.w = this.width; // set image width
        item.h = this.height; // set image height
           gallery.invalidateCurrItems(); // reinit Items
           gallery.updateSize(true); // reinit Items
        }
    img.src = item.src; // let's download image
    }
});

gallery.init();

gincius avatar Mar 26 '15 14:03 gincius

Nice, thx for sharing.

#741

dimsemenov avatar Mar 26 '15 15:03 dimsemenov

+1

willyguevara avatar Apr 10 '15 16:04 willyguevara

Could it be that opening "zoom-in" animations don't work with this solution?

renet avatar May 08 '15 15:05 renet

@renet, as the animation runs instantly. It's not possible to execute it when final coordinates are unknown (0,0).

dimsemenov avatar May 08 '15 15:05 dimsemenov

Is it possible then to wait for 'zoom-in' until image is loaded and size is known?

pie6k avatar May 17 '15 10:05 pie6k

@AdamPietrasiak, sure, preload image, find its size and only then initialize PhotoSwipe. But you'll need to handle progress indication by yourself.

dimsemenov avatar May 17 '15 10:05 dimsemenov

Just question why you need the width and height? Usually all other lightbox plugins doesn't requre the width and height. Is this for zoom in and out feature?

sk29110 avatar Jul 23 '15 23:07 sk29110

@sk29110, http://photoswipe.com/documentation/faq.html#image-size

dimsemenov avatar Jul 24 '15 05:07 dimsemenov

Thanks. Just for interest and save time to save the width and height on database, I tried the @gincius 's solution by putting gallery.updateSize on image load. It seems going to the loop so the browser is frozen. His solution won't work for latest version(4.1.0)? I am trying with google chrome on mac.

sk29110 avatar Jul 24 '15 19:07 sk29110

Nice fix, thnx. A good fallback solution when exposing photoswipe for usage in custom html content.

mjau-mjau avatar Aug 02 '15 06:08 mjau-mjau

Just a quick addition to @gincius code, which might solve the issue for @sk29110 also. I noticed that gettingData triggers continuously, likely while images are being loaded? This is what I see in console for a 3-slide object:

screen1

This also means that the img.onload will be triggered multiple times for the same images, causing unnecessary invalidateCurrItems() and updateSize(true) being triggered, which could cause havoc. The simple solution is to add a flag item.onloading:

// Make sure the slide is not html, and that the onLoad was not already triggered for this item
if(item.html === undefined && item.onloading === undefined && (item.w < 1 || item.h < 1)) {
  item.onloading = true;

Although the gettingData listener still triggers, the img.onload will only trigger once per image:

screen2

mjau-mjau avatar Aug 03 '15 07:08 mjau-mjau

I seems that this only works with the history setting set to history:false, correct?

jamminjames avatar Aug 08 '15 00:08 jamminjames

I seems that this only works with the history setting set to history:false, correct?

I tested, and it works fine with history. No reason it shouldn't, as the function is entirely unrelated.

mjau-mjau avatar Aug 08 '15 06:08 mjau-mjau

Other solution: if you get items by querying from database, after getting image size you can store/update w and h field into database. Next client will get image size from database instead of getting data from browser

ghost avatar Sep 27 '15 12:09 ghost

@yudaprama good. But if you are querying the database, aren't you also using a server-side language that can simply read the image dimensions instead?

mjau-mjau avatar Sep 27 '15 13:09 mjau-mjau

I was originally adding the size data attributes in after they loaded then trying to insert them into the items array but I found this a much better solution and works for me. Thanks for sharing I was about to ditch this when I realized it had to have the image sizes, there's gotta be a way to work this in

ghost avatar Jan 08 '16 03:01 ghost

Thanks for this one. Didn't realize how much manual work was involved in init'ing PhotoSwipe and was getting worried for a sec.. this makes it usable for me! Would be nice if this kind of functionality were optionally baked in

nkpz avatar Aug 11 '16 22:08 nkpz

It works, Thanks!

mmillet avatar Oct 28 '16 09:10 mmillet

@mjau-mjau use the event 'imageLoadComplete' instead of 'gettingData' work for me.

JoDring avatar Nov 07 '16 09:11 JoDring

@mjau-mjau The reason for the multiple gettingData events (and the flickering it causes) is the call to invalidateCurrItems() inside @gincius code (great work btw! thanks a lot!), which is actually not necessary. That call invalidates all loaded items in the array (visible an preloaded); but we just need to refresh one item and, in fact, just calling updateSize(true) is enough:

pswpGallery.listen('gettingData', function (index, item) {
    if (item.w < 1 || item.h < 1) {
        var img = new Image();
        img.onload = function () {
            item.w = this.width;
            item.h = this.height;
            pswpGallery.updateSize(true);
        };
        img.src = item.src;
    }
});

Just by removing that function call, the flickering and redundant background calls to gettingData are gone.

I tried the item.onloading = true approach too, but I run into some kind of problem (can't remember right now) so I had to dig deeper and came up with the above solution.

I've extensively tested it with single- and multiple-image galleries in Firefox, Chrome, IE11 and Edge desktop browsers, and Firefox (Android), Safari (iPhone / iPad) and Chrome (both systems) mobile browsers. Images of unknown size are correctly resized and displayed in all of them. Hope it helps.

walenzack avatar Dec 30 '16 12:12 walenzack

@walenzack Unfortunately when I removed invalidateCurrItems() I stil get redraw of currently visible image, with flickering and zoom is reset.

tscislo-lingaro avatar Apr 18 '17 07:04 tscislo-lingaro

@gincius @mjau-mjau thx u guys, great works!

gnodiah avatar Jun 22 '17 11:06 gnodiah

All solutions provided here didn't work for me. Causes the browser to crash (endless loop I assume).

PoeHaH avatar Jun 29 '17 06:06 PoeHaH

@PoeHaH That would be something with your code unrelated to the code here. This code is being used for 100s of websites already, and you can see a demo here, which works in all browsers: https://demo.imagevuex.com/examples/plugins/popup/

If you post an example of your code on Codepen, I would be happy to take a look.

mjau-mjau avatar Jun 29 '17 16:06 mjau-mjau

@mjau-mjau Thanks for your quick reply! In the meantime, I fixed it by just preloading the images and setting width/height outside of the 'gettingData' event.

PoeHaH avatar Jun 29 '17 16:06 PoeHaH

A slightly modified version of the previous code to use the thumbnail image size (assuming a more or less equal image ratio) as a initial fallback:

Ps: my item has a el property denoting its relative HTMLElement

pswpGallery.listen('gettingData', (index, item) => {
  if (!item.w || !item.h) {
    const innerImgEl = item.el.getElementsByTagName('img')[0]
    if (innerImgEl) {
      item.w = innerImgEl.width
      item.h = innerImgEl.height
    }

    const img = new Image()
    img.onload = function () {
      item.w = this.width
      item.h = this.height
      pswpGallery.updateSize(true)
    }
    img.src = item.src
  }
})

kaisermann avatar Aug 30 '17 21:08 kaisermann

thank you very very much

YuJieun avatar Jul 31 '18 09:07 YuJieun

Hello! I'm trying to implement this method on this codepen example without success :

https://codepen.io/alienlebarge/pen/Kdrxga

Can someone implement it?

xariketi avatar Oct 29 '18 13:10 xariketi

You can use this to fix it.. Give any value you want for h and w and add

.pswp img {
    max-width: none;
    object-fit: contain;
}

above css

arammarm avatar Aug 18 '20 12:08 arammarm

My solution, maybe helps someone: link

darko-r avatar Nov 22 '20 14:11 darko-r