Skip to content

Commit

Permalink
fix: image blurhash logic
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 25, 2024
1 parent c13b812 commit bf54dd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/drawer/components/image-detail-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { Image as ImageModel } from '~/models/base'
import type { PropType } from 'vue'

import { getBlurHash, getDominantColor } from '~/utils/image'
import { encodeImageToBlurhash, getDominantColor } from '~/utils/image'
import { isVideoExt, pickImagesFromMarkdown } from '~/utils/markdown'

export const ImageDetailSection = defineComponent({
Expand Down Expand Up @@ -130,7 +130,7 @@ export const ImageDetailSection = defineComponent({
src: item.src,
type: ext,
accent: getDominantColor($image),
blurHash: getBlurHash($image),
blurHash: encodeImageToBlurhash($image),
})
})
$image.onerror = (err) => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ export function getBlurHash(imageObject: HTMLImageElement) {

return encode(pixels, 32, 32, componentX, componentY)
}

const getImageData = (image: HTMLImageElement) => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const context = canvas.getContext('2d')!
context.drawImage(image, 0, 0)
return context.getImageData(0, 0, image.width, image.height)
}

export const encodeImageToBlurhash = (image: HTMLImageElement) => {
const imageData = getImageData(image)
return encode(imageData.data, imageData.width, imageData.height, 4, 4)
}

0 comments on commit bf54dd4

Please sign in to comment.