How to use imagemin - 10 common examples

To help you get started, we’ve selected a few imagemin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github itgalaxy / imagemin-power-cli / cli.js View on Github external
try {
            // eslint-disable-next-line global-require, import/no-dynamic-require
            config = require(path.resolve(dataSource));
        } catch (error) {
            console.error(`Cannot require "config"\n${error}`); // eslint-disable-line no-console
            process.exit(1); // eslint-disable-line no-process-exit
        }

        opts.plugin = config.plugins;
    } else {
        opts.plugin = requirePlugins(arrify(opts.plugin));
    }

    if (Buffer.isBuffer(input)) {
        return imagemin
            .buffer(input, {
                plugins: opts.plugin
            })
            .then(buf => process.stdout.write(buf));
    }

    let spinner = null;

    if (opts.verbose || opts.silent) {
        spinner = ora();

        if (opts.verbose) {
            spinner.text = "Starting minifying images...";
            spinner.start();
        }
    }
github Kurtz1993 / ionic-minify / after_prepare / ionic-minify.js View on Github external
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
                interlaced: true
            }));
            break;
        case '.png':
            console.log('Minifying PNG File: ' + file);
            // OptiPNG options https://github.com/imagemin/imagemin#optipng
            imagemin.src(file).dest(file).use(ImageMin.optipng({
                optimizationLevel: 2
            }));
            break;
        case '.jpg':
        case '.jpeg':
            console.log('Minifying JPEG File: ' + file);
            // jpegTran options https://github.com/imagemin/imagemin#jpegtran
            imagemin.src(file).dest(file).use(ImageMin.jpegtran({
                progressive: true
            }));
            console.log('Minifying JPEG File: ' + file);
            break;
        default:
            console.log('Encountered file with ' + ext + ' extension - not compressing.');
            break;
    }
}
github Kurtz1993 / ionic-minify / after_prepare / ionic-minify.js View on Github external
case '.css':
            console.log('Minifying CSS File: ' + file);
            var source = fs.readFileSync(file, 'utf8');
            var result = cssMinifier.minify(source);
            fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
            break;
        // Image options https://github.com/imagemin/imagemin
        case '.svg':
            console.log('Minifying SVG File: ' + file);
            // svgGo options https://github.com/imagemin/imagemin#svgo
            imagemin.src(file).dest(file).use(ImageMin.svgo());
            break;
        case '.gif':
            console.log('Minifying GIF File: ' + file);
            // GifSicle options https://github.com/imagemin/imagemin#gifsicle
            imagemin.src(file).dest(file).use(ImageMin.gifsicle({
                interlaced: true
            }));
            break;
        case '.png':
            console.log('Minifying PNG File: ' + file);
            // OptiPNG options https://github.com/imagemin/imagemin#optipng
            imagemin.src(file).dest(file).use(ImageMin.optipng({
                optimizationLevel: 2
            }));
            break;
        case '.jpg':
        case '.jpeg':
            console.log('Minifying JPEG File: ' + file);
            // jpegTran options https://github.com/imagemin/imagemin#jpegtran
            imagemin.src(file).dest(file).use(ImageMin.jpegtran({
                progressive: true
github Kurtz1993 / ionic-minify / after_prepare / ionic-minify.js View on Github external
case '.svg':
            console.log('Minifying SVG File: ' + file);
            // svgGo options https://github.com/imagemin/imagemin#svgo
            imagemin.src(file).dest(file).use(ImageMin.svgo());
            break;
        case '.gif':
            console.log('Minifying GIF File: ' + file);
            // GifSicle options https://github.com/imagemin/imagemin#gifsicle
            imagemin.src(file).dest(file).use(ImageMin.gifsicle({
                interlaced: true
            }));
            break;
        case '.png':
            console.log('Minifying PNG File: ' + file);
            // OptiPNG options https://github.com/imagemin/imagemin#optipng
            imagemin.src(file).dest(file).use(ImageMin.optipng({
                optimizationLevel: 2
            }));
            break;
        case '.jpg':
        case '.jpeg':
            console.log('Minifying JPEG File: ' + file);
            // jpegTran options https://github.com/imagemin/imagemin#jpegtran
            imagemin.src(file).dest(file).use(ImageMin.jpegtran({
                progressive: true
            }));
            console.log('Minifying JPEG File: ' + file);
            break;
        default:
            console.log('Encountered file with ' + ext + ' extension - not compressing.');
            break;
    }
github Kurtz1993 / ionic-minify / after_prepare / ionic-minify.js View on Github external
},
                fromString: true
            });
            fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
            break;
        case '.css':
            console.log('Minifying CSS File: ' + file);
            var source = fs.readFileSync(file, 'utf8');
            var result = cssMinifier.minify(source);
            fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
            break;
        // Image options https://github.com/imagemin/imagemin
        case '.svg':
            console.log('Minifying SVG File: ' + file);
            // svgGo options https://github.com/imagemin/imagemin#svgo
            imagemin.src(file).dest(file).use(ImageMin.svgo());
            break;
        case '.gif':
            console.log('Minifying GIF File: ' + file);
            // GifSicle options https://github.com/imagemin/imagemin#gifsicle
            imagemin.src(file).dest(file).use(ImageMin.gifsicle({
                interlaced: true
            }));
            break;
        case '.png':
            console.log('Minifying PNG File: ' + file);
            // OptiPNG options https://github.com/imagemin/imagemin#optipng
            imagemin.src(file).dest(file).use(ImageMin.optipng({
                optimizationLevel: 2
            }));
            break;
        case '.jpg':
github tcoopman / image-webpack-loader / index.js View on Github external
// default optimizers
    if(options.gifsicle.enabled !== false)
      plugins.push(require('imagemin-gifsicle')(options.gifsicle));
    if(options.mozjpeg.enabled !== false)
      plugins.push(require('imagemin-mozjpeg')(options.mozjpeg));
    if(options.svgo.enabled !== false)
      plugins.push(require('imagemin-svgo')(options.svgo));
    if(options.pngquant.enabled !== false)
      plugins.push(require('imagemin-pngquant')(options.pngquant));
    if(options.optipng.enabled !== false)
      plugins.push(require('imagemin-optipng')(options.optipng));
    // optional optimizers
    if(options.webp)
      plugins.push(require('imagemin-webp')(options.webp));

    imagemin
      .buffer(content, {
        plugins
      })
      .then(data => {
        callback(null, data);
      })
      .catch(err => {
        callback(err);
      });
  }
};
github mamboer / hexo-filter-cleanup / lib / img.js View on Github external
}

                if (options.webp && ext === '.webp') {
                    imageminOption.plugins.push(webpmin({quality: options.webpQuality}));
                }

                // Add additional plugins.
                if (options.pngquant && ext === '.png') { // Lossy compression.
                    imageminOption.plugins.push(pngquant());
                }

                if (options.jpegrecompress && ['.jpg', '.jpeg'].indexOf(ext) !== -1) {
                    imageminOption.plugins.push(jpegrecompress({quality: options.jpegrecompressQuality}));
                }

                return imagemin.buffer(buffer, imageminOption)
                    .then(function (newBuffer) {
                        let length = buffer.length;
                        if (newBuffer && length > newBuffer.length) {
                            let saved = ((length - newBuffer.length) / length * 100).toFixed(2);
                            log.log('%s(IMG): %s [ %s saved]', pkg.name, path, saved + '%');
                            route.set(path, newBuffer); // Update the route.
                        }
                    });
            });
    });
github webframes / webframes / lib / util / minify.js View on Github external
function instantiate(type)
{
	var instance;
	
	switch(type)
	{
		case "gif":
		{
			if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
			instance = gif;
			break;
		}
		case "jpeg":
		{
			if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
			instance = jpg;
			break;
		}
		case "png":
		{
			if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) );	// 0-7
			instance = png;
			break;
		}
		default:
		{
			if (!svg)
			{
				svg = new Imagemin().use( Imagemin.svgo({plugins:[
				{
					collapseGroups: false,
github webframes / webframes / lib / util / minify.js View on Github external
function instantiate(type)
{
	var instance;
	
	switch(type)
	{
		case "gif":
		{
			if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
			instance = gif;
			break;
		}
		case "jpeg":
		{
			if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
			instance = jpg;
			break;
		}
		case "png":
		{
			if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) );	// 0-7
			instance = png;
			break;
		}
		default:
github webframes / webframes / lib / util / minify.js View on Github external
{
		case "gif":
		{
			if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
			instance = gif;
			break;
		}
		case "jpeg":
		{
			if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
			instance = jpg;
			break;
		}
		case "png":
		{
			if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) );	// 0-7
			instance = png;
			break;
		}
		default:
		{
			if (!svg)
			{
				svg = new Imagemin().use( Imagemin.svgo({plugins:[
				{
					collapseGroups: false,
					convertStyleToAttrs: false,	// didn't support base64 strings within inline CSS (svgo v0.4.4)
					moveGroupAttrsToElems: false
				}]}) );
			}
			instance = svg;
		}

imagemin

Minify images seamlessly

MIT
Latest version published 8 months ago

Package Health Score

79 / 100
Full package analysis