Skip to content

Commit

Permalink
Merge pull request #846 from aheckmann/windows
Browse files Browse the repository at this point in the history
Windows
  • Loading branch information
aheckmann committed Sep 21, 2022
2 parents be71cef + bf11965 commit b2ca6ca
Show file tree
Hide file tree
Showing 123 changed files with 1,011 additions and 859 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,36 @@ on:

jobs:
build:
runs-on: ${{ matrix.os }}

runs-on: ubuntu-latest
env:
DEBUG: "gm*"

strategy:
matrix:
os: [windows-latest, ubuntu-latest]
node-version: [14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- name: Install GraphicsMagic and Imagemagick
- name: Install GraphicsMagic and Imagemagick on Ubuntu
if: contains(matrix.os, 'ubuntu')
run: sudo apt-get install -y imagemagick graphicsmagick
- name: Install GraphicsMagic and Imagemagick on Windows
if: contains(matrix.os, 'windows')
run: choco install -y imagemagick graphicsmagick
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm i
- run: npm test
- name: Run tests on Windows
if: contains(matrix.os, 'windows')
shell: cmd
run: |
call refreshenv
npm test
- name: Run tests on Ubuntu
if: contains(matrix.os, 'ubuntu')
run: npm test
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,31 @@ or clone the repo:

## Use ImageMagick instead of gm

Subclass `gm` to enable ImageMagick, optionally specifying the path to the executable.
Subclass `gm` to enable [ImageMagick 7+](https://imagemagick.org/script/porting.php)

```js
const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: '7+' });
```

Or, to enable ImageMagick legacy mode (for ImageMagick version < 7)

```js
const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: true });
```

## Specify the executable path

Optionally specify the path to the executable.

```js
const fs = require('fs')
const gm = require('gm').subClass({
imageMagick: true,
appPath: String.raw`C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe`
});

// resize and remove EXIF profile data
gm('/path/to/my/img.jpg')
.resize(240, 240)
...
```


## Basic Usage

```js
Expand Down Expand Up @@ -622,6 +631,15 @@ http:https://github.com/quiiver/magickal-node
## Plugins
[https://github.com/aheckmann/gm/wiki](https://github.com/aheckmann/gm/wiki)

## Tests
`npm test`

To run a single test:

```
npm test -- alpha.js
```

## License

(The MIT License)
Expand Down
25 changes: 22 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,28 @@ module.exports = function (proto) {

proto._spawn = function _spawn (args, bufferOutput, callback) {
var appPath = this._options.appPath || '';
var bin = this._options.imageMagick
? appPath + args.shift()
: appPath + 'gm'
var bin

// Resolve executable
switch (this._options.imageMagick) {
// legacy behavior
case true:
bin = args.shift();
break;

// ImgeMagick >= 7
case '7+':
bin = 'magick'
break;

// GraphicsMagick
default:
bin = 'gm';
break;
}

// Prepend app path
bin = appPath + bin

var cmd = bin + ' ' + args.map(utils.escape).join(' ')
, self = this
Expand Down
34 changes: 27 additions & 7 deletions lib/compare.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// compare

var spawn = require('cross-spawn');
var debug = require('debug')('gm');
var utils = require('./utils');

/**
* Compare two images uses graphicsmagicks `compare` command.
Expand All @@ -22,13 +24,28 @@ module.exports = exports = function (proto) {

var isImageMagick = this._options && this._options.imageMagick;
var appPath = this._options && this._options.appPath || '';
var bin = isImageMagick
? appPath + 'compare'
: appPath + 'gm'
var args = ['-metric', 'mse', orig, compareTo]
if (!isImageMagick) {
var args = ['-metric', 'mse', orig, compareTo];

// Resove executable
let bin;

switch (isImageMagick) {
case true:
bin = 'compare';
break;
case '7+':
bin = 'magick'
args.unshift('compare');
break;
default:
bin = 'gm'
args.unshift('compare');
break
}

// Prepend app path
bin = appPath + bin

var tolerance = 0.4;
// outputting the diff image
if (typeof options === 'object') {
Expand Down Expand Up @@ -56,13 +73,13 @@ module.exports = exports = function (proto) {
}
args.push(options.file);
}

if (typeof options.tolerance != 'undefined') {
if (typeof options.tolerance !== 'number') {
throw new TypeError('The tolerance value should be a number');
}
tolerance = options.tolerance;
}
}
} else {
// For ImageMagick diff file is required but we don't care about it, so null it out
if (isImageMagick) {
Expand All @@ -76,6 +93,9 @@ module.exports = exports = function (proto) {
}
}

var cmd = bin + ' ' + args.map(utils.escape).join(' ')
debug(cmd);

var proc = spawn(bin, args);
var stdout = '';
var stderr = '';
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
],
"main": "./index",
"scripts": {
"test": "make test;"
"security": "npm audit",
"test": "npm run security && npm run test-integration",
"test-integration": "node test/ --integration",
"test-unit": "node test/"
},
"repository": {
"type": "git",
Expand All @@ -44,4 +47,4 @@
"cross-spawn": "^4.0.0",
"debug": "^3.1.0"
}
}
}
19 changes: 8 additions & 11 deletions test/109.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
var assert = require('assert')
var fs = require('fs')
const fs = require('fs');
const path = require('path');

module.exports = function (_, dir, finish, gm) {
if (!gm.integration)
return finish();
module.exports = function (_, dir, finish, gm, imageMagick) {
if (!gm.integration) return finish();

var original = dir + '/original.jpg';
var result = dir + '/fromBuffer.png';
const original = path.join(dir, 'original.jpg');
const buf = fs.readFileSync(original);
const m = gm(buf, 'original.jpg').options({ imageMagick });

var buf = fs.readFileSync(original);
var m = gm(buf, 'original.jpg');

m.identify(function (err, x) {
m.identify(function (err, _) {
finish(err);
});

Expand Down
21 changes: 11 additions & 10 deletions test/118.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/*
* If only the width is specified for a resize operation,
* If only the width is specified for a resize operation,
* GraphicsMagick requires the format
* -resize 10x
* while ImageMagick requires the format
* -resize 10
* -resize 10
*
*/
var assert = require('assert')
const assert = require('assert')
const path = require('path');

module.exports = function (_, dir, finish, gm) {
if (!gm.integration) return finish();

var src = dir + '/originalSideways.jpg';
var dst = dir + '/originalSideways10x.jpg';
module.exports = function (_, dir, finish, gm, imageMagick) {
if (!gm.integration) return finish();

gm(src).resize(10).write(dst, function(err) {
gm(dst).size(function(err, size) {
var src = path.join(dir, 'originalSideways.jpg');
var dst = path.join(dir, 'originalSideways10x.jpg');

gm(src).options({ imageMagick }).resize(10).write(dst, function(err) {
gm(dst).options({ imageMagick }).size(function(err, size) {
if (err) return finish(err);
assert.equal(10, size.width);
finish();
Expand Down
15 changes: 6 additions & 9 deletions test/393.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');

var assert = require('assert');
var fs = require('fs');
var path = require('path');

module.exports = function (_, dir, finish, gm) {
module.exports = function (_, dir, finish, gm, imageMagick) {
if (!gm.integration) return finish();

var imagePath = path.join(__dirname, './fixtures/nyancat.gif');
var imagePath = path.join(__dirname, 'fixtures', 'nyancat.gif');
var inputStream = fs.createReadStream(imagePath);
gm(inputStream)
.identify({ bufferStream: true }, function(err, value) {
gm(inputStream).options({ imageMagick }).identify({ bufferStream: true }, function(err, value) {
if (err) return finish(err);
var size = value.size;
assert.equal(400, size.width);
Expand Down
37 changes: 18 additions & 19 deletions test/417.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@

var assert = require('assert')
var fs = require('fs');
var path = require('path');
const assert = require('assert')
const fs = require('fs');
const path = require('path');

module.exports = function (_, dir, finish, gm) {
module.exports = function (_, dir, finish, gm, imageMagick) {
if (!gm.integration)
return finish();

gm(dir + '/original.jpg')
.thumb(150, 40, dir + '/thumb.png', function thumb (err) {
gm(dir + '/thumb.png')
.size(function (err, size) {
if (err) return finish(err);
const originalPathName = path.join(dir, 'original.jpg');
const thumbPathName = path.join(dir, 'thumb.png');

assert.equal(142, size.width);
assert.equal(40, size.height);
gm(originalPathName).options({ imageMagick }).thumb(150, 40, thumbPathName, function thumb (err) {
gm(thumbPathName).options({ imageMagick }).size(function (err, size) {
if (err) return finish(err);

gm(dir + '/original.jpg')
.thumbExact(150, 40, dir + '/thumb.png', function thumb (err) {
gm(dir + '/thumb.png')
.size(function (err, size) {
assert.equal(150, size.width);
assert.equal(40, size.height);
finish(err);
});
assert.equal(142, size.width);
assert.equal(40, size.height);

gm(originalPathName).options({ imageMagick }).thumbExact(150, 40, thumbPathName, function thumb (err) {
gm(thumbPathName).options({ imageMagick }).size(function (err, size) {
assert.equal(150, size.width);
assert.equal(40, size.height);
finish(err);
});
});
});
});
}
Loading

0 comments on commit b2ca6ca

Please sign in to comment.