Skip to content

Commit 3535bd0

Browse files
committed
fix(ui): fix LQIP aspect ratio
1 parent f46023c commit 3535bd0

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pkg/thumbhash/thumbhash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func GetThumbhash(r io.Reader) (string, error) {
1919
if err != nil {
2020
return "", err
2121
}
22-
width := img.Bounds().Size().X
22+
size := img.Bounds().Size()
2323

2424
binHash := thumbhash.EncodeImage(img)
2525
hash := base64.StdEncoding.EncodeToString(binHash)
2626

27-
return fmt.Sprintf("%d|%s", width, hash), nil
27+
return fmt.Sprintf("%dx%d|%s", size.X, size.Y, hash), nil
2828
}

ui/src/components/LazyImage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const LazyImage: FC<ImgHTMLAttributes<HTMLImageElement> & Props> = ({thum
1616
const [loaded, setLoaded] = useState(false)
1717
const [data, setData] = useState('')
1818
const [width, setWidth] = useState('0px')
19+
const [aspectRatio, setAspectRatio] = useState<number>()
1920
const imgRef = useRef<HTMLImageElement>(null)
2021
const lqipRef = useRef<HTMLImageElement>(null)
2122
useEffect(() => {
@@ -28,9 +29,13 @@ export const LazyImage: FC<ImgHTMLAttributes<HTMLImageElement> & Props> = ({thum
2829
if (!thumbhash) {
2930
return
3031
}
31-
const [width, hash] = thumbhash.split('|')
32+
const [size, hash] = thumbhash.split('|')
33+
const [width, height] = size.split('x')
3234
setWidth(`${width}px`)
3335
try {
36+
if (height) {
37+
setAspectRatio(parseInt(width) / parseInt(height))
38+
}
3439
setData(thumbHashToDataURL(base64ToBinary(hash)))
3540
} catch (err) {
3641
console.error('unable to decode thumbhash', err)
@@ -49,6 +54,7 @@ export const LazyImage: FC<ImgHTMLAttributes<HTMLImageElement> & Props> = ({thum
4954
src={data}
5055
aria-hidden="true"
5156
onAnimationEnd={hideFn}
57+
style={{ aspectRatio }}
5258
className={classNames(styles.lqip, loaded ? styles.loaded : null)}
5359
/>
5460
<img

0 commit comments

Comments
 (0)