mirror of
https://github.com/lovell/sharp.git
synced 2026-02-05 06:06:18 +01:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5d85a8697 | ||
|
|
ae9a8b0f57 |
16
README.md
16
README.md
@@ -51,7 +51,7 @@ Scale and crop to `width` x `height` calling `callback` when complete.
|
|||||||
|
|
||||||
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data when a Buffer is requested.
|
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data when a Buffer is requested.
|
||||||
|
|
||||||
### Examples
|
#### Examples
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
|
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
|
||||||
@@ -101,6 +101,20 @@ sharp.resize("input.jpg", sharp.buffer.webp, 200, 300, {canvas: sharp.canvas.emb
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### cache([limit])
|
||||||
|
|
||||||
|
If `limit` is provided, set the `vips` internal cache limit to this value in MB. The default value is 100.
|
||||||
|
|
||||||
|
Always returns cache statistics, namely current usage, high water mark and maximum limit.
|
||||||
|
|
||||||
|
The high water mark may be higher than the maximum limit.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var stats = sharp.cache(); // { current: 98, high: 115, limit: 100 }
|
||||||
|
sharp.cache(200); // { current: 98, high: 115, limit: 200 }
|
||||||
|
sharp.cache(50); // { current: 49, high: 115, limit: 50 }
|
||||||
|
```
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
npm test
|
npm test
|
||||||
|
|||||||
7
index.js
7
index.js
@@ -53,6 +53,13 @@ module.exports.resize = function(input, output, width, height, options, callback
|
|||||||
sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback);
|
sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.cache = function(limit) {
|
||||||
|
if (Number.isNaN(limit)) {
|
||||||
|
limit = null;
|
||||||
|
}
|
||||||
|
return sharp.cache(limit);
|
||||||
|
}
|
||||||
|
|
||||||
/* Deprecated v0.0.x methods */
|
/* Deprecated v0.0.x methods */
|
||||||
module.exports.crop = function(input, output, width, height, sharpen, callback) {
|
module.exports.crop = function(input, output, width, height, sharpen, callback) {
|
||||||
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);
|
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sharp",
|
"name": "sharp",
|
||||||
"version": "0.1.6",
|
"version": "0.1.7",
|
||||||
"author": "Lovell Fuller",
|
"author": "Lovell Fuller",
|
||||||
"description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library",
|
"description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
28
src/sharp.cc
28
src/sharp.cc
@@ -268,11 +268,6 @@ void resize_async_after(uv_work_t *work, int status) {
|
|||||||
|
|
||||||
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
||||||
|
|
||||||
// Free temporary copy of input buffer
|
|
||||||
if (baton->buffer_in_len > 0) {
|
|
||||||
g_free(baton->buffer_in);
|
|
||||||
}
|
|
||||||
|
|
||||||
Handle<Value> argv[2] = { Null(), Null() };
|
Handle<Value> argv[2] = { Null(), Null() };
|
||||||
if (!baton->err.empty()) {
|
if (!baton->err.empty()) {
|
||||||
// Error
|
// Error
|
||||||
@@ -301,12 +296,8 @@ Handle<Value> resize(const Arguments& args) {
|
|||||||
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
||||||
if (args[1]->IsObject()) {
|
if (args[1]->IsObject()) {
|
||||||
Local<Object> buffer = args[1]->ToObject();
|
Local<Object> buffer = args[1]->ToObject();
|
||||||
// Take temporary copy of input buffer
|
|
||||||
if (Buffer::Length(buffer) > 0) {
|
|
||||||
baton->buffer_in_len = Buffer::Length(buffer);
|
baton->buffer_in_len = Buffer::Length(buffer);
|
||||||
baton->buffer_in = g_malloc(baton->buffer_in_len);
|
baton->buffer_in = Buffer::Data(buffer);
|
||||||
memcpy(baton->buffer_in, Buffer::Data(buffer), baton->buffer_in_len);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
baton->file_out = *String::Utf8Value(args[2]->ToString());
|
baton->file_out = *String::Utf8Value(args[2]->ToString());
|
||||||
baton->width = args[3]->Int32Value();
|
baton->width = args[3]->Int32Value();
|
||||||
@@ -332,6 +323,22 @@ Handle<Value> resize(const Arguments& args) {
|
|||||||
return scope.Close(Undefined());
|
return scope.Close(Undefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Handle<Value> cache(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
// Set cache limit
|
||||||
|
if (args[0]->IsInt32()) {
|
||||||
|
vips_cache_set_max_mem(args[0]->Int32Value() * 1048576);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get cache statistics
|
||||||
|
Local<Object> cache = Object::New();
|
||||||
|
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
|
||||||
|
cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
|
||||||
|
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
||||||
|
return scope.Close(cache);
|
||||||
|
}
|
||||||
|
|
||||||
static void at_exit(void* arg) {
|
static void at_exit(void* arg) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
vips_shutdown();
|
vips_shutdown();
|
||||||
@@ -342,6 +349,7 @@ extern "C" void init(Handle<Object> target) {
|
|||||||
vips_init("");
|
vips_init("");
|
||||||
AtExit(at_exit);
|
AtExit(at_exit);
|
||||||
NODE_SET_METHOD(target, "resize", resize);
|
NODE_SET_METHOD(target, "resize", resize);
|
||||||
|
NODE_SET_METHOD(target, "cache", cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_MODULE(sharp, init)
|
NODE_MODULE(sharp, init)
|
||||||
|
|||||||
@@ -26,4 +26,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}, function() {});
|
}, function() {
|
||||||
|
console.dir(sharp.cache());
|
||||||
|
});
|
||||||
|
|||||||
@@ -420,4 +420,5 @@ async.series({
|
|||||||
Object.keys(results).forEach(function(format) {
|
Object.keys(results).forEach(function(format) {
|
||||||
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
|
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
|
||||||
});
|
});
|
||||||
|
console.dir(sharp.cache());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user