Skip to content

Commit

Permalink
Replace sharp with jimp (#325)
Browse files Browse the repository at this point in the history
* #44449: replace sharp with jimp

* #44449: update sharp download script

* #44449: add darwin-arm64 build

* Revert "#44449: add darwin-arm64 build"

This reverts commit d6bae5f.

* remove sharp scripts
  • Loading branch information
ArturWierzbicki authored Feb 23, 2022
1 parent 617be3d commit 391eec1
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 145 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
ARCH = darwin-x64-unknown
SKIP_CHROMIUM =
OUT =
SKIP_SHARP_DOWNLOAD =
DOCKER_TAG = dev

all: clean build
Expand All @@ -24,7 +23,7 @@ clean_package:
./scripts/clean_target.sh ${ARCH} ${OUT}

package:
./scripts/package_target.sh ${ARCH} ${SKIP_CHROMIUM} ${OUT} ${SKIP_SHARP_DOWNLOAD}
./scripts/package_target.sh ${ARCH} ${SKIP_CHROMIUM} ${OUT}

archive:
./scripts/archive_target.sh ${ARCH} ${OUT}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"puppeteer": "^13.1.3",
"puppeteer-cluster": "^0.22.0",
"poolpeteer": "^0.22.0",
"sharp": "0.29.3",
"jimp": "0.16.1",
"unique-filename": "^1.1.0",
"winston": "^3.2.1"
},
Expand Down
42 changes: 0 additions & 42 deletions scripts/download_sharp.js

This file was deleted.

7 changes: 0 additions & 7 deletions scripts/package_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
ARCH="${1:-}"
SKIP_CHROMIUM=${2:-false}
OUT="${3:-}"
SKIP_SHARP_DOWNLOAD=${4:-false}

if [ -z "$ARCH" ]; then
echo "ARCH (arg 1) has to be set"
exit 1
fi

if [ ${SKIP_SHARP_DOWNLOAD} = false ]; then
node scripts/download_sharp.js ${ARCH} ${OUT}
else
echo "Skipping sharp download"
fi

mkdir -p dist
node scripts/pkg.js ${ARCH} ${OUT}
if [ $? != 0 ]; then
Expand Down
2 changes: 0 additions & 2 deletions scripts/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,3 @@ const outputNodeModules = `${outputPath}/node_modules`
childProcess.execSync(`"./node_modules/.bin/pkg" -t node14-${platform}-${arch} . --out-path ${outputPath} --no-native-build`, {stdio: 'inherit'});

childProcess.execSync(`rm -rf ${outputNodeModules}`)
fs.mkdirSync(`${outputNodeModules}/sharp`, {recursive: true})
childProcess.execSync(`cp -RP ./node_modules/sharp/build ${outputNodeModules}/sharp && cp -RP ./node_modules/sharp/vendor ${outputNodeModules}/sharp`)
23 changes: 13 additions & 10 deletions src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as chokidar from 'chokidar';
import * as path from 'path';
import * as fs from 'fs';
import * as promClient from 'prom-client';
import * as sharp from 'sharp';
import * as Jimp from 'jimp';
import { Logger } from '../logger';
import { RenderingConfig } from '../config';
import { ImageRenderOptions, RenderOptions } from '../types';
Expand Down Expand Up @@ -360,15 +360,18 @@ export class Browser {
const scaled = `${options.filePath}_${Date.now()}_scaled.png`;
const w = +options.width / options.scaleImage;
const h = +options.height / options.scaleImage;
await sharp(options.filePath)
.resize(w, h, { fit: 'inside' })
// .toFormat('webp', {
// quality: 70, // 80 is default
// })
.toFile(scaled);

// overwrite the original image with the scaled value
fs.renameSync(scaled, options.filePath);

await this.withTimingMetrics(async () => {
const file = await Jimp.read(options.filePath);
await file
.resize(w, h)
// .toFormat('webp', {
// quality: 70, // 80 is default
// })
.writeAsync(scaled);

fs.renameSync(scaled, options.filePath);
}, 'imageResize');
}

return { filePath: options.filePath };
Expand Down
Loading

0 comments on commit 391eec1

Please sign in to comment.