Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save docluv/92d07ed88d118afce2a3f0c0039f3bfa to your computer and use it in GitHub Desktop.

Select an option

Save docluv/92d07ed88d118afce2a3f0c0039f3bfa to your computer and use it in GitHub Desktop.
var fs = require('fs'),
path = require("path"),
sizeOf = require('image-size'),
request = require('request').defaults({ proxy: process.env.HTTP_PROXY || process.env.http_proxy, jar: false }),
Kraken = require('kraken');
//If you do not have a Kraken Account visit http://bit.ly/1Vnvnmr
var kraken = new Kraken({
api_key: '{Your Kraken Key Goes Here}',
api_secret: '{Your Kraken Secret Goes Here}'
});
//this function downloads the file locally to your hard drive. This is not required.
function download (uri) {
request.head(uri, function (err, res, body) {
var path = "{Your Target Path Goes Here}" + getFileName(uri);
request(uri).pipe(fs.createWriteStream(path)).on('close', function () {
console.log("file written - " + path);
});
});
};
function getFileName (path) {
return path.replace(/^.*[\\\/]/, '');
};
function KrakenImage(fileName, height, width){
var ext = path.extname(fileName);
opts = {
file: fileName,
wait: true,
resize: {
width: width,
height: height,
strategy: 'auto'
},
"azure_store": {
"account": "{Your Account Name Goes Here}",
"key": "{Your Azure Storage Key Goes Here}",
"container": "{Enter Your Container Name Here}",
"path": "images/" + path.basename(fileName, ext) + "-" + height + "-" + width + ext
},
};
kraken.upload(opts, function (data) {
if (data.success) {
console.log('Success. Optimized image URL: %s', data.kraked_url);
console.log('Original Size: %s', data.original_size);
console.log('kraked_size: %s', data.kraked_size);
console.log('Saved Bytes: %s', data.saved_bytes);
// var s3bucket = new AWS.S3({ params: { Bucket: 'images.professionalaspnet.com' } });
download(data.kraked_url);
} else {
console.log('Fail. Error message: %s', data.error);
}
});
}
function CreateHTML(fileName, newDimensions){
}
var fileName = "{Your File Path Goes Here}",
dimensions = sizeOf(fileName),
newDimensions = [],
i = 0, resizedCount = 10, ratio = resizedCount / 100;
console.log(dimensions.width, dimensions.height);
for(; i < resizedCount; i++){
newDimensions.push(
{
"height": dimensions.width - (dimensions.width * (i * ratio)),
"width": dimensions.height - (dimensions.height * (i * ratio))
}
);
}
for(i = 0; i < resizedCount; i++){
KrakenImage(fileName, newDimensions[i].height, newDimensions[i].width);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment