feat(new tool): Images Formats Converter

Fix #1313
This commit is contained in:
sharevb 2024-10-02 22:27:41 +02:00 committed by ShareVB
parent 1c35ac3704
commit bac8c8dc15
7 changed files with 198 additions and 17 deletions

4
components.d.ts vendored
View file

@ -101,6 +101,7 @@ declare module '@vue/runtime-core' {
IconMdiSearch: typeof import('~icons/mdi/search')['default']
IconMdiTranslate: typeof import('~icons/mdi/translate')['default']
IconMdiTriangleDown: typeof import('~icons/mdi/triangle-down')['default']
ImageConverter: typeof import('./src/tools/image-converter/image-converter.vue')['default']
InputCopyable: typeof import('./src/components/InputCopyable.vue')['default']
IntegerBaseConverter: typeof import('./src/tools/integer-base-converter/integer-base-converter.vue')['default']
Ipv4AddressConverter: typeof import('./src/tools/ipv4-address-converter/ipv4-address-converter.vue')['default']
@ -135,13 +136,16 @@ declare module '@vue/runtime-core' {
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin']
NTable: typeof import('naive-ui')['NTable']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']

View file

@ -68,6 +68,7 @@
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
"image-in-browser": "^3.2.0",
"js-base64": "^3.7.6",
"json5": "^2.2.3",
"jwt-decode": "^3.1.2",
@ -98,6 +99,7 @@
"vue-router": "^4.1.6",
"vue-shadow-dom": "^4.2.0",
"vue-tsc": "^1.8.1",
"webp-converter-browser": "^1.0.4",
"xml-formatter": "^3.3.2",
"xml-js": "^1.6.11",
"yaml": "^2.2.1"

22
pnpm-lock.yaml generated
View file

@ -101,6 +101,9 @@ dependencies:
ibantools:
specifier: ^4.3.3
version: 4.3.3
image-in-browser:
specifier: ^3.2.0
version: 3.2.0
js-base64:
specifier: ^3.7.6
version: 3.7.7
@ -191,6 +194,9 @@ dependencies:
vue-tsc:
specifier: ^1.8.1
version: 1.8.1(typescript@5.2.2)
webp-converter-browser:
specifier: ^1.0.4
version: 1.0.4
xml-formatter:
specifier: ^3.3.2
version: 3.3.2
@ -3395,7 +3401,7 @@ packages:
dependencies:
'@unhead/dom': 0.5.1
'@unhead/schema': 0.5.1
'@vueuse/shared': 11.0.3(vue@3.3.4)
'@vueuse/shared': 11.1.0(vue@3.3.4)
unhead: 0.5.1
vue: 3.3.4
transitivePeerDependencies:
@ -4037,8 +4043,8 @@ packages:
- vue
dev: false
/@vueuse/shared@11.0.3(vue@3.3.4):
resolution: {integrity: sha512-0rY2m6HS5t27n/Vp5cTDsKTlNnimCqsbh/fmT2LgE+aaU42EMfXo8+bNX91W9I7DDmxfuACXMmrd7d79JxkqWA==}
/@vueuse/shared@11.1.0(vue@3.3.4):
resolution: {integrity: sha512-YUtIpY122q7osj+zsNMFAfMTubGz0sn5QzE5gPzAIiCmtt2ha3uQUY1+JPyL4gRCTsLPX82Y9brNbo/aqlA91w==}
dependencies:
vue-demi: 0.14.10(vue@3.3.4)
transitivePeerDependencies:
@ -6171,6 +6177,10 @@ packages:
engines: {node: '>= 4'}
dev: true
/image-in-browser@3.2.0:
resolution: {integrity: sha512-N2u2IEBbgBwYCZQ150f1G0RY+eDQ5AoT43BYEbg6nnjtiLqb5LZTj64t0R6AEicRNKOm80rGuleZcWOFNumR8g==}
dev: false
/image-size@0.5.5:
resolution: {integrity: sha512-6TDAlDPZxUFCv+fuOkIoXT/V/f3Qbq8e37p+YOiYrUv3v9cc3/6x78VdfPgFVaB9dZYeLUfKgHRebpkm/oP2VQ==}
engines: {node: '>=0.10.0'}
@ -9388,6 +9398,12 @@ packages:
engines: {node: '>=12'}
dev: true
/webp-converter-browser@1.0.4:
resolution: {integrity: sha512-ZkcrrM4TyxBI7mPmE4uG6PU2MF5YXhKTtvub/ll/4ZjXM7THcms0C0tA10CwnsrLzRBC+ry/sUzwpW6vdPzA8g==}
dependencies:
'@babel/runtime': 7.23.2
dev: false
/webpack-sources@3.2.3:
resolution: {integrity: sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==}
engines: {node: '>=10.13.0'}

View file

@ -1,6 +1,7 @@
import { extension as getExtensionFromMimeType, extension as getMimeTypeFromExtension } from 'mime-types';
import type { Ref } from 'vue';
import type { MaybeRef, Ref } from 'vue';
import _ from 'lodash';
import { get } from '@vueuse/core';
export {
getMimeTypeFromBase64,
@ -75,21 +76,11 @@ function downloadFromBase64({ sourceValue, filename, extension, fileMimeType }:
}
function useDownloadFileFromBase64(
{ source, filename, extension, fileMimeType }:
{ source: Ref<string>; filename?: string; extension?: string; fileMimeType?: string }) {
return {
download() {
downloadFromBase64({ sourceValue: source.value, filename, extension, fileMimeType });
},
};
}
function useDownloadFileFromBase64Refs(
{ source, filename, extension }:
{ source: Ref<string>; filename?: Ref<string>; extension?: Ref<string> }) {
{ source: MaybeRef<string>; filename?: MaybeRef<string>; extension?: MaybeRef<string> }) {
return {
download() {
downloadFromBase64({ sourceValue: source.value, filename: filename?.value, extension: extension?.value });
downloadFromBase64({ sourceValue: get(source), filename: get(filename), extension: get(extension) });
},
};
}
@ -116,3 +107,13 @@ function previewImageFromBase64(base64String: string): HTMLImageElement {
return img;
}
function useDownloadFileFromBase64Refs(
{ source, filename, extension }:
{ source: Ref<string>; filename?: Ref<string>; extension?: Ref<string> }) {
return {
download() {
downloadFromBase64({ sourceValue: source.value, filename: filename?.value, extension: extension?.value });
},
};
}

View file

@ -0,0 +1,139 @@
<script setup lang="ts">
import { Base64 } from 'js-base64';
import type { MemoryImage } from 'image-in-browser';
import { decodeImage, encodeBmp, encodeGif, encodeIco, encodeJpg, encodePng, encodePvr, encodeTga, encodeTiff } from 'image-in-browser';
import { arrayBufferToWebP } from 'webp-converter-browser';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
const file = ref<File | null>(null);
const base64OutputFile = ref('');
const fileName = ref('');
const fileExtension = ref('');
const { download } = useDownloadFileFromBase64(
{
source: base64OutputFile,
filename: fileName,
extension: fileExtension,
});
const outputQuality = useQueryParamOrStorage({ name: 'qual', storageName: 'imgconv:q', defaultValue: 0.95 });
const outputFormats = {
png: {
mime: 'image/png',
save: (image: MemoryImage) => encodePng({ image }),
},
jpg: {
mime: 'image/jpeg',
save: (image: MemoryImage) => encodeJpg({ image, quality: outputQuality.value }),
},
bmp: {
mime: 'image/bmp',
save: (image: MemoryImage) => encodeBmp({ image }),
},
gif: {
mime: 'image/gif',
save: (image: MemoryImage) => encodeGif({ image }),
},
ico: {
mime: 'image/x-icon',
save: (image: MemoryImage) => encodeIco({ image }),
},
tga: {
mime: 'image/tga',
save: (image: MemoryImage) => encodeTga({ image }),
},
pvr: {
mime: 'image/pvr',
save: (image: MemoryImage) => encodePvr({ image }),
},
tif: {
mime: 'image/tif',
save: (image: MemoryImage) => encodeTiff({ image }),
},
webp: {
mime: 'image/webp',
save: () => null,
},
};
const outputFormat = useQueryParamOrStorage({ name: 'fmt', storageName: 'imgconv:fmt', defaultValue: 'png' });
const outputFormatHasQuality = computed(() => {
return outputFormat.value === 'jpg';
});
async function onFileUploaded(uploadedFile: File) {
const outputFormatValue = outputFormat.value;
file.value = uploadedFile;
const fileBuffer = new Uint8Array(await uploadedFile.arrayBuffer());
fileName.value = `${uploadedFile.name}`;
status.value = 'processing';
try {
if (outputFormatValue === 'webp') {
const encodedImage = await arrayBufferToWebP(fileBuffer);
fileExtension.value = 'webp';
base64OutputFile.value = `data:image/webp;base64,${Base64.fromUint8Array(new Uint8Array(await encodedImage.arrayBuffer()))}`;
}
else {
const decodedImage = decodeImage({
data: fileBuffer,
});
if (decodedImage == null) {
throw new Error('Invalid Image file!');
};
const outConfig = outputFormats[outputFormatValue as (keyof typeof outputFormats)];
const encodedImage = outConfig.save(decodedImage);
fileExtension.value = outputFormatValue;
base64OutputFile.value = `data:${outConfig.mime};base64,${Base64.fromUint8Array(encodedImage!)}`;
}
status.value = 'done';
download();
}
catch (e) {
status.value = 'error';
}
}
</script>
<template>
<div>
<div style="flex: 0 0 100%" mb-2>
<div mx-auto max-w-600px>
<c-file-upload
title="Drag and drop an image file here, or click to select a file"
accept="image/*"
paste-image
@file-upload="onFileUploaded"
/>
</div>
</div>
<c-select
v-model:value="outputFormat"
label="Output format:"
label-position="left"
:options="Object.keys(outputFormats)"
placeholder="Select output format"
mb-2
/>
<n-form-item v-if="outputFormatHasQuality" label="Output quality:" label-placement="left" mb-2>
<n-input-number v-model:value="outputQuality" :max="100" :min="0" w-full />
</n-form-item>
<div mt-3 flex justify-center>
<c-alert v-if="status === 'error'" type="error">
An error occured processing {{ fileName }}
</c-alert>
<n-spin
v-if="status === 'processing'"
size="small"
/>
</div>
</div>
</template>

View file

@ -0,0 +1,12 @@
import { PictureInPicture } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Image Formats Converter',
path: '/image-converter',
description: 'Convert images from one format to another',
keywords: ['image', 'bmp', 'gif', 'ico', 'jpg', 'png', 'tga', 'pvr', 'tiff', 'pnm', 'pbm', 'pgm', 'ppm', 'psd', 'webp', 'converter'],
component: () => import('./image-converter.vue'),
icon: PictureInPicture,
createdAt: new Date('2024-08-15'),
});

View file

@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as imageConverter } from './image-converter';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@ -141,7 +142,13 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Images and videos',
components: [qrCodeGenerator, wifiQrCodeGenerator, svgPlaceholderGenerator, cameraRecorder],
components: [
qrCodeGenerator,
wifiQrCodeGenerator,
svgPlaceholderGenerator,
cameraRecorder,
imageConverter,
],
},
{
name: 'Development',