Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/four-pots-see.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Adds kernel option to select resize algorithm in the sharp image service
Comment thread
Princesseuh marked this conversation as resolved.
Outdated
11 changes: 10 additions & 1 deletion packages/astro/src/assets/services/sharp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FitEnum, FormatEnum, SharpOptions } from 'sharp';
import type { FitEnum, FormatEnum, ResizeOptions, SharpOptions } from 'sharp';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { ImageFit, ImageOutputFormat, ImageQualityPreset } from '../types.js';
import {
Expand All @@ -13,6 +13,11 @@ export interface SharpImageServiceConfig {
* The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information
*/
limitInputPixels?: SharpOptions['limitInputPixels'];

/**
* The `kernel` option is passed to resize calls. See https://sharp.pixelplumbing.com/api-resize/ for more information
*/
kernel?: ResizeOptions['kernel'];
}

let sharp: typeof import('sharp');
Expand Down Expand Up @@ -57,6 +62,7 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
async transform(inputBuffer, transformOptions, config) {
if (!sharp) sharp = await loadSharp();
const transform: BaseServiceTransform = transformOptions as BaseServiceTransform;
const kernel = config.service.config.kernel;

// Return SVGs as-is
// TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service.
Expand All @@ -81,18 +87,21 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
result.resize({
width: Math.round(transform.width),
height: Math.round(transform.height),
kernel: kernel,
fit,
position: transform.position,
withoutEnlargement,
});
} else if (transform.height && !transform.width) {
result.resize({
height: Math.round(transform.height),
kernel: kernel,
withoutEnlargement,
});
} else if (transform.width) {
result.resize({
width: Math.round(transform.width),
kernel: kernel,
withoutEnlargement,
});
}
Expand Down
Loading