Git Product home page Git Product logo

imagemin-optipng's Introduction

imagemin

Minify images seamlessly





Install

npm install imagemin

Usage

import imagemin from 'imagemin';
import imageminJpegtran from 'imagemin-jpegtran';
import imageminPngquant from 'imagemin-pngquant';

const files = await imagemin(['images/*.{jpg,png}'], {
	destination: 'build/images',
	plugins: [
		imageminJpegtran(),
		imageminPngquant({
			quality: [0.6, 0.8]
		})
	]
});

console.log(files);
//=> [{data: <Uint8Array 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]

API

imagemin(input, options?)

Returns Promise<object[]> in the format {data: Uint8Array, sourcePath: string, destinationPath: string}.

input

Type: string[]

File paths or glob patterns.

options

Type: object

destination

Type: string

Set the destination folder to where your files will be written. If no destination is specified, no files will be written.

plugins

Type: Array

The plugins to use.

glob

Type: boolean
Default: true

Enable globbing when matching file paths.

imagemin.buffer(data, options?)

Returns Promise<Uint8Array>.

data

Type: Uint8Array

The image data to optimize.

options

Type: object

plugins

Type: Array

Plugins to use.

Related

imagemin-optipng's People

Contributors

1000ch avatar jdalton avatar kevva avatar luckyraul avatar nilscox avatar ojthek avatar shinnn avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imagemin-optipng's Issues

Installation error

I'm getting this when I install on a Windows 7 32 bit machine:

node lib/install.js

‼ spawn ENOENT
‼ optipng pre-build test failed
i compiling from source
× Error: Command failed: '.' is not recognized as an internal or external
and,
operable program or batch file.

at ChildProcess.exithandler (child_process.js:648:15)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket.<anonymous> (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)

[email protected] node_modules\imagemin-optipng
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected])

Cheers,
Dave

Using false for bitDepthReduction, colorTypeReduction, or paletteReduction causes error

Using false for bitDepthReduction, colorTypeReduction, or paletteReduction causes an error:

** Error: The option -out requires one input file

I'm guessing execBuffer.input should be last.

Moving the execBuffer.input lower or a combination of them lower doesn't help.
I tried manually executing optipng with the settings and it executed without error. Maybe it's something funky with the temp files used as input/output destinations?

Using with gulp

Is the support for gulp removed now.?

Counldn't find the description in README anymore.!

Unable to run demo using nodejs

have a js file (demo.js) :

const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');

(async () => {
	await imagemin(['img/*.png'], 'build/img', {
		use: [
			imageminOptipng()
		]
	});

	console.log('Images optimized!');
})();

and package.json :

{
  "name": "imagemin",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
    "imagemin": "^8.0.0",
    "imagemin-jpegtran": "^7.0.0",
    "imagemin-optipng": "^8.0.0",
    "imagemin-pngquant": "^9.0.2",
    "jpegtran-bin": "^5.0.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

run demo.js :

node demo.js

result :

C02F93TLMD6M:imagemin PLQin$ node demo.js 
internal/modules/cjs/loader.js:1080
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/PLQin/Desktop/AtHome/demo/imagemin/node_modules/imagemin/index.js
require() of ES modules is not supported.
require() of /Users/PLQin/Desktop/AtHome/demo/imagemin/node_modules/imagemin/index.js from /Users/PLQin/Desktop/AtHome/demo/imagemin/demo.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/PLQin/Desktop/AtHome/demo/imagemin/node_modules/imagemin/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/PLQin/Desktop/AtHome/demo/imagemin/demo.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
  code: 'ERR_REQUIRE_ESM'
}

What should I do? I just want to use nodejs to compress my pictures (all the pictures in the directory).

Is there any other tool to recommend?

On Freebsd 10 (x64), we cannot use the optipng in the vendor folder

I'm getting a "ELF interpreter /libexec/ld-elf.so.1 not found" when I try to minify images using optipng since the changes on the 4.3.0 that occured around the 06/06/2015. -> We added the 32-bit compatibility on our Freebsd10 systems, and it works now.

However, we installed a custom optipng on our system. Is it possible to tell the plugin to use the system-wide optipng instead of the one shipped in the vendor folder?

optimizationLevel 0 Error in file

Hi I am testing and comparing all plugins and I am wondering if this is the correct behavior.

From the readme

optimizationLevel
Type: number
Default: 3
Select an optimization level between 0 and 7.

with optimizationLevel: 0 I got the following error:

{ Error: Error in file: images/imagemin.png

** Processing: /var/folders/vc/g0g62cj15vg3vwyn3pdbp6bc0000gp/T/cce3c2b8-f207-44d9-8190-b364455560ed
Warning: Extraneous data found after IEND
400x400 pixels, 4x8 bits/pixel, RGB+alpha
Recoverable errors found in input. Fixing...
IDAT recoding is necessary, but is disabled by the user.
Error: Can't continue

** Status report
1 file(s) have been processed.
1 error(s) have been encountered.

    at ChildProcess.exithandler (child_process.js:202:12)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:850:16)
    at Socket.<anonymous> (internal/child_process.js:323:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:492:12)
  killed: false,
  code: 1,
  signal: null,
  cmd: '/Users/me/project/node_modules/optipng-bin/vendor/optipng -strip all -clobber -force -fix -o 0 -out /var/folders/vc/g0g62cj15vg3vwyn3pdbp6bc0000gp/T/401ab274-d280-49f7-b6f3-2f3bd0d558bb /var/folders/vc/g0g62cj15vg3vwyn3pdbp6bc0000gp/T/cce3c2b8-f207-44d9-8190-b364455560ed',
  stdout: '',
  stderr: '** Processing: /var/folders/vc/g0g62cj15vg3vwyn3pdbp6bc0000gp/T/cce3c2b8-f207-44d9-8190-b364455560ed\nWarning: Extraneous data found after IEND\n400x400 pixels, 4x8 bits/pixel, RGB+alpha\nRecoverable errors found in input. Fixing...\nIDAT recoding is necessary, but is disabled by the user.\nError: Can\'t continue\n\n** Status report\n1 file(s) have been processed.\n1 error(s) have been encountered.\n' }

test

const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');

imagemin(['images/*.png'], 'build', {
  use: [imageminOptipng({
    optimizationLevel: 0
  })]
})
.then(() => {
  console.log('Images optimized');
})
.catch(err => {
  console.log(err)
});

issues on Ubuntu

having

$ optipng -v
OptiPNG version 0.7.4
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 13.10
Release:        13.10
Codename:       saucy
$ node -v
v0.10.28

npm test fails like this

selection_094

Single PNG file always causes decoder error in IE11

I have lots of PNG files that work as expected. All processed in the exact way and settings from Photoshop. However just 1 file causes a decoder error in IE11.

PLEASE NOTE: The following images are white on transparent, so not visible here with white background.

Original file. RGB, exported from photoshop.
logo_normal

Optimized output that causes error
logo_normal

none

发错了。。

Fails to build on Apple Silicon devices (Arm64)

On an Apple Silicon Mac:

$ npm install imagemin-optipng --save-dev

...

Undefined symbols for architecture arm64:
npm ERR! "_png_init_filter_functions_neon", referenced from:
npm ERR! _png_read_filter_row in libpng.a(pngrutil.o)
npm ERR! ld: symbol(s) not found for architecture arm64

Provide env var for passing optipng binary path?

I'm on nixos, where it looks like the regular linux binary from optipng-bin fails to execute. For reference, here's how the binary is compiled on nixos: https://github.com/NixOS/nixpkgs/blob/nixos-20.09/pkgs/tools/graphics/optipng/default.nix

I see that this package (in my case) downloads a precompiled binary using optipng-bin and returns the path to the binary:

return execBuffer({
input: buffer,
bin: optipng,
args: arguments_
});

Are you ok with allowing users to specify an override of this path? I'm imagining using it something like this: OPTIPNG_EXECUTABLE_PATH=path/to/optipng yarn start.

Happy to provide the PR if you approve of this strategy!

Issue with optipng-bin

I have issue in Travis build:

Error: spawn /app/node_modules/optipng-bin/vendor/optipng ENOENT
at exports._errnoException (util.js:1036:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
The command '/bin/sh -c gulp' returned a non-zero code: 1

I'm not sure, if it helps, but can you make release with latest changes, where optipng-bin has version 4.0.0. Thank you

Compilation fails

We are using gulp-imagemin but this issue is related exactly to jpegtran. We have a build system running in docker container and base on node:alpine, recently it started to fail. The minimal case to replicate the problem.

  1. docker run -it node:alpine /bin/sh
    Install some build dependencies
  2. apk add --update --no-cache make g++ automake autoconf libtool nasm libpng-dev
  3. npm install optipng-bin

We get following output that is worth mentioning

> [email protected] postinstall /node_modules/optipng-bin
> node lib/install.js

  ⚠ spawn /node_modules/optipng-bin/vendor/optipng ENOENT
  ⚠ optipng pre-build test failed
  ℹ compiling from source
  ✖ Error: ./configure --with-system-zlib --prefix="/node_modules/optipng-bin/vendor" --bindir="/node_modules/optipng-bin/vendor" && make install
Command failed: ./configure --with-system-zlib --prefix="/node_modules/optipng-bin/vendor" --bindir="/node_modules/optipng-bin/vendor"
/bin/sh: ./configure: not found

    at ChildProcess.exithandler (child_process.js:270:12)
    at emitTwo (events.js:125:13)
    at ChildProcess.emit (events.js:213:7)
    at maybeClose (internal/child_process.js:927:16)
    at Socket.stream.socket.on (internal/child_process.js:348:11)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at Pipe._handle.close [as _onclose] (net.js:545:12)

Previously it was running ok on the same build system. I can't say exactly when it first started to fail as I was using cache and I have noticed the problem after cleaning the cache.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.