Skip to content

Commit

Permalink
feat: Add Oss image resizeScale params
Browse files Browse the repository at this point in the history
  • Loading branch information
Vibes-INS committed Oct 14, 2021
1 parent e19fdbf commit 765c96c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 3 additions & 2 deletions apps/mibao-ui-docs/src/app/pages/image.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ const Template: Story = (args) =>

export const Image = Template.bind({})
Image.args = {
src: 'https://images.pexels.com/photos/2635817/pexels-photo-2635817.jpeg',
src: 'https://oss.jinse.cc/production/bd7c7508-026e-4fcd-abf8-1fa1061f4d6c.png',
alt: '',
aspectRatio: true,
width: '200px',
height: '',
objectFit: 'cover',
crossOrigin: '',
disableContextMenu: false
disableContextMenu: false,
resizeScale: 200
}
9 changes: 9 additions & 0 deletions libs/mibao-ui/src/constants/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const OSS_IMG_PROCESS_QUERY_KEY = process.env.OSS_IMG_PROCESS_QUERY_KEY ?? 'x-oss-process'
export const OSS_IMG_PROCESS_QUERY_KEY_SCALE = process.env.OSS_IMG_PROCESS_QUERY_KEY_SCALE ?? 'image/resize,s_'
export const OSS_IMG_PROCESS_QUERY_KEY_FORMAT_WEBP = process.env.OSS_IMG_PROCESS_QUERY_KEY_FORMAT_WEBP ?? '/format,webp'
export const OSS_IMG_HOSTS = process.env.OSS_IMG_HOSTS
? process.env.OSS_IMG_HOSTS.split(',')
: [
'https://oss.jinse.cc',
'https://goldenlegend.oss-accelerate.aliyuncs.com'
]
14 changes: 10 additions & 4 deletions libs/mibao-ui/src/lib/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Box, Image as ChakraImage, ImageProps as ChakraImageProps, Skeleton } f
import styles from './image.module.scss'
import { useState, useMemo, useEffect, useCallback, ReactNode } from 'react'
import FALLBACK_SRC from '../../../assets/images/fallback.svg'
import { addParamsToUrl, disableImageContext, omit } from '../../utils'
import { addParamsToUrl, disableImageContext, getImagePreviewUrl, omit } from '../../utils'

export interface ImageProps extends ChakraImageProps {
aspectRatio?: boolean
loader?: ReactNode
srcQueryParams?: Record<string, string | number>
disableContextMenu?: boolean
resizeScale?: number // Specifies the shortest edge of the target zoom graph.
}

export const Image = (props: ImageProps) => {
Expand All @@ -22,14 +23,19 @@ export const Image = (props: ImageProps) => {
}, [props.loader])
// src
const imageSrc = useMemo(() => {
if (!props.srcQueryParams || !props.src) return props.src
return addParamsToUrl(props.src, props.srcQueryParams)
if (!props.src) return props.src
const srcQueryParams = props.srcQueryParams ?? {}
if (props.resizeScale) {
return addParamsToUrl(getImagePreviewUrl(props.src, props.resizeScale), srcQueryParams)
}
return addParamsToUrl(props.src, srcQueryParams)
}, [props])
// omit props
const imageProps = useMemo(() => omit(props, [
'aspectRatio',
'loader',
'srcQueryParams'
'srcQueryParams',
'resizeScale'
]), [props])

useEffect(() => {
Expand Down
24 changes: 24 additions & 0 deletions libs/mibao-ui/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
OSS_IMG_HOSTS,
OSS_IMG_PROCESS_QUERY_KEY,
OSS_IMG_PROCESS_QUERY_KEY_SCALE
} from '../constants/env'

export function addParamsToUrl (
url: string,
params: Record<string, string | number>,
Expand All @@ -18,6 +24,24 @@ export function addParamsToUrl (
return decodeURI(urlObj.toString())
}

export function getImagePreviewUrl<U extends string | undefined> (
url: U,
size = 300
): U {
if (!url) {
return url
}
const urlObj = new URL(url)
const isOssHost = OSS_IMG_HOSTS.some((host) => url?.startsWith(host))
const isSvgOrWebp = /\.(svg|webp)$/i.test(urlObj.pathname)
if (!isOssHost || isSvgOrWebp) {
return url
}
const params: Record<string, string | number> = {}
params[OSS_IMG_PROCESS_QUERY_KEY] = `${OSS_IMG_PROCESS_QUERY_KEY_SCALE}${size}`
return addParamsToUrl(url, params) as U
}

export function omit<T extends {[key: string]: any}, K extends keyof T> (obj: T, keys: K[]): Omit<T, K> {
return keys.reduce((acc, key) => ({ ...acc, [key]: undefined }), obj)
}
Expand Down

0 comments on commit 765c96c

Please sign in to comment.