Created
October 24, 2015 23:05
-
-
Save davidepedranz/2776473e803ac4230a74 to your computer and use it in GitHub Desktop.
Rename + resize photos with Gulp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var gulp = require('gulp'); | |
var del = require('del'); | |
var sort = require('gulp-sort'); | |
var rename = require('gulp-rename'); | |
var debug = require('gulp-debug'); | |
var gm = require('gulp-gm'); | |
var imagemin = require('gulp-imagemin'); | |
var jpegtran = require('imagemin-jpegtran'); | |
// constants | |
var ORIGINAL = './original/'; | |
var RENAMED = './renamed/'; | |
var RESIZED = './resized/'; | |
var SCALE = 0.65; | |
function pad(num) { | |
var s = "000" + num; | |
return s.substr(s.length - 3); | |
} | |
function myRenameFunction() { | |
var i, lastDir; | |
return function (path) { | |
// select the right number | |
i = lastDir !== path.dirname ? 1 : i+1; | |
lastDir = path.dirname; | |
// rename | |
path.basename = path.dirname + '-' + pad(i); | |
path.extname = path.extname.toLowerCase(); | |
return path; | |
}; | |
} | |
gulp.task('clean-renamed', function() { | |
return del(RENAMED); | |
}); | |
gulp.task('clean-resized', function() { | |
return del(RENAMED); | |
}); | |
gulp.task('rename', ['clean-renamed'], function() { | |
return gulp.src(path.join(ORIGINAL, '*', '*')) | |
.pipe(sort({ asc: true })) | |
.pipe(debug({title: 'renaming:'})) | |
.pipe(rename(myRenameFunction())) | |
.pipe(debug({title: 'in:'})) | |
.pipe(gulp.dest(RENAMED)); | |
}); | |
gulp.task('resize', ['rename', 'clean-resized'], function() { | |
return gulp.src(path.join(RENAMED, '*', '*')) | |
.pipe(debug({title: 'rezizing:'})) | |
.pipe(gm(function (gmfile, done) { | |
gmfile.size(function (err, size) { | |
done(null, gmfile.resize( | |
size.width * SCALE, | |
size.height * SCALE | |
)); | |
}); | |
})) | |
.pipe(gm(function (gmfile) { | |
return gmfile.autoOrient(); | |
})) | |
.pipe(imagemin({ | |
progressive: true, | |
use: [jpegtran()] | |
})) | |
.pipe(gm(function (gmfile) { | |
return gmfile.label('Ciao'); | |
})) | |
.pipe(gulp.dest(RESIZED)); | |
}); | |
gulp.task('default', ['resize']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "rename-resize-photos", | |
"version": "0.1.0", | |
"description": "Resize + rename photos with Gulp.js", | |
"author": "Davide Pedranz", | |
"license": "MIT", | |
"devDependencies": { | |
"del": "^2.0.2", | |
"gulp": "^3.9.0", | |
"gulp-debug": "^2.1.2", | |
"gulp-gm": "0.0.8", | |
"gulp-imagemin": "^2.3.0", | |
"gulp-rename": "^1.2.2", | |
"gulp-sort": "^1.1.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB: install
graphicsmagick
andimagemagick
https://www.npmjs.com/package/gulp-gm