async.forEach(Media.SIZES, function (size, done) { var width = parseInt(size.split('x')[0]); var height = parseInt(size.split('x')[1] || 0); var resizeFilename = data.filename.split('.').slice(0, -1).join('.') + '_' + size + '.' + data.filename.split('.').slice(-1)[0]; //resize and upload each file im.crop({ srcPath: data.filename, dstPath: resizeFilename, width: width, height: height }, function (err, stdout, stderr) { if (err) { return done(err); } else { im.identify(resizeFilename, function (err, features) { data.resized.push({ size: size, width: features.width, height: features.height, filename: resizeFilename.split('/').slice(-1)[0] }); done(); }); } }); }, function (err) {
fs.unlink(temp_path, function(err) { if (err) { console.error(err); } else { im.crop({ srcPath: new_location + file_name, dstPath: new_location + 'thumb/img_' + file_name, width: Number(wdth) / 4, height: Number(hgth) / 4, quality: 1, gravity: "North" }, function(err, stdout, stderr) { if (err) { console.log(err); } else { im.crop({ srcPath: new_location + file_name, dstPath: new_location + 'large/img_' + file_name, width: Number(wdth), height: Number(hgth), quality: 1, gravity: "North" }, function(err, stdout, stderr) { if (err) { console.log(err); } }); } }); } });
var saveImageToParse = function (image, prefix, fileName, callback) { var buf = new Buffer((prefix + image).replace(/^data:image\/\w+;base64,/, ""), 'base64'); //resize then upload to parse im.crop({ srcData: buf, width: 250, height: 250, gravity: "North" }, function(err, data){ if (err) { console.error('Error processing image', err); } else { var timestamp = new Date().getTime(); var path = __dirname + '/' + timestamp + "_" + fileName; console.log("Writing file to disk"); fs.writeFileSync(path, data, 'binary'); console.log("Uploading to parse.com"); kaiseki.uploadFile(path, function (err, res, body, success) { fs.unlink(path, function (err) { console.log('successfully deleted: ' + path); }); if (success) { callback(body); } }); } }); };
var generateSplash = function(platform, splash) { var deferred = Q.defer(); try { var filePath = path.join(platform.splashPath, splash.name); var filedirName = path.dirname(filePath); if (!fs.existsSync(filedirName)) { nodeFs.mkdirSync(filedirName, '0777', true); } ig.crop({ srcPath: settings.SPLASH_FILE, dstPath: filePath, quality: 1, format: splash.name.replace(/.*\.(\w+)$/i, '$1').toLowerCase(), width: splash.width, height: splash.height, }, function(err, stdout, stderr) { if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(splash.name + ' created'); } }); } catch (error) { deferred.reject(err); } return deferred.promise; };
var generateSplash = function (platform, splash) { var deferred = Q.defer(); var srcPath = settings.SPLASH_FILE; var platformPath = srcPath.replace(/\.png$/, '-' + platform.name + '.png'); if (fs.existsSync(platformPath)) { srcPath = platformPath; } var dstPath = platform.splashPath + splash.name; var dst = path.dirname(dstPath); if (!fs.existsSync(dst)) { wrench.mkdirSyncRecursive(dst); } ig.crop({ srcPath: srcPath, dstPath: dstPath, quality: 1, format: 'png', width: splash.width, height: splash.height } , function(err, stdout, stderr){ if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(splash.name + ' created'); } }); return deferred.promise; };
im.identify(image, function(err, features){ w = features.width; h = features.height; aspect_ratio = parseFloat(w / h).toFixed(5); if(aspect_ratio != target_aspect_ratio) { new_width = Math.floor(h * target_aspect_ratio); new_filename = source_image_path + generate_file_name(target_aspect_ratio, new_width, h); im.crop({ srcPath: image, width: new_width, height: h, }, function(err, stdout, stderr){ fs.writeFile(new_filename, stdout, "binary", function(){ display_image_at_size(res, new_filename, width, height); }); }); } else { display_image_at_size(res, image, width, height); } });
fs.rename(fileUploaded.path, newPath, function (err) { if(err){ console.error("[filehandler::upload] Error! An error ocurred while moving the file from [%s] to [%s]: %j", fileUploaded.path, newPath, err); res.redirect("/"); res.end(); return; } im.crop({ srcPath: newPath, dstPath: thumbPath, width: 280, height: 120, gravity: 'Center' }, function(err, stdout, stderr){ if (err) { console.error("[filehandler::upload] Error! An error ocurred while cropping the file [%s]: %j", newPath, err); } var result = { "files": [{ "name": imageName, "size": fileUploaded.size, "url": "/static/uploaded/fullsize/"+imageName, "thumbnailUrl": "/static/uploads/thumbs/"+imageName, "deleteUrl": "/static/uploads/fullsize/"+imageName, "deleteType": "DELETE" }] }; console.log("[filehandler::upload] File uploaded to [%s] with thumbnail [%s]", newPath, thumbPath); res.send(result); }); });
crop: function(callback){ im.crop({ srcPath: path, dstPath: server.set('public') + dest_square, gravity: "NorthWest", width: 175, height: 175}, callback); }
exports.getFileFormat = function (file, section, format) { var conf = config.sections[section].thumbnails[format]; if (file && file.file) { var source = file.preview ? file.preview : file.file; var folder = source.substring(0,source.lastIndexOf('/')+1); file = source.substring(source.lastIndexOf('/')+1,source.length); var formatfolder = folder+conf.w+'x'+conf.h+'/'; var ext = file.substring(file.lastIndexOf('.')+1,file.length); var formatfile = file.substring(0,file.lastIndexOf('.'))+'_'+ext+'.jpg'; var originalImg = folder+file; var formatImg = formatfolder+formatfile; /* console.log("---------"); console.log(config.sitepath); console.log(config.uploadpath); console.log(folder); console.log(file); console.log(formatfolder); console.log(formatfile); console.log(config.sitepath+config.uploadpath); console.log(config.sitepath+config.uploadpath+formatImg); console.log(config.sitepath+config.uploadpath+originalImg); console.log("---------"); */ if (fs.existsSync(config.sitepath+config.uploadpath+formatImg)) { return formatImg; } else if (fs.existsSync(config.sitepath+config.uploadpath+originalImg)) { im.crop({ srcPath: config.sitepath+config.uploadpath+originalImg, dstPath: config.sitepath + config.uploadpath + formatImg, width: conf.w, height: conf.h, quality: 1, gravity: 'North' }, function (err) { if (err) return console.log(err.stack || err); // FIXME (not available as done, need to reload) }); //console.log(formatfolder + formatfile); return formatImg; } else { if (fs.existsSync(config.sitepath+config.uploadpath+conf.default)) { return this.getFileFormat({file:conf.default}, section, format); } else { return conf.default; } } } else { if (fs.existsSync(config.sitepath+config.uploadpath+conf.default)) { return this.getFileFormat({file:conf.default}, section, format); } else { return conf.default; } } };
function (nextstep) { im.crop({ srcPath: outpath, dstPath: outpath, quality: options.quality || 0.8, format: options.format || 'png', width: options.size.width, height: options.size.height }, nextstep) }
function cropImage(){ ig.crop(imageProp, function(err, stdout, stderr){ if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(image.name + ' created'); } }); }
crop: function(callback){ if(ext === 'psd' || ext === 'tif') path = path+'[0]'; im.crop({ srcPath: path, dstPath: server.set('public') + dest_square, //gravity: "NorthWest", width: 175, height: 175}, callback); }
fs.rename(tmp_path, target_path, function(err) { fs.unlink(tmp_path, function() {}); im.crop({ srcPath: target_path, dstPath: target_path, format: 'png', width: 25 }, function(err, stdout, stderr){}); res.send("/appimages/" + req.body.app_image_id + ".png"); });
function(nextstep){ im.crop({ srcPath:outpath, dstPath:outpath, quality:0.8, format:'png', width:size.width, height:size.height }, nextstep) }
fs.writeFile(newPath, data, function (err) { im.crop({ srcPath: newPath, dstPath: newPath, width: 100, height: 100, quality: 1, gravity: "Center" }, function(err, stdout, stderr){ res.send("uploads/"+current); }); });
}, function (dimension, callback) { // crop the image var size = dimension.width > dimension.height ? dimension.height : dimension.width; im.crop({ srcPath: avatarFile.path, dstPath: avatarFile.path, width: size, height: size, quality: 1, gravity: "Center" }, function (error) { callback(error); }); }, function (callback) {
function crop(width, heigth, file) { var defer = q.defer(); im.crop({ srcPath: (file) ? file : this.file, width: width, height: heigth }, function (err, stdout, stderr) { if (err) defer.reject(err); else defer.resolve(stdout); }); return defer.promise; }
function cropImage(path,size) { var im = require('imagemagick'); im.crop({ srcPath: path, dstPath: path, width: size, height: size, quality: 1, gravity: "North", filter: 'Blackman', //customArgs: ['-setImageOrientation', '1'], }, function(err, stdout, stderr){ if (err) throw err //fs.writeFileSync(path, stdout, 'binary'); }); };
var generateSplash = function (platform, splash) { var deferred = Q.defer(); ig.crop({ srcPath: settings.SPLASH_FILE, dstPath: platform.splashPath + splash.name, quality: 1, format: 'png', width: splash.width, height: splash.height, } , function(err, stdout, stderr){ if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(splash.name + ' created'); } }); return deferred.promise; };
user.save(function(err) { if(!err){ im.crop({ srcPath: files.file.path, dstPath: './client/uploads/user/'+img, width: 128, height: 128, quality: 0.9, gravity: "Center" }, function(err, stdout, stderr){ if (err) throw err; }); res.sendStatus(200); } else { res.sendStatus(500); } });
var generateIcon = function (icon) { var deferred = Q.defer(); var srcPath = settings.ICON_FILE; var dstPath = settings.DESTINATION + icon.name; var dst = path.dirname(dstPath); if (!fs.existsSync(dst)) { wrench.mkdirSyncRecursive(dst); } ig.resize({ srcPath: srcPath, dstPath: dstPath, quality: 1, format: 'png', width: icon.size, height: icon.size } , function(err, stdout, stderr){ if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(icon.name + ' created'); } }); if (icon.height) { ig.crop({ srcPath: srcPath, dstPath: dstPath, quality: 1, format: 'png', width: icon.size, height: icon.height } , function(err, stdout, stderr){ if (err) { deferred.reject(err); } else { deferred.resolve(); display.success(icon.name + ' cropped'); } }); } return deferred.promise; };
im.identify('downloaded/'+pageCount+'/'+filename, function(err, output){ if (err){ return false; } console.log('Cropping ' + filename); im.crop({ srcPath: 'downloaded/'+pageCount+'/'+filename, width: output.width, height: output.height - 30, quality: 1, gravity: "North" },function(err,stdout,strerr){ if(err){ return false; } fs.writeFileSync('cropped/'+pageCount+'/'+filename, stdout, 'binary'); }); });
var cropImage = function(file, fields) { if (fields['x']) { im.crop({ srcPath: file, dstPath: file, width: fields['w'], height: fields['h'], offset: {x: fields['x'], y: fields['y']} }, function(err, stdout, stderr) { im.resize({ srcData: file, dstPath: file, width: 200, height: 200 }, function() { cropping[fields.app] = false; }); }); } }
fs.stat(image.dest, function(err, stat) { // if file doesnt exist or force flag is set to true if (err != null || force) { // crop image im.crop({ srcPath: image.file, dstPath: image.dest, width: image.resize.width, height: image.resize.height, quality: 1 }, function(err) { if (err != null) { // if error, try to create folder and call crop as a callback createFolder(image, function() { crop(image); }); } }); } });
process: function(){ var self = this; // get the next item from the queue var img = this.queue.pop(); // skip this file if it already exists if( fs.existsSync(img.dest) && !this.options.force ) return this.tick(); // actual image resizing im.crop({ srcPath: img.src, dstPath: img.dest, width: img.w, height: img.h, }, function(err, stdout, stderr){ if (err) throw err console.log("- created image: "+ img.dest) // process the next image self.tick(); }); },
fs.exists(dstPath, function (exists) { if (!exists) { im.crop({ srcPath: srcPath, dstPath: dstPath, width: width, height: height }, function(err, stdout, stderr) { if (err) throw new HttpException(request, response, err.message); croche.serveFromCache(request, response, dstPath, mimeType); }); } else { croche.serveFromCache(request, response, dstPath, mimeType); } });
exports.resizeImage = function (options, stream, callback) { const format = options.format || ''; const width = options.width; const height = options.height; if (format === 'c') { im.crop({ srcData: stream, width: width, height: height, gravity: 'center' }, (err, stdout) => { callback(new Buffer(stdout, 'binary')); }); } else { im.resize({ srcData: stream, width: width, height: height }, (err, stdout) => { callback(new Buffer(stdout, 'binary')); }); } };
q.all([stores.view(req,$arr.config.mysql,q)]).then(function(results) { req.body.logo = results[0][0][0].logo; req.body.banner = results[0][0][0].banner; if(typeof(req.files.store_banner_image) !== 'undefined') { var fs = require('fs'); var im = require('imagemagick'); im.crop({ srcPath: $arr.config.paths.path+'public/uploads/store/'+req.files.store_banner_image.name, dstPath: $arr.config.paths.path+'public/uploads/store/banner_'+req.files.store_banner_image.name, width: 1078, height: 192, quality: 1, gravity: "Center" }, function(err, stdout, stderr){ // foo }); stores.updateStores(req,$arr.config.mysql,q); res.writeHead(302, { 'Location': '/stores/view/'+req.param('sid') }); res.end();return false; } else { stores.updateStores(req,$arr.config.mysql,q); res.writeHead(302, { 'Location': '/stores/view/'+req.param('sid') }); res.end();return false; } });
fs.rename(fileUploaded.path, newPath, function (err) { if(err){ console.error("[filehandler::upload] Error! An error ocurred while moving the file from [%s] to [%s]: %j", fileUploaded.path, newPath, err); res.end(); return; } try { /* im.identify(['-strip', newPath], function(err, features){ console.log(err); if (err) throw err; console.log(features); // { format: 'JPEG', width: 3904, height: 2622, depth: 8 } */ im.crop({ srcPath: newPath, dstPath: thumbPath, width: 280, height: 120 }, function(errr, stdout, stderr){ console.log(errr); if (errr) { console.error("[filehandler::upload] Error! An error ocurred while cropping the file [%s]: %j", newPath, errr); } var result = { "files": [{ "name": imageName, "size": fileUploaded.size, "url": "/static/uploaded/fullsize/"+imageName, "thumbnailUrl": "/static/uploads/thumbs/"+imageName, "deleteUrl": "/static/uploads/fullsize/"+imageName, "deleteType": "DELETE" }] }; console.log("[filehandler::upload] File uploaded to [%s] with thumbnail [%s]", newPath, thumbPath); res.send(result); }); //}); } catch(e) { console.error("[filehandler::upload] "+e+" Error! An error ocurred while moving the file from [%s] to [%s]: %j", fileUploaded.path, newPath, err); res.redirect("/"); res.end(); return; } });
router.post('/', function (req, res) { var store_id = req.param('id'); var store_name = req.body.txtStoreName; var address = []; var city = req.body.txtCity; var district = req.body.txtDistrict; var street = req.body.txtStreet; var room = req.body.txtRoom; address.push({"city": city, "district": district, "street": street, "room": room}); var latitude = req.body.txtLatitude; var longitude = req.body.txtLongitude; var phone = req.body.txtPhone; var description = req.body.txtDescription; var industry = req.body.slcIndustry; var hours_of_work = req.body.txtHoursOfWork; var website = req.body.txtWebsite; var fanpage = req.body.txtFanpage; var im = require('imagemagick'); //Cover: var cover_new = cover; if (typeof req.files.ulfCover != 'undefined') { var cover_upload_path = req.files.ulfCover.path; var cover_save_path = "public/images/" + req.files.ulfCover.name; /*im.resize({ srcPath: cover_upload_path, dstPath: cover_save_path, width: 800 }, function (err, stdout, stderr) { if (err) throw err; console.log('Resized cover successful.'); });*/ var option = { srcPath: cover_upload_path, dstPath: cover_save_path, width: 1100, height: 400, quality: 1, gravity: "Center" }; im.crop(option, function (err, stdout, stderr) { if (err) throw err; console.log('Resized cover successful.'); }); cover_save_path = ".." + req.files.ulfCover.path.replace("public", ""); cover_new = cover_save_path; } console.log(cover_new); //Logo: var logo_new = logo; if (typeof req.files.ulfLogo != 'undefined') { var logo_upload_path = req.files.ulfLogo.path; var logo_save_path = "public/images/" + req.files.ulfLogo.name; im.resize({ srcPath: logo_upload_path, dstPath: logo_save_path, width: 500 }, function (err, stdout, stderr) { if (err) throw err; console.log('Resized logo successful.'); }); logo_save_path = ".." + req.files.ulfLogo.path.replace("public", ""); logo_new = logo_save_path; } console.log(logo_new); store_schema.store.update({_id: store_id}, {$set: {store_name: store_name, address: address, latitude: latitude, longitude: longitude, phone: phone, description: description, industry: industry, hours_of_work: hours_of_work, cover: cover_new, logo: logo_new, website: website, fanpage: fanpage}}, function (error, result) { if (!error && result) { var query_store = store_schema.store.find({"_id": store_id}); query_store.limit(8); query_store.sort({date: -1}); query_store.exec(function (store_error, store_array) { if (store_array && store_array.length > 0) { var query_product = product_schema.product.find({"id_store": store_id}); query_product.limit(10); query_product.sort({date: -1}); query_product.exec(function (product_error, product_array) { if (product_array && product_array.length > 0) { res.render('store_detail', {store_id: store_id, store_array: store_array, industry_array: req.session.industry_array, product_array: product_array}); } else { res.render("store_detail", {store_id: store_id, industry_array: req.session.industry_array, store_array: store_array, product_array: product_array, product_notification: "Không có sản phẩm tồn tại."}); } }); } else { console.log(store_error); } }); } else { console.log(error); } }); });