Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace jimp with jimp custom #2243

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/screenshot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
savePng,
saveJpg
} from '../support/images/index.js';
import { loadCustomJimp } from './loadCustomJimp.js';

const SCREENSHOT_DIR = 'screenshots';
const log = intel.getLogger('browsertime.screenshot');

export class ScreenshotManager {
constructor(storageManager, options) {
this.storageManager = storageManager;
Expand All @@ -18,17 +21,12 @@ export class ScreenshotManager {
}

async save(name, data, url, index) {
let jimp;
try {
jimp = await import('jimp');
} catch {
jimp = undefined;
}
const jimp = await loadCustomJimp();

if (!jimp) {
if (this.config.type === 'jpg') {
log.info(
'Missing sharp dependency so you can only save images as png at viewport size'
'Missing jimp dependency so you can only save images as png at viewport size'
);
}
const pathAndName = await savePngWithoutResize(
Expand Down
17 changes: 17 additions & 0 deletions lib/screenshot/loadCustomJimp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export async function loadCustomJimp() {
try {
const { default: configure } = await import('@jimp/custom');
const { default: pluginPng } = await import('@jimp/png');
const { default: pluginJpeg } = await import('@jimp/jpeg');
const { default: pluginScale } = await import('@jimp/plugin-scale');
// The scale plugin use resize
const { default: pluginResize } = await import('@jimp/plugin-resize');
const jimp = configure({
types: [pluginPng, pluginJpeg],
plugins: [pluginResize, pluginScale]
});
return jimp;
} catch {
return;
}
}
25 changes: 8 additions & 17 deletions lib/support/images/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { pathToFolder } from '../pathToFolder.js';

import { loadCustomJimp } from '../../screenshot/loadCustomJimp.js';
export async function savePngWithoutResize(
name,
data,
Expand All @@ -24,18 +24,13 @@ export async function savePng(
dir,
options
) {
let jimp;
try {
jimp = await import('jimp');
} catch {
jimp = undefined;
}
const jimp = await loadCustomJimp();

if (jimp) {
const image = await jimp.default.read(data);
const image = await jimp.read(data);
const buffer = await image
.deflateLevel(config.png.compressionLevel)
.scaleToFit(config.maxSize, config.maxSize, jimp.default.RESIZE_HERMITE)
.scaleToFit(config.maxSize, config.maxSize, jimp.RESIZE_HERMITE)
.getBufferAsync('image/png');

return storageManager.writeData(
Expand All @@ -54,18 +49,14 @@ export async function saveJpg(
dir,
options
) {
let jimp;
try {
jimp = await import('jimp');
} catch {
jimp = undefined;
}
const jimp = await loadCustomJimp();

if (jimp) {
const image = await jimp.default.read(data);
const image = await jimp.read(data);
// https://github.com/sitespeedio/sitespeed.io/issues/3922
const buffer = await image
.quality(config.jpg.quality)
.scaleToFit(config.maxSize, config.maxSize, jimp.default.RESIZE_HERMITE)
.scaleToFit(config.maxSize, config.maxSize, jimp.RESIZE_HERMITE)
.getBufferAsync('image/jpeg');
return storageManager.writeData(
`${name}.jpg`,
Expand Down
Loading
Loading