-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Jimp as fallback when GraphicsMagick isn't installed (#10)
- Loading branch information
Showing
15 changed files
with
475 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import gm from 'gm'; | ||
import fsExtra from 'fs-promise'; | ||
import path from 'path'; | ||
|
||
import generateUUID from '../generateUUID'; | ||
import CropDimension from '../CropDimension'; | ||
|
||
const tmpDir = path.join(__dirname, '../../../.tmp'); | ||
|
||
|
||
/** | ||
* Crops an image | ||
* @param {string} base64Screenshot image to crop | ||
* @param {CropDimension} cropDimensions dimensions | ||
* @return {string} cropped image | ||
*/ | ||
export async function cropImage(base64Screenshot, cropDimensions) { | ||
|
||
if (!(cropDimensions instanceof CropDimension )) { | ||
throw new Error('Please provide a valid instance of CropDimension!'); | ||
} | ||
|
||
const image = gm(new Buffer(base64Screenshot, 'base64')) | ||
|
||
if (cropDimensions.getRotation() !== 0) { | ||
image.rotate('white', cropDimensions.getRotation()); | ||
} | ||
|
||
image.gravity(cropDimensions.getGravity()); | ||
image.crop(cropDimensions.getWidth(), cropDimensions.getHeight(), cropDimensions.getX(), cropDimensions.getY()); | ||
|
||
return new Promise((resolve, reject) => { | ||
image.toBuffer('PNG',function (err, buffer) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(buffer.toString('base64')); | ||
}) | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Merges mulidimensional array of images to a single image: | ||
* @param {string[][]} images array of images | ||
* @return {string} screenshot | ||
*/ | ||
export async function mergeImages(images) { | ||
|
||
const uuid = generateUUID(); | ||
const dir = path.join(tmpDir, uuid); | ||
|
||
try { | ||
await fsExtra.ensureDir(dir); | ||
|
||
// merge all horizintal screens | ||
const rowImagesPromises = images.map(((colImages, key) => { | ||
const firstImage = colImages.shift(); | ||
const rowImage = gm(firstImage); | ||
|
||
if (colImages.length) { | ||
colImages.push(true); | ||
rowImage.append.apply(rowImage, colImages); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const file = path.join(dir, `${key}.png`); | ||
rowImage.write(file, function (err) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(file); | ||
}); | ||
}); | ||
})); | ||
|
||
// merge all vertical screens | ||
const base64Screenshot = await Promise | ||
.all(rowImagesPromises) | ||
.then((rowImages) => { | ||
const firstImage = rowImages.shift(); | ||
const mergedImage = gm(firstImage); | ||
|
||
if (rowImages.length) { | ||
mergedImage.append.apply(mergedImage, rowImages); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
mergedImage.toBuffer('PNG',function (err, buffer) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(buffer.toString('base64')); | ||
}); | ||
}); | ||
}); | ||
|
||
await fsExtra.remove(dir); | ||
return base64Screenshot; | ||
|
||
} catch (e) { | ||
try { | ||
await fsExtra.remove(dir); | ||
} catch(e) {} | ||
|
||
throw e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as jimp from './jimp'; | ||
import * as gm from './gm'; | ||
import which from 'which'; | ||
import debug from 'debug'; | ||
const log = debug('wdio-screenshot:image'); | ||
|
||
let gmInstalled = false; | ||
|
||
try { | ||
gmInstalled = !!which.sync('gm'); | ||
} catch(e) {} | ||
|
||
log(`Use image processing library: ${gmInstalled ? 'GraphicsMagick' : 'Jimp'}`); | ||
|
||
const { cropImage, mergeImages } = gmInstalled ? gm : jimp; | ||
export { cropImage, mergeImages }; |
Oops, something went wrong.