Skip to content

Commit cf644f0

Browse files
committed
Update README
1 parent 6aa3bb7 commit cf644f0

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,22 @@ npm install --save smartcrop-sharp sharp
2626
## Example
2727

2828
```javascript
29-
var request = require('request');
30-
var sharp = require('sharp');
31-
var smartcrop = require('smartcrop-sharp');
29+
const sharp = require('sharp');
30+
const smartcrop = require('smartcrop-sharp');
3231

32+
// finds the best crop of src and writes the cropped and resized image to dest.
3333
function applySmartCrop(src, dest, width, height) {
34-
request(src, { encoding: null }, function process(error, response, body) {
35-
if (error) return console.error(error);
36-
smartcrop.crop(body, { width: width, height: height }).then(function(result) {
37-
var crop = result.topCrop;
38-
sharp(body)
34+
return smartcrop.crop(src, { width: width, height: height })
35+
.then(function(result) {
36+
const crop = result.topCrop;
37+
return sharp(src)
3938
.extract({ width: crop.width, height: crop.height, left: crop.x, top: crop.y })
4039
.resize(width, height)
4140
.toFile(dest);
42-
});
43-
});
41+
})
4442
}
4543

46-
var src = 'https://raw.githubusercontent.com/jwagner/smartcrop-gm/master/test/flower.jpg';
47-
applySmartCrop(src, 'flower-square.jpg', 128, 128);
44+
applySmartCrop('flower.jpg', 'flower-square.jpg', 128, 128);
4845
```
4946

5047
## Face Detection Example

test/test.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
1-
var path = require('path');
2-
var assert = require('chai').assert;
1+
const fs = require('fs');
2+
const path = require('path');
3+
const assert = require('chai').assert;
34

4-
var smartcrop = require('../index.js');
5+
const smartcrop = require('../index.js');
6+
const https = require('https');
57

6-
var imageSrc = path.resolve(__dirname, 'flower.jpg');
8+
const imageSrc = path.resolve(__dirname, 'flower.jpg');
9+
const imageSrcHttp = 'https://raw.githubusercontent.com/jwagner/smartcrop-gm/master/test/flower.jpg';
710

811
describe('smartcrop', function() {
912
describe('crop', function() {
13+
function validateCrop(data) {
14+
assert(data.topCrop, 0);
15+
assert.equal(data.topCrop.x, 0);
16+
assert.equal(data.topCrop.y, 26);
17+
assert.equal(data.topCrop.width, 427);
18+
assert.equal(data.topCrop.height, 427);
19+
}
1020
it('returns a valid crop', function() {
1121
return smartcrop
1222
.crop(imageSrc, { width: 128, height: 128, minScale: 1 })
13-
.then(function(data) {
14-
assert(data.topCrop, 0);
15-
assert.equal(data.topCrop.x, 0);
16-
assert.equal(data.topCrop.y, 26);
17-
assert.equal(data.topCrop.width, 427);
18-
assert.equal(data.topCrop.height, 427);
23+
.then(validateCrop);
24+
});
25+
it('accepts a stream', function() {
26+
function httpsGetBuffer(src) {
27+
return new Promise((resolve,reject) => {
28+
const chunks = [];
29+
https.get(src, (response) => {
30+
response.on('data', chunk => chunks.push(chunk));
31+
response.on('end', () => resolve(Buffer.concat(chunks)));
32+
response.on('error', reject);
33+
});
34+
});
35+
}
36+
return httpsGetBuffer(imageSrcHttp).then(responseBodyBuffer =>
37+
smartcrop
38+
.crop(responseBodyBuffer, { width: 128, height: 128, minScale: 1 })
39+
).then(validateCrop);
40+
});
41+
});
42+
describe('readme example', function() {
43+
it('writes a file', function() {
44+
var sharp = require('sharp');
45+
46+
function applySmartCrop(src, dest, width, height) {
47+
return smartcrop.crop(src, { width: width, height: height }).then(function(result) {
48+
var crop = result.topCrop;
49+
return sharp(src)
50+
.extract({ width: crop.width, height: crop.height, left: crop.x, top: crop.y })
51+
.resize(width, height)
52+
.toFile(dest);
53+
}).then(() => {
54+
fs.unlinkSync(dest);
1955
});
56+
}
57+
58+
return applySmartCrop(imageSrc, 'flower-square.jpg', 128, 128);
2059
});
2160
});
2261
});

0 commit comments

Comments
 (0)