Skip to content
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

Refactoring isGIF() as a utility function #1511

Merged
merged 10 commits into from
Jan 17, 2020
7 changes: 4 additions & 3 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var defaultHtmlSequencerUi = require('./lib/defaultHtmlSequencerUi.js'),
DefaultHtmlStepUi = require('./lib/defaultHtmlStepUi.js'),
urlHash = require('./lib/urlHash.js'),
insertPreview = require('./lib/insertPreview.js'),
versionManagement = require('./lib/versionManagement.js');
versionManagement = require('./lib/versionManagement.js'),
isGIF = require('../src/util/isGif');


window.onload = function () {
Expand Down Expand Up @@ -270,8 +271,8 @@ window.onload = function () {
* @param {string} imageDataURL - The data URL for the image.
*/
function savePDF(imageDataURL) {
sequencer.getImageDimensions(imageDataURL, function(dimensions, isGIF) {
if (!isGIF) {
sequencer.getImageDimensions(imageDataURL, function(dimensions) {
if (isGIF(imageDataURL)) {
// Get the dimensions of the image.
let pageWidth = dimensions.width;
let pageHeight = dimensions.height;
Expand Down
7 changes: 4 additions & 3 deletions examples/lib/defaultHtmlStepUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const intermediateHtmlStepUi = require('./intermediateHtmlStepUi.js'),
urlHash = require('./urlHash.js'),
_ = require('lodash'),
mapHtmlTypes = require('./mapHtmltypes'),
scopeQuery = require('./scopeQuery');
scopeQuery = require('./scopeQuery'),
isGIF = require('../../src/util/isGif');

function DefaultHtmlStepUi(_sequencer, options) {
options = options || {};
Expand Down Expand Up @@ -379,8 +380,8 @@ function DefaultHtmlStepUi(_sequencer, options) {
*
*/
function updateDimensions(step){
_sequencer.getImageDimensions(step.imgElement.src, function (dim, isGIF) {
step.ui.querySelector('.' + step.name).attributes['data-original-title'].value = `<div style="text-align: center"><p>Image Width: ${dim.width}<br>Image Height: ${dim.height}</br>${isGIF ? `Frames: ${dim.frames}` : ''}</div>`;
_sequencer.getImageDimensions(step.imgElement.src, function (dim) {
step.ui.querySelector('.' + step.name).attributes['data-original-title'].value = `<div style="text-align: center"><p>Image Width: ${dim.width}<br>Image Height: ${dim.height}</br>${isGIF(step.output) ? `Frames: ${dim.frames}` : ''}</div>`;
});
}

Expand Down
7 changes: 1 addition & 6 deletions src/util/getImageDimensions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const getPixels = require('get-pixels');
module.exports = function getImageDimensions(img, cb) {
let dimensions;
let isGIF;
getPixels(img, function(err, pixels) {
if (pixels.shape.length === 4) {
const [frames, width, height] = pixels.shape;
Expand All @@ -10,20 +9,16 @@ module.exports = function getImageDimensions(img, cb) {
width,
height
};

isGIF = true;
}
else {
const [width, height] = pixels.shape;
dimensions = {
width,
height
};

isGIF = false;
}

if (cb) cb(dimensions, isGIF);
if (cb) cb(dimensions);
});
};

7 changes: 7 additions & 0 deletions src/util/isGif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(url){
if(url.match){
const toMatch = url.match(/data:image\/(.*);/) || 'png';
return toMatch[1] === 'gif';
}else
return false;
};