From 4a08c67f678dc7e7e3821cc8b39d9a1f066d4706 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 3 Jul 2022 18:20:41 +0900 Subject: [PATCH] Fix #1948 use naturalWidth/naturalHeight of original image tag, if possible, when loading PhotoSwipe --- addons/photoswipe/rx_photoswipe.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/addons/photoswipe/rx_photoswipe.js b/addons/photoswipe/rx_photoswipe.js index 14ea40563..1af7bab7b 100644 --- a/addons/photoswipe/rx_photoswipe.js +++ b/addons/photoswipe/rx_photoswipe.js @@ -1,12 +1,14 @@ /* Modified version of a http://photoswipe.com/documentation/getting-started.html example. Modified by misol for rhymix */ -var getPSImageSize = function(src) { - var testImg = new Image(); - testImg.src = src; - +var getPSImageSize = function(el) { var size = new Array(); - size[0] = testImg.width; - size[1] = testImg.height; - + size[0] = el.naturalWidth ? el.naturalWidth : (el.width ? el.width : 0); + size[1] = el.naturalHeight ? el.naturalHeight : (el.height ? el.height : 0); + if (!size[0] || !size[1]) { + var test = new Image(); + test.src = el.src; + size[0] = test.naturalWidth ? test.naturalWidth : (test.width ? test.width : 0); + size[1] = test.naturalHeight ? test.naturalHeight : (test.height ? test.height : 0); + } return size; } @@ -38,11 +40,11 @@ var initPhotoSwipeFromDOM = function(gallerySelector) { imgEl = imgElements.get(i); // element // include only element nodes - if(imgEl.nodeType !== 1 || !$(imgEl).attr('data-pswp-pid')) { + if (imgEl.nodeType !== 1 || !imgEl.src || !$(imgEl).attr('data-pswp-pid')) { continue; } - size = getPSImageSize($(imgEl).attr('src')); + size = getPSImageSize(imgEl); // create slide object item = { @@ -256,4 +258,4 @@ var initPhotoSwipeFromDOM = function(gallerySelector) { // execute above function -initPhotoSwipeFromDOM('.rhymix_content, .xe_content'); \ No newline at end of file +initPhotoSwipeFromDOM('.rhymix_content, .xe_content');