PhotoSwipe: Serving images of unknown size - suggestion/solution
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();
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 166
- Comments: 31 (4 by maintainers)
@sk29110, http://photoswipe.com/documentation/faq.html#image-size
@mjau-mjau The reason for the multiple
gettingDataevents (and the flickering it causes) is the call toinvalidateCurrItems()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 callingupdateSize(true)is enough:Just by removing that function call, the flickering and redundant background calls to
gettingDataare gone.I tried the
item.onloading = trueapproach 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.
You can use this to fix it… Give any value you want for h and w and add
above css
Just a quick addition to @gincius code, which might solve the issue for @sk29110 also. I noticed that
gettingDatatriggers continuously, likely while images are being loaded? This is what I see in console for a 3-slide object:This also means that the
img.onloadwill be triggered multiple times for the same images, causing unnecessaryinvalidateCurrItems()andupdateSize(true)being triggered, which could cause havoc. The simple solution is to add a flagitem.onloading:Although the
gettingDatalistener still triggers, theimg.onloadwill only trigger once per image:This is my solution:
Create a function that sets the
pswp-widthandpswp-heightattributes to the anchors before initializing the PhotoSwipe gallery. We need to wait until all the images have been loaded, and we’re using promises for this.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
itemhas aelproperty denoting its relative HTMLElementHello! I’m trying to implement this method on this codepen example without success :
https://codepen.io/alienlebarge/pen/Kdrxga
Can someone implement it?
@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.
@mjau-mjau use the event ‘imageLoadComplete’ instead of ‘gettingData’ work for me.