New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serving images of unknown size - suggestion/solution #796
Comments
Nice, thx for sharing. |
+1 |
Could it be that opening "zoom-in" animations don't work with this solution? |
@renet, as the animation runs instantly. It's not possible to execute it when final coordinates are unknown (0,0). |
Is it possible then to wait for 'zoom-in' until image is loaded and size is known? |
@AdamPietrasiak, sure, preload image, find its size and only then initialize PhotoSwipe. But you'll need to handle progress indication by yourself. |
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? |
Thanks. |
Nice fix, thnx. A good fallback solution when exposing photoswipe for usage in custom html content. |
Just a quick addition to @gincius code, which might solve the issue for @sk29110 also. I noticed that This also means that the // 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 |
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. |
Other solution: if you get items by querying from database, after getting image size you can store/update |
@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? |
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 |
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 |
It works, Thanks! |
@mjau-mjau use the event 'imageLoadComplete' instead of 'gettingData' work for me. |
@mjau-mjau The reason for the multiple 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 I tried the 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 |
@gincius @mjau-mjau thx u guys, great works! |
All solutions provided here didn't work for me. Causes the browser to crash (endless loop I assume). |
@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: If you post an example of your code on Codepen, I would be happy to take a look. |
@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. |
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 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
}
}) |
thank you very very much |
Hello! I'm trying to implement this method on this codepen example without success : https://codepen.io/alienlebarge/pen/Kdrxga Can someone implement it? |
You can use this to fix it.. Give any value you want for h and w and add
above css |
My solution, maybe helps someone: link |
This is my solution: Create a function that sets the
|
How to serve images with unknown size - my solution:
The text was updated successfully, but these errors were encountered: