npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zebrajaeger/gulp-scale-images

v0.0.3

Published

Gulp plugin to resize each image into multiple smaller variants.

Downloads

3

Readme

gulp-scale-images

Gulp plugin to make each image smaller. Combined with flat-map, you can create multiple variantes per image, which is useful for responsive images.

ISC-licensed

Motivation

This is 'heavily inspired' by gulp-scale-images (https://www.npmjs.com/package/gulp-scale-images). And the lib does a great job.

So why fork this?

On my Orange and Raspberry Pi, the Sharp-Code node crashes because of memory errors and i did not found a workaround. So i decided to take the plugin and separate the parts (Gulp plugin and resizer).

Now there is a unique interface and you can decide to use sharp or jimp as resizing engine.

Installing

npm install @zebrajaeger/gulp-scale-images @zebrajaeger/gulp-scale-images-resize-sharp --save-dev

or

npm install @zebrajaeger/gulp-scale-images @zebrajaeger/gulp-scale-images-resize-jimp --save-dev

Usage

gulp-scale-images expects the instructions for each file to be in file.scale. They may look like this:

{
	maxWidth: 300, // optional maximum width, respecting the aspect ratio
	maxHeight: 400, // optional maximum height, respecting the aspect ratio
	format: 'jpeg', // optional, one of ('jpeg', 'png', 'webp')
	quality: 80, // optional and only for jpeg target format
	withoutEnlargement: false // optional, default is true
}

Note: You must specify at least one of maxWidth and maxHeight.

An example, we're going to generate two smaller variants for each input file. We're going to use flat-map for this:

const gulp = require('gulp')
const flatMap = require('flat-map').default
const scaleImages = require('@zebrajaeger/gulp-scale-images')

// choose one of them
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-sharp')
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-jimp')

const twoVariantsPerFile = (file, cb) => {
	const pngFile = file.clone()
	pngFile.scale = {maxWidth: 500, maxHeight: 500, format: 'png'}
	const jpegFile = file.clone()
	jpegFile.scale = {maxWidth: 700, format: 'jpeg'}
	cb(null, [pngFile, jpegFile])
}

gulp.src('src/*.{jpeg,jpg,png,gif}')
.pipe(flatMap(twoVariantsPerFile))
.pipe(scaleImages(scaleImagesResize))
.pipe(gulp.dest(…))

Definining scale instructions based on metadata

You can let gulp-scale-images read the image metadata first, to device what to do with the file:

const readMetadata = require('gulp-scale-images/read-metadata')
const through = require('through2')
const scaleImages = require('@zebrajaeger/gulp-scale-images')

// choose one of them
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-sharp')
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-jimp')

const computeScaleInstructions = (file, _, cb) => {
	readMetadata(file.path, (err, meta) => {
		if (err) return cb(err)
		file.scale = {
			maxWidth: Math.floor(meta.width / 2),
			maxHeight: Math.floor(meta.height / 2)
		}
		cb(null, file)
	})
}

gulp.src(…)
.pipe(through.obj(computeScaleInstructions))
.pipe(scaleImages(scaleImagesResize))
.pipe(gulp.dest(…))

Custom output file names

By default, gulp-scale-images will use {basename}.{maxWidth}w-{maxHeight}h.{format} (e.g. foo.500w-300h.jpeg). You can define a custom logic though:

const path = require('path')
const scaleImages = require('@zebrajaeger/gulp-scale-images')

// choose one of them
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-sharp')
const scaleImagesResize = require('@zebrajaeger/gulp-scale-images-resize-jimp')

const computeFileName = (output, scale, cb) => {
	const fileName = [
		path.basename(output.path, output.extname), // strip extension
		scale.maxWidth + 'w',
		scale.format || output.extname
	].join('.')
	cb(null, fileName)
}

gulp.src(…)
.pipe(through.obj(computeScaleInstructions))
.pipe(scaleImages(scaleImagesResize, computeFileName)) // not that we pass computeFileName here
.pipe(gulp.dest(…))

gulp-scale-images works well with

  • flat-map – A flat map implementation for node streams. (One chunk in, n chunks out.)
  • replace-ext – Replaces a file extension with another one.

Contributing

If you have a question or have difficulties using gulp-scale-images, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.