astro/packages/integrations/image/src/loaders/sharp.ts
Gérald Gounot ac3649bb58
Add support for SVG images to  @astrojs/image (#6118)
* @astrojs/image: add support for SVG images

* @astrojs/image: add tests for SVG images

* @astrojs/image: update README.md with SVG format info

* Add minor changeset for @astrojs/image
2023-02-15 09:21:41 +01:00

53 lines
1.5 KiB
TypeScript

import sharp from 'sharp';
import type { SSRImageService } from '../loaders/index.js';
import { BaseSSRService, isOutputFormatSupportsAlpha } from '../loaders/index.js';
import type { OutputFormat, TransformOptions } from './index.js';
class SharpService extends BaseSSRService {
async transform(inputBuffer: Buffer, transform: TransformOptions) {
if (transform.format === 'svg') {
// sharp can't output SVG so we return the input image
return {
data: inputBuffer,
format: transform.format,
};
}
const sharpImage = sharp(inputBuffer, { failOnError: false, pages: -1 });
// always call rotate to adjust for EXIF data orientation
sharpImage.rotate();
if (transform.width || transform.height) {
const width = transform.width && Math.round(transform.width);
const height = transform.height && Math.round(transform.height);
sharpImage.resize({
width,
height,
fit: transform.fit,
position: transform.position,
background: transform.background,
});
}
if (transform.format) {
sharpImage.toFormat(transform.format, { quality: transform.quality });
if (transform.background && !isOutputFormatSupportsAlpha(transform.format)) {
sharpImage.flatten({ background: transform.background });
}
}
const { data, info } = await sharpImage.toBuffer({ resolveWithObject: true });
return {
data,
format: info.format as OutputFormat,
};
}
}
const service: SSRImageService = new SharpService();
export default service;