Skip to content

Commit

Permalink
fix: upload app image cover to jpeg for format png
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-bro committed Sep 5, 2024
1 parent 863f305 commit 2b44b35
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions web/app/components/base/app-icon-picker/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ALLOW_FILE_EXTENSIONS } from '@/types/app'

type UploaderProps = {
className?: string
onImageCropped?: (tempUrl: string, croppedAreaPixels: Area, fileName: string) => void
onImageCropped?: (tempUrl: string, type: string, croppedAreaPixels: Area, fileName: string) => void
}

const Uploader: FC<UploaderProps> = ({
Expand All @@ -33,7 +33,7 @@ const Uploader: FC<UploaderProps> = ({
const onCropComplete = async (_: Area, croppedAreaPixels: Area) => {
if (!inputImage)
return
onImageCropped?.(inputImage.url, croppedAreaPixels, inputImage.file.name)
onImageCropped?.(inputImage.url, inputImage.file.type, croppedAreaPixels, inputImage.file.name)
}

const handleLocalFileInput = (e: ChangeEvent<HTMLInputElement>) => {
Expand Down
8 changes: 4 additions & 4 deletions web/app/components/base/app-icon-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ const AppIconPicker: FC<AppIconPickerProps> = ({
},
})

const [imageCropInfo, setImageCropInfo] = useState<{ tempUrl: string; croppedAreaPixels: Area; fileName: string }>()
const handleImageCropped = async (tempUrl: string, croppedAreaPixels: Area, fileName: string) => {
setImageCropInfo({ tempUrl, croppedAreaPixels, fileName })
const [imageCropInfo, setImageCropInfo] = useState<{ tempUrl: string; type: string; croppedAreaPixels: Area; fileName: string }>()
const handleImageCropped = async (tempUrl: string, type: string, croppedAreaPixels: Area, fileName: string) => {
setImageCropInfo({ tempUrl, type, croppedAreaPixels, fileName })
}

const handleSelect = async () => {
Expand All @@ -88,7 +88,7 @@ const AppIconPicker: FC<AppIconPickerProps> = ({
if (!imageCropInfo)
return
setUploading(true)
const blob = await getCroppedImg(imageCropInfo.tempUrl, imageCropInfo.croppedAreaPixels)
const blob = await getCroppedImg(imageCropInfo.tempUrl, imageCropInfo.type, imageCropInfo.croppedAreaPixels)
const file = new File([blob], imageCropInfo.fileName, { type: blob.type })
handleLocalFileUpload(file)
}
Expand Down
3 changes: 2 additions & 1 deletion web/app/components/base/app-icon-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function rotateSize(width: number, height: number, rotation: number) {
*/
export default async function getCroppedImg(
imageSrc: string,
imageType: string,
pixelCrop: { x: number; y: number; width: number; height: number },
rotation = 0,
flip = { horizontal: false, vertical: false },
Expand Down Expand Up @@ -93,6 +94,6 @@ export default async function getCroppedImg(
resolve(file)
else
reject(new Error('Could not create a blob'))
}, 'image/jpeg')
}, imageType)
})
}

0 comments on commit 2b44b35

Please sign in to comment.