mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-26 09:29:30 +02:00
feat: add support for HEIC image conversion using heic2any
This commit is contained in:
7
package-lock.json
generated
7
package-lock.json
generated
@@ -34,6 +34,7 @@
|
|||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"fast-xml-parser": "^5.2.5",
|
"fast-xml-parser": "^5.2.5",
|
||||||
"formik": "^2.4.6",
|
"formik": "^2.4.6",
|
||||||
|
"heic2any": "^0.0.4",
|
||||||
"i18next": "^25.3.2",
|
"i18next": "^25.3.2",
|
||||||
"i18next-http-backend": "^3.0.2",
|
"i18next-http-backend": "^3.0.2",
|
||||||
"jimp": "^0.22.12",
|
"jimp": "^0.22.12",
|
||||||
@@ -7024,6 +7025,12 @@
|
|||||||
"node": ">= 0.4"
|
"node": ">= 0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/heic2any": {
|
||||||
|
"version": "0.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/heic2any/-/heic2any-0.0.4.tgz",
|
||||||
|
"integrity": "sha512-3lLnZiDELfabVH87htnRolZ2iehX9zwpRyGNz22GKXIu0fznlblf0/ftppXKNqS26dqFSeqfIBhAmAj/uSp0cA==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/hoist-non-react-statics": {
|
"node_modules/hoist-non-react-statics": {
|
||||||
"version": "3.3.2",
|
"version": "3.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz",
|
"resolved": "https://registry.npmjs.org/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz",
|
||||||
|
@@ -53,6 +53,7 @@
|
|||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"fast-xml-parser": "^5.2.5",
|
"fast-xml-parser": "^5.2.5",
|
||||||
"formik": "^2.4.6",
|
"formik": "^2.4.6",
|
||||||
|
"heic2any": "^0.0.4",
|
||||||
"i18next": "^25.3.2",
|
"i18next": "^25.3.2",
|
||||||
"i18next-http-backend": "^3.0.2",
|
"i18next-http-backend": "^3.0.2",
|
||||||
"jimp": "^0.22.12",
|
"jimp": "^0.22.12",
|
||||||
|
@@ -5,6 +5,7 @@ import ToolContent from '@components/ToolContent';
|
|||||||
import { ToolComponentProps } from '@tools/defineTool';
|
import { ToolComponentProps } from '@tools/defineTool';
|
||||||
import ToolImageInput from '@components/input/ToolImageInput';
|
import ToolImageInput from '@components/input/ToolImageInput';
|
||||||
import { removeBackground } from '@imgly/background-removal';
|
import { removeBackground } from '@imgly/background-removal';
|
||||||
|
import * as heic2any from 'heic2any';
|
||||||
|
|
||||||
const initialValues = {};
|
const initialValues = {};
|
||||||
|
|
||||||
@@ -23,8 +24,33 @@ export default function RemoveBackgroundFromImage({
|
|||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Convert the input file to a Blob URL
|
let fileToProcess = input;
|
||||||
const inputUrl = URL.createObjectURL(input);
|
// Check if the file is HEIC (by MIME type or extension)
|
||||||
|
if (
|
||||||
|
input.type === 'image/heic' ||
|
||||||
|
input.name?.toLowerCase().endsWith('.heic')
|
||||||
|
) {
|
||||||
|
// Convert HEIC to PNG using heic2any
|
||||||
|
const convertedBlob = await heic2any.default({
|
||||||
|
blob: input,
|
||||||
|
toType: 'image/png'
|
||||||
|
});
|
||||||
|
// heic2any returns a Blob or an array of Blobs
|
||||||
|
let pngBlob;
|
||||||
|
if (Array.isArray(convertedBlob)) {
|
||||||
|
pngBlob = convertedBlob[0];
|
||||||
|
} else {
|
||||||
|
pngBlob = convertedBlob;
|
||||||
|
}
|
||||||
|
fileToProcess = new File(
|
||||||
|
[pngBlob],
|
||||||
|
input.name.replace(/\.[^/.]+$/, '') + '.png',
|
||||||
|
{ type: 'image/png' }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the file to a Blob URL
|
||||||
|
const inputUrl = URL.createObjectURL(fileToProcess);
|
||||||
|
|
||||||
// Process the image with the background removal library
|
// Process the image with the background removal library
|
||||||
const blob = await removeBackground(inputUrl, {
|
const blob = await removeBackground(inputUrl, {
|
||||||
@@ -36,7 +62,7 @@ export default function RemoveBackgroundFromImage({
|
|||||||
// Create a new file from the blob
|
// Create a new file from the blob
|
||||||
const newFile = new File(
|
const newFile = new File(
|
||||||
[blob],
|
[blob],
|
||||||
input.name.replace(/\.[^/.]+$/, '') + '-no-bg.png',
|
fileToProcess.name.replace(/\.[^/.]+$/, '') + '-no-bg.png',
|
||||||
{
|
{
|
||||||
type: 'image/png'
|
type: 'image/png'
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user