mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 21:37:11 -04:00
parent
b430baef40
commit
c8dbec756b
4 changed files with 128 additions and 0 deletions
34
src/tools/image-to-css/image-to-css.service.ts
Normal file
34
src/tools/image-to-css/image-to-css.service.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { stringToUrl } from 'svg-to-url';
|
||||
|
||||
export type CSSType = 'Background' | 'Border' | 'ListItemBullet' | 'Url';
|
||||
|
||||
function fileToDataUrl(file: File) {
|
||||
return new Promise<string>((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})`;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue