Skip to content

Commit

Permalink
Add support to retrieve image dimensions from a Photon url
Browse files Browse the repository at this point in the history
Added support to retrieve image dimensions from a Photon url, and also
changed the regex used to retrieve the dimension in standard Wordpress
image urls so that it retrieves both the width and height correctly.
  • Loading branch information
Draikin committed Aug 8, 2015
1 parent ea5ff33 commit b4ad963
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions modules/carousel/jetpack-carousel.js
Expand Up @@ -948,16 +948,22 @@ jQuery(document).ready(function($) {

if ( 'undefined' === typeof args.medium_file || 'undefined' === typeof args.large_file ) {
return args.orig_file;
}

var medium_size = args.medium_file.replace(/-([\d]+x[\d]+)\..+$/, '$1'),
medium_size_parts = (medium_size !== args.medium_file) ? medium_size.split('x') : [args.orig_width, 0],
}

// Check if the image is being served by Photon

var imageLinkParser = document.createElement('a');
imageLinkParser.href = args.large_file;

var isPhotonUrl = (imageLinkParser.hostname.match(/^i[\d]{1}.wp.com$/i) != null);

var medium_size_parts = gallery.jp_carousel('getImageSizeParts', args.medium_file, args.orig_width, isPhotonUrl);
var large_size_parts = gallery.jp_carousel('getImageSizeParts', args.large_file, args.orig_width, isPhotonUrl);

var large_width = parseInt( large_size_parts[0], 10 ),
large_height = parseInt( large_size_parts[1], 10 ),
medium_width = parseInt( medium_size_parts[0], 10 ),
medium_height = parseInt( medium_size_parts[1], 10 ),
large_size = args.large_file.replace(/-([\d]+x[\d]+)\..+$/, '$1'),
large_size_parts = (large_size !== args.large_file) ? large_size.split('x') : [args.orig_width, 0],
large_width = parseInt( large_size_parts[0], 10 ),
large_height = parseInt( large_size_parts[1], 10 );
medium_height = parseInt( medium_size_parts[1], 10 );

// Give devices with a higher devicePixelRatio higher-res images (Retina display = 2, Android phones = 1.5, etc)
if ( 'undefined' !== typeof window.devicePixelRatio && window.devicePixelRatio > 1 ) {
Expand All @@ -975,6 +981,27 @@ jQuery(document).ready(function($) {

return args.orig_file;
},

getImageSizeParts: function(file, orig_width, isPhotonUrl) {
var size = isPhotonUrl ?
file.replace(/.*=([\d]+%2C[\d]+).*$/, '$1') :
file.replace(/.*-([\d]+x[\d]+)\..+$/, '$1');

var size_parts = (size !== file) ?
(isPhotonUrl ? size.split('%2C') : size.split('x')) :
[orig_width, 0];

if (size_parts[0] === '9999')
{
size_parts[0] = '0';
}
if (size_parts[1] === '9999')
{
size_parts[1] = '0';
}

return size_parts;
},

originalDimensions: function() {
var splitted = $(this).data('orig-size').split(',');
Expand Down

0 comments on commit b4ad963

Please sign in to comment.