forked from CorentinTh/it-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CorentinTh#997
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<script setup lang="ts"> | ||
import { Base64 } from 'js-base64'; | ||
import heicConvert from 'heic-convert/browser'; | ||
import { useDownloadFileFromBase64Refs } from '@/composable/downloadBase64'; | ||
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle'); | ||
const file = ref<File | null>(null); | ||
const base64OutputImage = ref(''); | ||
const fileName = ref(''); | ||
const format = ref('jpg'); | ||
const formats = [ | ||
{ value: 'jpg', label: 'JPEG' }, | ||
{ value: 'png', label: 'PNG' }, | ||
]; | ||
const { download } = useDownloadFileFromBase64Refs( | ||
{ | ||
source: base64OutputImage, | ||
filename: fileName, | ||
}); | ||
async function onFileUploaded(uploadedFile: File) { | ||
file.value = uploadedFile; | ||
const fileBuffer = await uploadedFile.arrayBuffer(); | ||
fileName.value = `${uploadedFile.name}${format.value}`; | ||
status.value = 'processing'; | ||
try { | ||
let convertFormat; | ||
if (format.value === 'jpg') { | ||
convertFormat = 'JPEG'; | ||
} | ||
else if (format.value === 'png') { | ||
convertFormat = 'PNG'; | ||
} | ||
else { | ||
throw new Error('unknown format'); | ||
} | ||
const outputBuffer = await heicConvert({ | ||
buffer: fileBuffer, | ||
format: convertFormat, | ||
quality: 0.98, | ||
}); | ||
base64OutputImage.value = `data:image/${convertFormat.toLowerCase()};base64,${Base64.fromUint8Array(outputBuffer)}`; | ||
status.value = 'done'; | ||
download(); | ||
} | ||
catch (e) { | ||
status.value = 'error'; | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<c-select | ||
v-model:value="format" | ||
:options="formats" | ||
label="Output format" | ||
w-100px | ||
/> | ||
|
||
<div style="flex: 0 0 100%" mb-3> | ||
<div mx-auto max-w-600px> | ||
<c-file-upload | ||
title="Drag and drop a HEIC file here, or click to select a file" | ||
accept=".heic,.heif" @file-upload="onFileUploaded" | ||
/> | ||
</div> | ||
</div> | ||
<div mt-3 flex justify-center> | ||
<img :src="base64OutputImage" max-w-300px> | ||
</div> | ||
|
||
<div mt-3 flex justify-center> | ||
<c-alert v-if="status === 'error'" type="error"> | ||
An error occured processing {{ fileName }}. HEIC/HEIF is invalid. | ||
</c-alert> | ||
<n-spin | ||
v-if="status === 'processing'" | ||
size="small" | ||
/> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ArrowsShuffle } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'Heic converter', | ||
path: '/heic-converter', | ||
description: '', | ||
keywords: ['heic', 'converter'], | ||
component: () => import('./heic-converter.vue'), | ||
icon: ArrowsShuffle, | ||
createdAt: new Date('2024-04-23'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters