├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── fixture.png ├── index.js ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 4, 10 | "newcap": true, 11 | "noarg": true, 12 | "quotmark": "single", 13 | "regexp": true, 14 | "undef": true, 15 | "unused": true, 16 | "strict": true, 17 | "trailing": true, 18 | "smarttabs": true 19 | } 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | -------------------------------------------------------------------------------- /fixture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firetix/gulp-image-optimization/eaa9b8ff54cf6436326080fa2299062403767e1b/fixture.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var path = require('path'); 3 | var fs = require('graceful-fs'); 4 | var gutil = require('gulp-util'); 5 | var map = require('map-stream-limit'); 6 | var filesize = require('filesize'); 7 | var tempWrite = require('temp-write'); 8 | var imagemin = require('image-min'); 9 | 10 | module.exports = function(options) { 11 | return map(function(file, cb) { 12 | if (file.isNull()) { 13 | return cb(null, file); 14 | } 15 | 16 | if (file.isStream()) { 17 | return cb(new gutil.PluginError('gulp-image-optimization', 'Streaming not supported')); 18 | } 19 | 20 | if (['.jpg', '.JPG', '.JPEG', '.jpeg', '.png', '.PNG', '.gif', '.GIF'].indexOf(path.extname(file.path)) === -1) { 21 | gutil.log('gulp-image-optimization: Skipping unsupported image ' + gutil.colors.blue(file.relative)); 22 | return cb(null, file); 23 | } 24 | 25 | tempWrite(file.contents, path.extname(file.path), function(err, tempFile) { 26 | if (err) { 27 | return cb(new gutil.PluginError('gulp-image-optimization', err)); 28 | } 29 | 30 | // workaround: https://github.com/kevva/image-min/issues/8 31 | fs.stat(tempFile, function(err, stats) { 32 | if (err) { 33 | return cb(new gutil.PluginError('gulp-image-optimization', err)); 34 | } 35 | 36 | var origSize = stats.size; 37 | 38 | imagemin(tempFile, tempFile, options, function() { 39 | fs.readFile(tempFile, function(err, data) { 40 | if (err) { 41 | return cb(new gutil.PluginError('gulp-image-optimization', err)); 42 | } 43 | 44 | var saved = origSize - data.length; 45 | var savedMsg = saved > 0 ? 'saved ' + filesize(saved, { 46 | round: 1 47 | }) : 'already optimized'; 48 | 49 | gutil.log('gulp-image-optimization:', gutil.colors.green('✔ ') + file.relative + gutil.colors.gray(' (' + savedMsg + ')')); 50 | 51 | file.contents = data; 52 | cb(null, file); 53 | }); 54 | }); 55 | }); 56 | }); 57 | }, 10); 58 | }; 59 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-image-optimization", 3 | "version": "0.1.3", 4 | "description": "Minify PNG, JPEG and GIF images. this is based on https://github.com/sindresorhus/gulp-imagemin with stream-limit implementation", 5 | "license": "MIT", 6 | "repository": "firetix/gulp-image-optimization", 7 | "author": { 8 | "name": "Mohamed Rachidi", 9 | "email": "rachidi29@gmail.com" 10 | }, 11 | "engines": { 12 | "node": "<6.0.0" 13 | }, 14 | "scripts": { 15 | "test": "mocha" 16 | }, 17 | "files": [ 18 | "index.js" 19 | ], 20 | "keywords": [ 21 | "gulpplugin", 22 | "imagemin", 23 | "image", 24 | "img", 25 | "picture", 26 | "photo", 27 | "minify", 28 | "minifier", 29 | "compress", 30 | "png", 31 | "jpg", 32 | "jpeg", 33 | "gif" 34 | ], 35 | "dependencies": { 36 | "gulp-util": "~3.0.8", 37 | "imagemin": "~5.3.1", 38 | "graceful-fs": "~4.1.11", 39 | "filesize": "~3.5.11", 40 | "temp-write": "~3.4.0", 41 | "map-stream-limit": "~1.1.0", 42 | "jpegtran-bin": "^3.2.0" 43 | }, 44 | "devDependencies": { 45 | "mocha": "*" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # [gulp](https://github.com/wearefractal/gulp)-image-optimization 2 | 3 | > Minify PNG, JPEG and GIF images with [image-min](https://github.com/kevva/image-min) 4 | 5 | *Issues with the output should be reported on the image-min [issue tracker](https://github.com/kevva/image-min/issues).* 6 | 7 | ## Install 8 | 9 | Install with [npm](https://npmjs.org/package/gulp-image-optimization) 10 | 11 | ``` 12 | npm install --save-dev gulp-image-optimization 13 | ``` 14 | 15 | 16 | ## Example 17 | 18 | ``` 19 | var gulp = require('gulp'); 20 | var imageop = require('gulp-image-optimization'); 21 | 22 | gulp.task('images', function(cb) { 23 | gulp.src('src/**/*.+(png|jpg|gif|jpeg)').pipe(imageop({ 24 | optimizationLevel: 5, 25 | progressive: true, 26 | interlaced: true 27 | })).pipe(gulp.dest('public/images')).on('end', cb).on('error', cb); 28 | }); 29 | ``` 30 | 31 | ## API 32 | 33 | ### imageop(options) 34 | 35 | See the image-min [options](https://github.com/kevva/image-min#options). 36 | 37 | 38 | ## License 39 | 40 | MIT © [Mohamed Rachidi] 41 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs'); 3 | var assert = require('assert'); 4 | var gutil = require('gulp-util'); 5 | var imagemin = require('./index'); 6 | 7 | it('should minify images', function (cb) { 8 | var stream = imagemin(); 9 | 10 | stream.on('data', function (file) { 11 | assert(file.contents.length < fs.statSync('fixture.png').size); 12 | cb(); 13 | }); 14 | 15 | stream.write(new gutil.File({ 16 | path: __dirname + '/fixture.png', 17 | contents: fs.readFileSync('fixture.png') 18 | })); 19 | }); 20 | 21 | it('should skip unsupported images', function (cb) { 22 | var stream = imagemin(); 23 | 24 | stream.on('data', function (file) { 25 | assert.strictEqual(file.contents, null); 26 | cb(); 27 | }); 28 | 29 | stream.write(new gutil.File({ 30 | path: __dirname + '/fixture.bmp' 31 | })); 32 | }); 33 | --------------------------------------------------------------------------------