-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tool): ICO <> PNG Converter
Fix #1271
- Loading branch information
Showing
8 changed files
with
276 additions
and
61 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,92 @@ | ||
<script setup lang="ts"> | ||
import { Base64 } from 'js-base64'; | ||
import { Transform, decodeIco, decodeImage, encodeIcoImages, encodePng } from 'image-in-browser'; | ||
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64'; | ||
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, | ||
}); | ||
async function onFileUploaded(uploadedFile: File) { | ||
file.value = uploadedFile; | ||
const fileBuffer = new Uint8Array(await uploadedFile.arrayBuffer()); | ||
fileName.value = `${uploadedFile.name}`; | ||
status.value = 'processing'; | ||
try { | ||
if (uploadedFile.type.includes('icon')) { | ||
const decodedIco = decodeIco({ | ||
data: fileBuffer, | ||
largest: true, | ||
}); | ||
if (decodedIco == null) { | ||
throw new Error('Invalid ICO file!'); | ||
} | ||
const encodedPng = encodePng({ | ||
image: decodedIco, | ||
}); | ||
fileExtension.value = 'png'; | ||
base64OutputFile.value = `data:image/png;base64,${Base64.fromUint8Array(encodedPng)}`; | ||
} | ||
else { | ||
const decodedImage = decodeImage({ | ||
data: fileBuffer, | ||
}); | ||
if (decodedImage == null) { | ||
throw new Error('Invalid PNG file!'); | ||
}; | ||
const encodedICO = encodeIcoImages({ | ||
images: [16, 32, 64, 128, 256].map(size => Transform.copyResize({ | ||
image: decodedImage, | ||
width: size, | ||
maintainAspect: true, | ||
})), | ||
}); | ||
fileExtension.value = 'ico'; | ||
base64OutputFile.value = `data:image/x-icon;base64,${Base64.fromUint8Array(encodedICO)}`; | ||
} | ||
status.value = 'done'; | ||
download(); | ||
} | ||
catch (e) { | ||
status.value = 'error'; | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div style="flex: 0 0 100%"> | ||
<div mx-auto max-w-600px> | ||
<c-file-upload | ||
title="Drag and drop an ICO or PNG/JPEG file here, or click to select a file" | ||
accept=".ico,.png,.jpg" | ||
paste-image | ||
@file-upload="onFileUploaded" | ||
/> | ||
</div> | ||
</div> | ||
|
||
<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> |
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 { PictureInPicture } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'ICO/PNG converter', | ||
path: '/ico-converter', | ||
description: 'Convert from PNG/JPEG to/from ICO', | ||
keywords: ['ico', 'png', 'jpeg', 'icon', 'converter'], | ||
component: () => import('./ico-converter.vue'), | ||
icon: PictureInPicture, | ||
createdAt: new Date('2024-08-15'), | ||
}); |
Oops, something went wrong.