Fix #1948 use naturalWidth/naturalHeight of original image tag, if possible, when loading PhotoSwipe

This commit is contained in:
Kijin Sung 2022-07-03 18:20:41 +09:00
parent 8b611e9d08
commit 4a08c67f67

View file

@ -1,12 +1,14 @@
/* Modified version of a http://photoswipe.com/documentation/getting-started.html example. Modified by misol for rhymix */ /* Modified version of a http://photoswipe.com/documentation/getting-started.html example. Modified by misol for rhymix */
var getPSImageSize = function(src) { var getPSImageSize = function(el) {
var testImg = new Image();
testImg.src = src;
var size = new Array(); var size = new Array();
size[0] = testImg.width; size[0] = el.naturalWidth ? el.naturalWidth : (el.width ? el.width : 0);
size[1] = testImg.height; 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; return size;
} }
@ -38,11 +40,11 @@ var initPhotoSwipeFromDOM = function(gallerySelector) {
imgEl = imgElements.get(i); // <img> element imgEl = imgElements.get(i); // <img> element
// include only element nodes // 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; continue;
} }
size = getPSImageSize($(imgEl).attr('src')); size = getPSImageSize(imgEl);
// create slide object // create slide object
item = { item = {
@ -256,4 +258,4 @@ var initPhotoSwipeFromDOM = function(gallerySelector) {
// execute above function // execute above function
initPhotoSwipeFromDOM('.rhymix_content, .xe_content'); initPhotoSwipeFromDOM('.rhymix_content, .xe_content');