Skip to content

Commit

Permalink
fix: heif compression options
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Dec 2, 2023
1 parent 48f8e7b commit 9f86e6d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/pathToImage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Buffer } from 'node:buffer'
import { parse as urlParse } from 'node:url'
import { match } from 'path-to-regexp'
import type { Create, CreateText } from 'sharp'
import sharp from 'sharp'
Expand All @@ -12,7 +11,7 @@ import type {
ImagePlaceholderQuery,
ImageType,
} from './types'
import { formatColor, formatText, getBackground } from './utils'
import { formatColor, formatText, getBackground, urlParse } from './utils'

export type CreateOptions = Create

Expand All @@ -23,14 +22,16 @@ export async function pathToImage(
findPathRule: FindPathRule,
options: Required<ImagePlaceholderOptions>,
): Promise<ImageCacheItem | undefined> {
if (bufferCache.has(url)) {
if (bufferCache.has(url))
return bufferCache.get(url)
}
const { query: urlQuery, pathname } = urlParse(url, true)

const { query: urlQuery, pathname } = urlParse(url)

const rule = findPathRule(pathname!)

if (!rule) return
if (!rule)
return

const urlMatch = match(rule, { decode: decodeURIComponent })(pathname!) || {
params: {
width: options.width,
Expand Down Expand Up @@ -83,7 +84,8 @@ export async function pathToImage(
}
bufferCache.set(url, result)
return result
} catch (e) {
}
catch (e) {
console.error(e)
}
}
Expand All @@ -103,9 +105,9 @@ export async function createImage({
compressionLevel: number
textColor: string
}) {
if (type === 'svg') {
if (type === 'svg')
return createSVG(create, text.text, textColor)
}

let image = sharp({ create })

text.text = formatText(text.text, textColor)
Expand All @@ -121,7 +123,7 @@ export async function createImage({
image = image.webp({ quality })
break
case 'heif':
image = image.heif({ quality })
image = image.heif({ quality, compression: 'av1' })
break
case 'avif':
image = image.avif({ quality })
Expand Down

0 comments on commit 9f86e6d

Please sign in to comment.