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

Fix: let Squoosh default image quality internally #4906

Merged
merged 3 commits into from
Sep 28, 2022
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-garlics-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Updates the default image service to use format-specific quality defaults
2 changes: 1 addition & 1 deletion packages/integrations/image/src/loaders/squoosh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SquooshService extends BaseSSRService {
inputBuffer,
operations,
transform.format,
transform.quality || 100
transform.quality
);

return {
Expand Down
8 changes: 4 additions & 4 deletions packages/integrations/image/src/vendor/squoosh/image-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function processBuffer(
buffer: Buffer,
operations: Operation[],
encoding: OutputFormat,
quality: number
quality?: number
): Promise<Uint8Array> {
// @ts-ignore
const worker = await getWorker()
Expand Down Expand Up @@ -109,14 +109,14 @@ export async function processBuffer(
return await worker.dispatchJob({
operation: 'encodeavif',
imageData,
quality: quality || 100
quality,
}) as Uint8Array;
case 'jpeg':
case 'jpg':
return await worker.dispatchJob({
operation: 'encodejpeg',
imageData,
quality: quality || 100,
quality,
}) as Uint8Array;
case 'png':
return await worker.dispatchJob({
Expand All @@ -127,7 +127,7 @@ export async function processBuffer(
return await worker.dispatchJob({
operation: 'encodewebp',
imageData,
quality: quality || 100,
quality,
}) as Uint8Array;
default:
throw Error(`Unsupported encoding format`)
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/image/src/vendor/squoosh/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function processBuffer(
buffer: Buffer,
operations: Operation[],
encoding: OutputFormat,
quality: number
quality?: number
): Promise<Uint8Array> {
let imageData = await impl.decodeBuffer(buffer)
for (const operation of operations) {
Expand All @@ -29,7 +29,7 @@ export async function processBuffer(

switch (encoding) {
case 'avif':
return await impl.encodeAvif(imageData, { quality: quality }) as Uint8Array;
return await impl.encodeAvif(imageData, { quality }) as Uint8Array;
case 'jpeg':
case 'jpg':
return await impl.encodeJpeg(imageData, { quality }) as Uint8Array;
Expand Down
12 changes: 8 additions & 4 deletions packages/integrations/image/src/vendor/squoosh/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ export async function resize({ image, width, height }: ResizeOpts) {

export async function encodeJpeg(
image: ImageData,
{ quality }: { quality: number }
opts: { quality?: number }
): Promise<Uint8Array> {
image = ImageData.from(image)

const e = supportedFormats['mozjpeg']
const m = await e.enc()
await maybeDelay()
const quality = opts.quality || e.defaultEncoderOptions.quality
const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
quality,
Expand All @@ -87,13 +88,14 @@ export async function encodeJpeg(

export async function encodeWebp(
image: ImageData,
{ quality }: { quality: number }
opts: { quality?: number }
): Promise<Uint8Array> {
image = ImageData.from(image)

const e = supportedFormats['webp']
const m = await e.enc()
await maybeDelay()
const quality = opts.quality || e.defaultEncoderOptions.quality
const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
quality,
Expand All @@ -103,14 +105,16 @@ export async function encodeWebp(

export async function encodeAvif(
image: ImageData,
{ quality }: { quality: number }
opts: { quality?: number }
): Promise<Uint8Array> {
image = ImageData.from(image)

const e = supportedFormats['avif']
const m = await e.enc()
await maybeDelay()
const val = e.autoOptimize.min
// AVIF doesn't use a 0-100 quality, default to 75 and convert to cqLevel below
const quality = opts.quality || 75
const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
// Think of cqLevel as the "amount" of quantization (0 to 62),
Expand Down