question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamic image dimensions

See original GitHub issue

as you said in the docs its not possible, i even tried and went half way through…just one tiny piece missing. here my attempt:

        var images = [
              { src: "http://foo.com/bar.gif", w:0, h:0}
        ];
        var pswpElement = document.querySelectorAll('.pswp')[0];

        var options = {
            index: 0,
            showHideOpacity: true
        };

        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, images, options);

        gallery.listen('imageLoadComplete', function(index, item) {
            var img = new Image();

            item.w = img.width;
            item.h = img.height;
        });

        gallery.init();

sadly it only works good for the lazyloaded images “left” and “right” but not for the current one requested. in my use case i cant encode the size into the filename as the images are thumbnailed to fit in a box (e.g. image 760+420 should fit in a box of 400x250) and i dont know the resulting dimension, i even dont care 😉. i dont want to make backend requests to get the size, thats not performant. as the example above is half way working i think its possible by PhotoSwipe itself (and it should be a feature anyway 😉 )

i think the easiest fix would be to start all displaying stuff after the event is done?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:5
  • Comments:53 (3 by maintainers)

github_iconTop GitHub Comments

34reactions
dimsemenovcommented, Sep 24, 2015

Related section in FAQ.

In documentation there is an example on how to add or edit slides dynamically, see http://photoswipe.com/documentation/api.html (at the bottom). You may preload image, retrieve its size, and append (or replace) the dummy image.

Or if you have just 1 or 2 images, you can preload it, and only then initialize PhotoSwipe.

For now there are no plans to add such feature in core, as PhotoSwipe displays and animates image before it’s loaded.

29reactions
carl-jincommented, Feb 14, 2019

i tried like this , its working for me

    // .......
      let items= [
              { src: ".....", w:0, h:0}
        ];
        let gallery = new PhotoSwipe(this.$pswpElement, PhotoSwipeUI_Default, items, options)
        gallery.listen('imageLoadComplete', function (index, item) {
          if (item.h < 1 || item.w < 1) {
            let img = new Image()
            img.onload = () => {
              item.w = img.width
              item.h = img.height
              gallery.invalidateCurrItems()
              gallery.updateSize(true)
            }
            img.src = item.src
          }
        })
        gallery.init()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I resize an image dynamically with CSS as the ...
To make the images flexible, simply add max-width:100% and height:auto . Image max-width:100% and height:auto works in IE7, but not in IE8 (yes,...
Read more >
Dynamic image resizing - ImageKit.io
Learn how to dynamically resize & adapt images to fit the page layout by changing URL parameters. No image processing server is required....
Read more >
Dynamically resize images - Akamai TechDocs
In addition to resizing images using hard coded dimensions, you can use Image and Video Manager to dynamically resize images based on published...
Read more >
Dynamic Dummy Image Generator - DummyImage.com
Dynamic Dummy Image Generator ... Height is optional, if no height is specified the image will be a square. ... Standard image sizes...
Read more >
Dynamic Image Sizing (to scale) - Canvas Community
This is ideal for embedding images into your Canvas unit that can be viewed on a mobile with no issues.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found