Skip to content

Commit bac8c8d

Browse files
committed
feat(new tool): Images Formats Converter
Fix CorentinTh#1313
1 parent 1c35ac3 commit bac8c8d

File tree

7 files changed

+198
-17
lines changed

7 files changed

+198
-17
lines changed

components.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ declare module '@vue/runtime-core' {
101101
IconMdiSearch: typeof import('~icons/mdi/search')['default']
102102
IconMdiTranslate: typeof import('~icons/mdi/translate')['default']
103103
IconMdiTriangleDown: typeof import('~icons/mdi/triangle-down')['default']
104+
ImageConverter: typeof import('./src/tools/image-converter/image-converter.vue')['default']
104105
InputCopyable: typeof import('./src/components/InputCopyable.vue')['default']
105106
IntegerBaseConverter: typeof import('./src/tools/integer-base-converter/integer-base-converter.vue')['default']
106107
Ipv4AddressConverter: typeof import('./src/tools/ipv4-address-converter/ipv4-address-converter.vue')['default']
@@ -135,13 +136,16 @@ declare module '@vue/runtime-core' {
135136
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
136137
NDivider: typeof import('naive-ui')['NDivider']
137138
NEllipsis: typeof import('naive-ui')['NEllipsis']
139+
NFormItem: typeof import('naive-ui')['NFormItem']
138140
NH1: typeof import('naive-ui')['NH1']
139141
NH3: typeof import('naive-ui')['NH3']
140142
NIcon: typeof import('naive-ui')['NIcon']
143+
NInputNumber: typeof import('naive-ui')['NInputNumber']
141144
NLayout: typeof import('naive-ui')['NLayout']
142145
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
143146
NMenu: typeof import('naive-ui')['NMenu']
144147
NSpace: typeof import('naive-ui')['NSpace']
148+
NSpin: typeof import('naive-ui')['NSpin']
145149
NTable: typeof import('naive-ui')['NTable']
146150
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
147151
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"highlight.js": "^11.7.0",
6969
"iarna-toml-esm": "^3.0.5",
7070
"ibantools": "^4.3.3",
71+
"image-in-browser": "^3.2.0",
7172
"js-base64": "^3.7.6",
7273
"json5": "^2.2.3",
7374
"jwt-decode": "^3.1.2",
@@ -98,6 +99,7 @@
9899
"vue-router": "^4.1.6",
99100
"vue-shadow-dom": "^4.2.0",
100101
"vue-tsc": "^1.8.1",
102+
"webp-converter-browser": "^1.0.4",
101103
"xml-formatter": "^3.3.2",
102104
"xml-js": "^1.6.11",
103105
"yaml": "^2.2.1"

pnpm-lock.yaml

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/composable/downloadBase64.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { extension as getExtensionFromMimeType, extension as getMimeTypeFromExtension } from 'mime-types';
2-
import type { Ref } from 'vue';
2+
import type { MaybeRef, Ref } from 'vue';
33
import _ from 'lodash';
4+
import { get } from '@vueuse/core';
45

56
export {
67
getMimeTypeFromBase64,
@@ -75,21 +76,11 @@ function downloadFromBase64({ sourceValue, filename, extension, fileMimeType }:
7576
}
7677

7778
function useDownloadFileFromBase64(
78-
{ source, filename, extension, fileMimeType }:
79-
{ source: Ref<string>; filename?: string; extension?: string; fileMimeType?: string }) {
80-
return {
81-
download() {
82-
downloadFromBase64({ sourceValue: source.value, filename, extension, fileMimeType });
83-
},
84-
};
85-
}
86-
87-
function useDownloadFileFromBase64Refs(
8879
{ source, filename, extension }:
89-
{ source: Ref<string>; filename?: Ref<string>; extension?: Ref<string> }) {
80+
{ source: MaybeRef<string>; filename?: MaybeRef<string>; extension?: MaybeRef<string> }) {
9081
return {
9182
download() {
92-
downloadFromBase64({ sourceValue: source.value, filename: filename?.value, extension: extension?.value });
83+
downloadFromBase64({ sourceValue: get(source), filename: get(filename), extension: get(extension) });
9384
},
9485
};
9586
}
@@ -116,3 +107,13 @@ function previewImageFromBase64(base64String: string): HTMLImageElement {
116107

117108
return img;
118109
}
110+
111+
function useDownloadFileFromBase64Refs(
112+
{ source, filename, extension }:
113+
{ source: Ref<string>; filename?: Ref<string>; extension?: Ref<string> }) {
114+
return {
115+
download() {
116+
downloadFromBase64({ sourceValue: source.value, filename: filename?.value, extension: extension?.value });
117+
},
118+
};
119+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<script setup lang="ts">
2+
import { Base64 } from 'js-base64';
3+
import type { MemoryImage } from 'image-in-browser';
4+
import { decodeImage, encodeBmp, encodeGif, encodeIco, encodeJpg, encodePng, encodePvr, encodeTga, encodeTiff } from 'image-in-browser';
5+
import { arrayBufferToWebP } from 'webp-converter-browser';
6+
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
7+
import { useQueryParamOrStorage } from '@/composable/queryParams';
8+
9+
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
10+
const file = ref<File | null>(null);
11+
12+
const base64OutputFile = ref('');
13+
const fileName = ref('');
14+
const fileExtension = ref('');
15+
const { download } = useDownloadFileFromBase64(
16+
{
17+
source: base64OutputFile,
18+
filename: fileName,
19+
extension: fileExtension,
20+
});
21+
22+
const outputQuality = useQueryParamOrStorage({ name: 'qual', storageName: 'imgconv:q', defaultValue: 0.95 });
23+
const outputFormats = {
24+
png: {
25+
mime: 'image/png',
26+
save: (image: MemoryImage) => encodePng({ image }),
27+
},
28+
jpg: {
29+
mime: 'image/jpeg',
30+
save: (image: MemoryImage) => encodeJpg({ image, quality: outputQuality.value }),
31+
},
32+
bmp: {
33+
mime: 'image/bmp',
34+
save: (image: MemoryImage) => encodeBmp({ image }),
35+
},
36+
gif: {
37+
mime: 'image/gif',
38+
save: (image: MemoryImage) => encodeGif({ image }),
39+
},
40+
ico: {
41+
mime: 'image/x-icon',
42+
save: (image: MemoryImage) => encodeIco({ image }),
43+
},
44+
tga: {
45+
mime: 'image/tga',
46+
save: (image: MemoryImage) => encodeTga({ image }),
47+
},
48+
pvr: {
49+
mime: 'image/pvr',
50+
save: (image: MemoryImage) => encodePvr({ image }),
51+
},
52+
tif: {
53+
mime: 'image/tif',
54+
save: (image: MemoryImage) => encodeTiff({ image }),
55+
},
56+
webp: {
57+
mime: 'image/webp',
58+
save: () => null,
59+
},
60+
};
61+
62+
const outputFormat = useQueryParamOrStorage({ name: 'fmt', storageName: 'imgconv:fmt', defaultValue: 'png' });
63+
const outputFormatHasQuality = computed(() => {
64+
return outputFormat.value === 'jpg';
65+
});
66+
67+
async function onFileUploaded(uploadedFile: File) {
68+
const outputFormatValue = outputFormat.value;
69+
file.value = uploadedFile;
70+
const fileBuffer = new Uint8Array(await uploadedFile.arrayBuffer());
71+
72+
fileName.value = `${uploadedFile.name}`;
73+
status.value = 'processing';
74+
try {
75+
if (outputFormatValue === 'webp') {
76+
const encodedImage = await arrayBufferToWebP(fileBuffer);
77+
fileExtension.value = 'webp';
78+
base64OutputFile.value = `data:image/webp;base64,${Base64.fromUint8Array(new Uint8Array(await encodedImage.arrayBuffer()))}`;
79+
}
80+
else {
81+
const decodedImage = decodeImage({
82+
data: fileBuffer,
83+
});
84+
85+
if (decodedImage == null) {
86+
throw new Error('Invalid Image file!');
87+
};
88+
89+
const outConfig = outputFormats[outputFormatValue as (keyof typeof outputFormats)];
90+
const encodedImage = outConfig.save(decodedImage);
91+
fileExtension.value = outputFormatValue;
92+
base64OutputFile.value = `data:${outConfig.mime};base64,${Base64.fromUint8Array(encodedImage!)}`;
93+
}
94+
status.value = 'done';
95+
96+
download();
97+
}
98+
catch (e) {
99+
status.value = 'error';
100+
}
101+
}
102+
</script>
103+
104+
<template>
105+
<div>
106+
<div style="flex: 0 0 100%" mb-2>
107+
<div mx-auto max-w-600px>
108+
<c-file-upload
109+
title="Drag and drop an image file here, or click to select a file"
110+
accept="image/*"
111+
paste-image
112+
@file-upload="onFileUploaded"
113+
/>
114+
</div>
115+
</div>
116+
117+
<c-select
118+
v-model:value="outputFormat"
119+
label="Output format:"
120+
label-position="left"
121+
:options="Object.keys(outputFormats)"
122+
placeholder="Select output format"
123+
mb-2
124+
/>
125+
<n-form-item v-if="outputFormatHasQuality" label="Output quality:" label-placement="left" mb-2>
126+
<n-input-number v-model:value="outputQuality" :max="100" :min="0" w-full />
127+
</n-form-item>
128+
129+
<div mt-3 flex justify-center>
130+
<c-alert v-if="status === 'error'" type="error">
131+
An error occured processing {{ fileName }}
132+
</c-alert>
133+
<n-spin
134+
v-if="status === 'processing'"
135+
size="small"
136+
/>
137+
</div>
138+
</div>
139+
</template>

src/tools/image-converter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { PictureInPicture } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Image Formats Converter',
6+
path: '/image-converter',
7+
description: 'Convert images from one format to another',
8+
keywords: ['image', 'bmp', 'gif', 'ico', 'jpg', 'png', 'tga', 'pvr', 'tiff', 'pnm', 'pbm', 'pgm', 'ppm', 'psd', 'webp', 'converter'],
9+
component: () => import('./image-converter.vue'),
10+
icon: PictureInPicture,
11+
createdAt: new Date('2024-08-15'),
12+
});

src/tools/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
44
import { tool as emailNormalizer } from './email-normalizer';
5+
import { tool as imageConverter } from './image-converter';
56

67
import { tool as asciiTextDrawer } from './ascii-text-drawer';
78

@@ -141,7 +142,13 @@ export const toolsByCategory: ToolCategory[] = [
141142
},
142143
{
143144
name: 'Images and videos',
144-
components: [qrCodeGenerator, wifiQrCodeGenerator, svgPlaceholderGenerator, cameraRecorder],
145+
components: [
146+
qrCodeGenerator,
147+
wifiQrCodeGenerator,
148+
svgPlaceholderGenerator,
149+
cameraRecorder,
150+
imageConverter,
151+
],
145152
},
146153
{
147154
name: 'Development',

0 commit comments

Comments
 (0)