upload(req, res, function(err) { if (err) { return res.send({ status: 0, message:'Something went wrong!', filename: req.file.filename }); } sharp('./uploads/cache/'+ req.file.filename) .resize(1024, 1024) .jpeg({quality: 100, chromaSubsampling: '4:4:4', force: true}) .png({ compressionLevel: 9, adaptiveFiltering: true, force: true }) .toFile('./uploads/profile_pics/'+ req.file.filename, (err) => { if(err){ console.log('Error ' + fileOut + ' ' + err.toString()); }else{ fs.unlink('./uploads/cache/'+ req.file.filename, (err) => { if (err) { console.log('error deleting ' + ' ' + err.toString()); } else { return res.send({ status: 1, message:'File uploaded sucessfully!', filename: req.file.filename }); } }); } }); sharp.cache(false); });
function requireSharp() { var sharp = require('sharp'); if (os.platform() === 'win32') { // https://github.com/lovell/sharp/issues/1259 sharp.cache(false); } return sharp; }
const sharp = require('sharp'); const path = require('path'); sharp.cache({ items: 0, }); const file = path.join(__dirname, 'test.jpg'); // recursive function function runBenchSharp(dir, times, res) { const results = Array.isArray(res) ? res : []; if (results.length < times) { return benchSharp(dir, results.length).then((data) => { results.push(data); return runBenchSharp(dir, times, results); }); } return Promise.resolve(results); } function benchSharp(dir, id) { id = id !== undefined ? id : ''; const start = new Date(); return new Promise((resolve, reject) => { sharp(file) .resize(300) .rotate(270) .toFile(path.join(dir, `sharp${id}.jpg`), function(err) { if (err) { return reject(err);
module.exports = function (callback) { var fs = require('fs'); var path = require('path'); var express = require('express'); var cors = require('cors'); var sharp = require('sharp'); var config = require('./config')(); var packageinfo = require('./package.json'); var imageProcessor = require('./imageProcessor')(sharp, path, config, fs); sharp.cache(0); var app = express(); try { var images = require(config.image_store_path); } catch (e) { var images = []; } fs.mkdir(config.cache_folder_path, function(e) {}); process.addListener('uncaughtException', function (err) { console.log('Uncaught exception: '); console.trace(err); }) var countImage = function () { process.send("count"); } var checkParameters = function (params, queryparams, square, callback) { if ((square && !params.size) || (square && isNaN(parseInt(params.size))) || (!square && !params.width) || (!square && !params.height) || (!square && isNaN(parseInt(params.width))) || (!square && isNaN(parseInt(params.height))) || (queryparams.gravity && sharp.gravity[queryparams.gravity] == null)) { return callback(true, 400, 'Invalid arguments'); } if (params.size > config.max_width || params.size > config.max_height || params.width > config.max_height || params.height > config.max_width) { return callback(true, 413, 'Specified dimensions too large'); } callback(false); } var displayError = function (res, code, message) { res.status(code); res.send({ error: message }); } var findMatchingImage = function (id, callback) { var matchingImages = images.filter(function (image) { return image.id == id; }); if (matchingImages.length == 0) { return false; } return matchingImages[0].filename; } var serveImage = function(req, res, square, gray) { checkParameters(req.params, req.query, square, function (err, code, message) { imageProcessor.getWidthAndHeight(req.params, square, function (width, height) { if (err) { return displayError(res, code, message); } var filePath; var blur = false; if (req.query.image) { var matchingImage = findMatchingImage(req.query.image); if (matchingImage) { filePath = matchingImage; } else { return displayError(res, 400, 'Invalid image id'); } } else { filePath = images[Math.floor(Math.random() * images.length)].filename; } imageProcessor.getProcessedImage(width, height, req.query.gravity, gray, !(!req.query.blur && req.query.blur != ''), filePath, (!req.query.image && !req.query.random && req.query.random != ''), function (err, imagePath) { if (err) { console.log('filePath: ' + filePath); console.log('imagePath: ' + imagePath); console.log('error: ' + err); return displayError(res, 500, 'Something went wrong'); } res.sendFile(imagePath); countImage(); }) }) }) } app.use(cors()); app.use(express.static(path.join(__dirname, 'public'))); app.get('/info', function (req, res, next) { res.jsonp({ name: packageinfo.name, version: packageinfo.version, author: packageinfo.author }); }) app.get('/list', function (req, res, next) { var newImages = []; for (var i in images) { var item = images[i]; var image = { format: item.format, width: item.width, height: item.height, filename: path.basename(item.filename), id: item.id, author: item.author, author_url: item.author_url, post_url: item.post_url } newImages.push(image); } res.jsonp(newImages); }) app.get('/:size', function (req, res, next) { serveImage(req, res, true, false); }) app.get('/g/:size', function (req, res, next) { serveImage(req, res, true, true); }) app.get('/:width/:height', function (req, res, next) { serveImage(req, res, false, false); }) app.get('/g/:width/:height', function (req, res, next) { serveImage(req, res, false, true); }) app.get('*', function (req, res, next) { res.status(404); res.send({ error: 'Resource not found' }); }) callback(app); }
var sharp = require('sharp') var path = require('path') var async = require('async') var config = require('./config')() var fs = require('fs') sharp.cache(0) var imageProcessor = require('./imageProcessor')(sharp, path, config, fs) var images = require(config.image_store_path) fs.mkdir(config.cache_folder_path, function (error) {}) var index = process.argv[2] || 0 console.log('Start: %s', index) if (index > 0) { images.splice(0, index) } async.eachLimit(images, 5, function (image, next) { var width = 458 var height = 354 var blur = false imageProcessor.getProcessedImage(width, height, null, false, false, image.filename, false, function (error, imagePath) { if (error) { console.log('filePath: ' + image.filename) console.log('imagePath: ' + imagePath) console.log('error: ' + err) } console.log('%s done', image.id)
function convertImages(array, options, callback, done){ options.limit = options.limit || 1; options.force = options.force || false; var sharpOptions = {}; sharpOptions.files = options.sharpCache.files || 10; sharpOptions.memory = options.sharpCache.memory || 200; sharpOptions.items = options.sharpCache.items || 100; sharp.cache(sharpOptions); var imagesDone = 0; var imagesError = 0; /** * Filter all directoris from the result */ array = array.map(function (item){ if (item.type === 'file') return item; }).filter(truth); var totalLength = array.length; filesExist(array, options, function (err, files){ if (err) imagesError ++; if (err) return callback(err); async.forEachLimit(array, options.limit, function (item, next){ var filePath = path.relative('.', item.path); var parsedFile = path.parse(item.path); if (!isImage(parsedFile.ext)){ imagesError ++; // console.log('Not an image', item.path); return callback('Not an image', item, next); } fs.readFile(item.path, function (err, buffer){ converter.start(item.path, buffer, options, function (err, status){ if (!err) imagesDone ++; if (err) imagesError ++; item.totalFiles = totalLength; item.errors = imagesError; item.done = imagesDone; return callback(err, item, next); }); }); }, function (err){ done({ success : imagesDone, errors : imagesError }) }); }); }
function (err, n) { console.log("CACHE BEFORE EXIT", sharp.cache()); }
var fs = require('fs'); var sharp = require('sharp'); var async = require('async'); var cacheMem = parseInt(process.env.CACHE_MEM || 20); sharp.cache({memory: cacheMem, files:20, items: 100}); console.log("CACHE", sharp.cache()); var fileName = '100mpix.jpg'; var singleTest = function(cb) { fs.readFile(fileName, function (err, buffer ) { if (err) { throw err; } var i = sharp(buffer); i.embed().interpolateWith(sharp.interpolator.bicubic) .resize(1000,1000).rotate() .background({r:0,g:0,b:0,a:0}).quality(75) .toFormat('jpeg').toBuffer(function(err, output, info) { if (err) { throw err; } cb(); }); }); }; var run = function(name) { var count = 0;