Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Moebits committed Sep 13, 2024
1 parent 2867693 commit 48e14f8
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 22 deletions.
Binary file modified .DS_Store
Binary file not shown.
26 changes: 13 additions & 13 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const removeDoubles = async (images: string[], dontProcessAll?: boolean) => {
let widthMap = {} as any

for (let i = 0; i < images.length; i++) {
const metadata = await sharp(images[i]).metadata()
const metadata = await sharp(images[i], {limitInputPixels: false}).metadata()
const width = metadata.width || 0
if (widthMap[width]) {
widthMap[width] += 1
Expand All @@ -76,7 +76,7 @@ const removeDoubles = async (images: string[], dontProcessAll?: boolean) => {
}

for (let i = 0; i < images.length; i++) {
const metadata = await sharp(images[i]).metadata()
const metadata = await sharp(images[i], {limitInputPixels: false}).metadata()
const width = metadata.width || 0
if (width > commonWidth * 1.5) {
doubleImages.push(images[i])
Expand All @@ -89,16 +89,16 @@ const removeDoubles = async (images: string[], dontProcessAll?: boolean) => {
}

for (let i = 0; i < doubleImages.length; i++) {
const metadata = await sharp(doubleImages[i]).metadata()
const metadata = await sharp(doubleImages[i], {limitInputPixels: false}).metadata()
const width = metadata.width || 0
const height = metadata.height || 0
const newWidth = Math.floor(width / 2)
const page1 = `${path.dirname(doubleImages[i])}/${path.basename(doubleImages[i], path.extname(doubleImages[i]))}.1${path.extname(doubleImages[i])}`
const page2 = `${path.dirname(doubleImages[i])}/${path.basename(doubleImages[i], path.extname(doubleImages[i]))}.2${path.extname(doubleImages[i])}`
await sharp(doubleImages[i])
await sharp(doubleImages[i], {limitInputPixels: false})
.extract({left: newWidth, top: 0, width: newWidth, height: height})
.toFile(page1)
await sharp(doubleImages[i])
await sharp(doubleImages[i], {limitInputPixels: false})
.extract({left: 0, top: 0, width: newWidth, height: height})
.toFile(page2)
}
Expand Down Expand Up @@ -701,14 +701,14 @@ const compress = async (info: any) => {
const {frameArray, delayArray} = await functions.getGIFFrames(info.source)
const newFrameArray = [] as Buffer[]
for (let i = 0; i < frameArray.length; i++) {
const newFrame = await sharp(frameArray[i])
const newFrame = await sharp(frameArray[i], {limitInputPixels: false})
.resize(width, height, {fit: "fill"})
.toBuffer()
newFrameArray.push(newFrame)
}
buffer = await functions.encodeGIF(newFrameArray, delayArray, width, height)
} else {
buffer = await sharp(buffer, {animated: true}).resize(width, height, {fit: "fill"}).gif().toBuffer()
buffer = await sharp(buffer, {animated: true, limitInputPixels: false}).resize(width, height, {fit: "fill"}).gif().toBuffer()
}
if (options.quality !== 100) {
buffer = await imagemin.buffer(buffer, {plugins: [
Expand All @@ -724,10 +724,10 @@ const compress = async (info: any) => {
}
} else {
if (resizeCondition) {
buffer = await sharp(buffer, {animated: true}).resize(width, height, {fit: "fill"}).toBuffer()
buffer = await sharp(buffer, {animated: true, limitInputPixels: false}).resize(width, height, {fit: "fill"}).toBuffer()
}
if (sourceExt !== ext) {
let s = sharp(buffer, {animated: true})
let s = sharp(buffer, {animated: true, limitInputPixels: false})
if (ext === "jpg" || ext === "jpeg") s.jpeg({optimiseScans: options.progressive, quality: options.quality})
if (ext === "png") s.png({quality: options.quality})
if (ext === "webp") s.webp({quality: options.quality})
Expand Down Expand Up @@ -819,14 +819,14 @@ ipcMain.handle("compress-realtime", async (event, info: any) => {
const {frameArray, delayArray} = await functions.getGIFFrames(info.source)
const newFrameArray = [] as Buffer[]
for (let i = 0; i < frameArray.length; i++) {
const newFrame = await sharp(frameArray[i])
const newFrame = await sharp(frameArray[i], {limitInputPixels: false})
.resize(width, height, {fit: "fill"})
.toBuffer()
newFrameArray.push(newFrame)
}
buffer = await functions.encodeGIF(newFrameArray, delayArray, width, height)
} else {
buffer = await sharp(buffer, {animated: true}).resize(width, height, {fit: "fill"}).gif().toBuffer()
buffer = await sharp(buffer, {animated: true, limitInputPixels: false}).resize(width, height, {fit: "fill"}).gif().toBuffer()
}
if (options.quality !== 100) {
buffer = await imagemin.buffer(buffer, {plugins: [
Expand All @@ -842,10 +842,10 @@ ipcMain.handle("compress-realtime", async (event, info: any) => {
}
} else {
if (resizeCondition) {
buffer = await sharp(buffer, {animated: true}).resize(width, height, {fit: "fill"}).toBuffer()
buffer = await sharp(buffer, {animated: true, limitInputPixels: false}).resize(width, height, {fit: "fill"}).toBuffer()
}
if (sourceExt !== ext) {
let s = sharp(buffer, {animated: true})
let s = sharp(buffer, {animated: true, limitInputPixels: false})
if (ext === "jpg" || ext === "jpeg") s.jpeg({optimiseScans: options.progressive, quality: options.quality})
if (ext === "png") s.png({quality: options.quality})
if (ext === "webp") s.webp({quality: options.quality})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "image-compressor",
"version": "0.2.1",
"version": "0.2.2",
"description": "Compresses, resizes, and renames images.",
"main": "dist/main.js",
"scripts": {
"start": "npm run clean && webpack && cross-env DEVELOPMENT=true electron dist/main.js",
"clean": "del-cli ./dist",
"build": "npm run clean ./build && webpack && npm run sharp:mac && cross-env CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder -m -p never",
"build": "npm run clean ./build && webpack && cross-env CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder -m -p never",
"rebuild": "npm rebuild --runtime=electron --target=14.0.0 --disturl=https://atom.io/download/atom-shell --abi=83",
"release": "npm run release:mac && npm run release:win",
"release:mac": "npm run clean ./build && webpack && npm run sharp:mac && cross-env CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder -m -p always",
Expand Down Expand Up @@ -46,11 +46,23 @@
"extraFiles": [
"poppler/mac/**"
],
"files": [
"vendor/cjpeg",
"vendor/cwebp",
"vendor/gifsicle",
"vendor/pngquant"
"extraResources": [
{
"from": "vendor/cjpeg",
"to": "app/vendor/cjpeg"
},
{
"from": "vendor/cwebp",
"to": "app/vendor/cwebp"
},
{
"from": "vendor/gifsicle",
"to": "app/vendor/gifsicle"
},
{
"from": "vendor/pngquant",
"to": "app/vendor/pngquant"
}
]
},
"dmg": {
Expand Down
Binary file modified patch/.DS_Store
Binary file not shown.
Binary file modified patch/cwebp-bin/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions patch/cwebp-bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
import process from 'node:process';
import {spawn} from 'node:child_process';
import binPath from './index.js';

spawn(binPath, process.argv.slice(2), {stdio: 'inherit'})
.on('exit', process.exit);
3 changes: 3 additions & 0 deletions patch/cwebp-bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import lib from './lib/index.js';

export default lib.path();
28 changes: 28 additions & 0 deletions patch/cwebp-bin/lib/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

bin.run(['-version']).then(() => {
console.log('cwebp pre-build test passed successfully');
}).catch(error => {
console.warn(error.message);
console.warn('cwebp pre-build test failed');
console.info('compiling from source');

try {
const source = fileURLToPath(new URL('../vendor/source/libwebp-1.2.1.tar.gz', import.meta.url));

binBuild.file(source, [
`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
'make && make install',
]);

console.log('cwebp built successfully');
} catch (error) {
console.error(error.stack);

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
});
9 changes: 9 additions & 0 deletions patch/cwebp-bin/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Imagemin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions patch/cwebp-bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "cwebp-bin",
"version": "8.0.0",
"description": "cwebp wrapper that makes it seamlessly available as a local dependency",
"license": "MIT",
"repository": "imagemin/cwebp-bin",
"type": "module",
"exports": "./index.js",
"funding": "https://github.com/imagemin/cwebp-bin?sponsor=1",
"bin": {
"cwebp": "cli.js"
},
"engines": {
"node": "^14.13.1 || >=16.0.0"
},
"scripts": {
"postinstall": "node lib/install.js",
"test": "xo && ava --timeout=180s"
},
"files": [
"cli.js",
"index.js",
"lib",
"vendor/source"
],
"keywords": [
"imagemin",
"compress",
"image",
"img",
"jpeg",
"jpg",
"minify",
"optimize",
"png",
"webp"
],
"dependencies": {
"bin-build": "^3.0.0",
"bin-wrapper": "^4.0.1"
},
"devDependencies": {
"ava": "^4.2.0",
"bin-check": "^4.1.0",
"compare-size": "^3.0.0",
"execa": "^6.1.0",
"tempy": "^3.0.0",
"xo": "^0.48.0"
}
}
44 changes: 44 additions & 0 deletions patch/cwebp-bin/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# cwebp-bin ![GitHub Actions Status](https://github.com/imagemin/cwebp-bin/workflows/test/badge.svg?branch=main)

> [WebP](https://developers.google.com/speed/webp/) is a new image format that provides lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller in size compared to JPEG images at equivalent SSIM index.
You probably want [`imagemin-webp`](https://github.com/imagemin/imagemin-webp) instead.


## Install

```
$ npm install cwebp-bin
```


## Usage

```js
import {execFile} from 'node:child_process';
import cwebp from 'cwebp-bin';

execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
if (err) {
throw err;
}

console.log('Image is converted!');
});
```


## CLI

```
$ npm install --global cwebp-bin
```

```
$ cwebp --help
```


## License

MIT © [Imagemin](https://github.com/imagemin)
Binary file added patch/cwebp-bin/vendor/cwebp
Binary file not shown.
Binary file not shown.
Binary file modified patch/mozjpeg/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions patch/mozjpeg/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node
import {spawn} from 'node:child_process';
import process from 'node:process';
import mozjpeg from './index.js';

const input = process.argv.slice(2);

spawn(mozjpeg, input, {stdio: 'inherit'})
.on('exit', process.exit);
3 changes: 3 additions & 0 deletions patch/mozjpeg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import lib from './lib/index.js';

export default lib.path();
42 changes: 42 additions & 0 deletions patch/mozjpeg/lib/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os from 'node:os';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

const cpuNumber = Math.max(os.cpus().length, 1);

bin.run(['-version']).then(() => {
console.log('mozjpeg pre-build test passed successfully');
}).catch(async error => {
console.warn(error.message);
console.warn('mozjpeg pre-build test failed');
console.info('compiling from source');

let cfgExtras = '';
if (process.platform === 'darwin') {
cfgExtras = 'libpng_LIBS=\'/usr/local/lib/libpng16.a -lz\' --enable-static';
}

const cfg = [
`./configure --enable-static --disable-shared --disable-dependency-tracking --with-jpeg8 ${cfgExtras}`,
`--prefix="${bin.dest()}" --bindir="${bin.dest()}" --libdir="${bin.dest()}"`,
].join(' ');

try {
const source = fileURLToPath(new URL('../vendor/source/mozjpeg.tar.gz', import.meta.url));
await binBuild.file(source, [
'autoreconf -fiv',
cfg,
`make -j${cpuNumber}`,
`make install -j${cpuNumber}`,
]);

console.log('mozjpeg built successfully');
} catch (error) {
console.error(error.stack);

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
});
9 changes: 9 additions & 0 deletions patch/mozjpeg/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Imagemin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 48e14f8

Please sign in to comment.