Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.1 KB
Raw Blame
import type { Area } from 'react-easy-crop'
import { createImage, getMimeType } from '@/app/components/base/icon-picker/image-crop'

const AVATAR_IMAGE_MAX_SIZE = 256
const AVATAR_IMAGE_QUALITY = 0.85
const AVATAR_IMAGE_EXTENSION_BY_MIME_TYPE: Record<string, string> = {
  'image/jpeg': 'jpg',
  'image/png': 'png',
  'image/webp': 'webp',
}

export const getBoundedAvatarImageSize = (
  crop: Pick<Area, 'width' | 'height'>,
  maxSize = AVATAR_IMAGE_MAX_SIZE,
) => {
  const scale = Math.min(1, maxSize / Math.max(crop.width, crop.height))

  return {
    width: Math.max(1, Math.round(crop.width * scale)),
    height: Math.max(1, Math.round(crop.height * scale)),
  }
}

export const createCroppedAvatarImage = async (
  imageSrc: string,
  pixelCrop: Area,
  fileName: string,
): Promise<Blob> => {
  const image = await createImage(imageSrc)
  const canvas = document.createElement('canvas')
  const context = canvas.getContext('2d')

  if (!context) throw new Error('Could not create a canvas context')

  const outputSize = getBoundedAvatarImageSize(pixelCrop)
  canvas.width = outputSize.width
  canvas.height = outputSize.height
  context.imageSmoothingEnabled = true
  context.imageSmoothingQuality = 'high'
  context.drawImage(
    image,
    pixelCrop.x,
    pixelCrop.y,
    pixelCrop.width,
    pixelCrop.height,
    0,
    0,
    outputSize.width,
    outputSize.height,
  )

  return new Promise((resolve, reject) => {
    canvas.toBlob(
      (file) => {
        if (file) resolve(file)
        else reject(new Error('Could not create a blob'))
      },
      getMimeType(fileName),
      AVATAR_IMAGE_QUALITY,
    )
  })
}

export const createAvatarImageFile = (blob: Blob, fileName: string) => {
  const extension = AVATAR_IMAGE_EXTENSION_BY_MIME_TYPE[blob.type]

  if (!extension || getMimeType(fileName) === blob.type)
    return new File([blob], fileName, { type: blob.type })

  const extensionStart = fileName.lastIndexOf('.')
  const baseName = extensionStart > 0 ? fileName.slice(0, extensionStart) : fileName

  return new File([blob], `${baseName}.${extension}`, { type: blob.type })
}