diff --git a/src/tools/image-to-css/image-to-css.service.ts b/src/tools/image-to-css/image-to-css.service.ts new file mode 100644 index 000000000..d01a2a409 --- /dev/null +++ b/src/tools/image-to-css/image-to-css.service.ts @@ -0,0 +1,34 @@ +import { stringToUrl } from 'svg-to-url'; + +export type CSSType = 'Background' | 'Border' | 'ListItemBullet' | 'Url'; + +function fileToDataUrl(file: File) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result?.toString() ?? ''); + reader.onerror = error => reject(error); + }); +} + +function svgToDataUrl(image: string) { + const getUrlFromSvgString = stringToUrl({}); + return getUrlFromSvgString(image); +} + +export async function imageToCSS( + image: File | string, + type: CSSType, +) { + const dataURI = image instanceof File ? await fileToDataUrl(image) : svgToDataUrl(image); + switch (type) { + case 'Background': + return `background-image: url(${dataURI});`; + case 'Border': + return `border-image-source: url(${dataURI});`; + case 'ListItemBullet': + return `li{\n list-style-image: ${dataURI};\n}\nli::marker{\n font-size: 1.5em;\n}'}`; + default: + return `url(${dataURI})`; + } +} diff --git a/src/tools/image-to-css/image-to-css.vue b/src/tools/image-to-css/image-to-css.vue new file mode 100644 index 000000000..2f51832ff --- /dev/null +++ b/src/tools/image-to-css/image-to-css.vue @@ -0,0 +1,80 @@ + + + + + diff --git a/src/tools/image-to-css/index.ts b/src/tools/image-to-css/index.ts new file mode 100644 index 000000000..08d20ad18 --- /dev/null +++ b/src/tools/image-to-css/index.ts @@ -0,0 +1,12 @@ +import { BrandCss3 } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Image to CSS', + path: '/image-to-css', + description: 'Convert image to CSS (url, background, ...)', + keywords: ['image', 'css'], + component: () => import('./image-to-css.vue'), + icon: BrandCss3, + createdAt: new Date('2024-05-11'), +}); diff --git a/src/tools/index.ts b/src/tools/index.ts index aa861c935..cb5adefca 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer'; import { tool as textToUnicode } from './text-to-unicode'; import { tool as safelinkDecoder } from './safelink-decoder'; +import { tool as imageToCss } from './image-to-css'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -128,6 +129,7 @@ export const toolsByCategory: ToolCategory[] = [ httpStatusCodes, jsonDiff, safelinkDecoder, + imageToCss, ], }, {