Add image cropping to the shared upload field
ImageUploadField now opens a crop overlay (react-easy-crop) before uploading a newly selected file, and offers a "Zuschneiden" button to re-crop an already-set image without re-uploading. Cropping happens entirely client-side via canvas; the resulting blob is sent through the existing /uploads endpoint unchanged. Wired a fixed aspect ratio into the fields whose images are displayed at a fixed ratio elsewhere on the site (gallery project/brand covers and service images at 4:3, matching their public aspect-[4/3] cards; the hero image at 16:9). Logo fields (brands, car models, site logo) stay unset, defaulting to the source image's own aspect ratio so they aren't forced into an unrelated shape. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
9f5919eb45
commit
48a2b5f7a6
12 changed files with 336 additions and 67 deletions
20
hifi-src/package-lock.json
generated
20
hifi-src/package-lock.json
generated
|
|
@ -19,6 +19,7 @@
|
|||
"qrcode": "^1.5.4",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"react-easy-crop": "^6.2.3",
|
||||
"react-router-dom": "^6.26.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
@ -2642,6 +2643,12 @@
|
|||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/normalize-wheel": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/normalize-wheel/-/normalize-wheel-1.0.1.tgz",
|
||||
"integrity": "sha512-1OnlAPZ3zgrk8B91HyRj+eVv+kS5u+Z0SCsak6Xil/kmgEia50ga7zfkumayonZrImffAxPU/5WcyGhzetHNPA==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
|
|
@ -3134,6 +3141,19 @@
|
|||
"react": "^18.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/react-easy-crop": {
|
||||
"version": "6.2.3",
|
||||
"resolved": "https://registry.npmjs.org/react-easy-crop/-/react-easy-crop-6.2.3.tgz",
|
||||
"integrity": "sha512-ebimG3OGlzizjxEZ77Cj9CVLcg5vDZ6QVz8ud1rx4WmsCDWHIROEhgMi0GpJ/jKAuwHrH0M+QZUK4hyvxbYhOA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"normalize-wheel": "^1.0.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.4.0",
|
||||
"react-dom": ">=16.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/react-refresh": {
|
||||
"version": "0.17.0",
|
||||
"resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.17.0.tgz",
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
"qrcode": "^1.5.4",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"react-easy-crop": "^6.2.3",
|
||||
"react-router-dom": "^6.26.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,76 @@
|
|||
import { useState } from 'react';
|
||||
import Cropper from 'react-easy-crop';
|
||||
import { api } from '../api/client.js';
|
||||
import { getCroppedImageBlob } from '../utils/cropImage.js';
|
||||
|
||||
export default function ImageUploadField({ value, onChange, label = 'Bild' }) {
|
||||
function extensionForMime(mime) {
|
||||
if (mime === 'image/jpeg') return 'jpg';
|
||||
if (mime === 'image/webp') return 'webp';
|
||||
return 'png';
|
||||
}
|
||||
|
||||
export default function ImageUploadField({ value, onChange, label = 'Bild', aspect }) {
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const handleFile = async (e) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
const [cropSrc, setCropSrc] = useState(null);
|
||||
const [cropMime, setCropMime] = useState('image/png');
|
||||
const [crop, setCrop] = useState({ x: 0, y: 0 });
|
||||
const [zoom, setZoom] = useState(1);
|
||||
const [croppedAreaPixels, setCroppedAreaPixels] = useState(null);
|
||||
const [cropAspect, setCropAspect] = useState(aspect || 1);
|
||||
|
||||
const openCropFromFile = (file) => {
|
||||
setCropMime(file.type || 'image/png');
|
||||
setCrop({ x: 0, y: 0 });
|
||||
setZoom(1);
|
||||
setCroppedAreaPixels(null);
|
||||
setCropAspect(aspect || 1);
|
||||
setCropSrc(URL.createObjectURL(file));
|
||||
};
|
||||
|
||||
const handleFile = (e) => {
|
||||
const file = e.target.files?.[0];
|
||||
e.target.value = '';
|
||||
if (!file) return;
|
||||
setError('');
|
||||
openCropFromFile(file);
|
||||
};
|
||||
|
||||
const openCropFromExisting = async () => {
|
||||
if (!value) return;
|
||||
setError('');
|
||||
try {
|
||||
const res = await fetch(value);
|
||||
const blob = await res.blob();
|
||||
openCropFromFile(blob);
|
||||
} catch {
|
||||
setError('Bild konnte nicht zum Zuschneiden geladen werden.');
|
||||
}
|
||||
};
|
||||
|
||||
const onMediaLoaded = ({ width, height }) => {
|
||||
if (!aspect && width && height) {
|
||||
setCropAspect(width / height);
|
||||
}
|
||||
};
|
||||
|
||||
const closeCrop = () => {
|
||||
if (cropSrc) URL.revokeObjectURL(cropSrc);
|
||||
setCropSrc(null);
|
||||
};
|
||||
|
||||
const confirmCrop = async () => {
|
||||
if (!cropSrc || !croppedAreaPixels) return;
|
||||
setUploading(true);
|
||||
setError('');
|
||||
try {
|
||||
const blob = await getCroppedImageBlob(cropSrc, croppedAreaPixels, cropMime);
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('file', blob, `zugeschnitten.${extensionForMime(cropMime)}`);
|
||||
const result = await api.post('/uploads', formData);
|
||||
onChange(result.path);
|
||||
closeCrop();
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
|
|
@ -42,9 +97,66 @@ export default function ImageUploadField({ value, onChange, label = 'Bild' }) {
|
|||
</div>
|
||||
)}
|
||||
<input type="file" accept="image/png,image/jpeg,image/webp" onChange={handleFile} className="text-sm" />
|
||||
{value && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={openCropFromExisting}
|
||||
className="rounded-md border border-neutral-300 px-2 py-1 text-xs font-medium text-neutral-600 hover:bg-neutral-50 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800"
|
||||
>
|
||||
Zuschneiden
|
||||
</button>
|
||||
)}
|
||||
{uploading && <span className="text-sm text-neutral-500">Lädt hoch…</span>}
|
||||
</div>
|
||||
{error && <p className="mt-1 text-sm text-red-600">{error}</p>}
|
||||
|
||||
{cropSrc && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 p-4">
|
||||
<div className="w-full max-w-lg rounded-xl bg-white p-4 dark:bg-neutral-900">
|
||||
<p className="mb-3 text-sm font-semibold text-neutral-900 dark:text-white">Bild zuschneiden</p>
|
||||
<div className="relative h-80 w-full overflow-hidden rounded-md bg-neutral-800 sm:h-96">
|
||||
<Cropper
|
||||
image={cropSrc}
|
||||
crop={crop}
|
||||
zoom={zoom}
|
||||
aspect={cropAspect}
|
||||
onCropChange={setCrop}
|
||||
onZoomChange={setZoom}
|
||||
onCropComplete={(_area, areaPixels) => setCroppedAreaPixels(areaPixels)}
|
||||
onMediaLoaded={onMediaLoaded}
|
||||
/>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
min={1}
|
||||
max={3}
|
||||
step={0.01}
|
||||
value={zoom}
|
||||
onChange={(e) => setZoom(Number(e.target.value))}
|
||||
className="mt-4 w-full"
|
||||
/>
|
||||
{error && <p className="mt-2 text-sm text-red-600">{error}</p>}
|
||||
<div className="mt-4 flex justify-end gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={closeCrop}
|
||||
disabled={uploading}
|
||||
className="rounded-md border border-neutral-300 px-4 py-2 text-sm dark:border-neutral-700"
|
||||
>
|
||||
Abbrechen
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={confirmCrop}
|
||||
disabled={uploading || !croppedAreaPixels}
|
||||
className="rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600 disabled:opacity-50"
|
||||
>
|
||||
{uploading ? 'Lädt hoch…' : 'Übernehmen'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,6 +107,7 @@ export default function GalleryBrands() {
|
|||
<ImageUploadField
|
||||
value={form.cover_image_path}
|
||||
onChange={(path) => setForm({ ...form, cover_image_path: path })}
|
||||
aspect={4 / 3}
|
||||
label="Titelbild (optional)"
|
||||
/>
|
||||
<div>
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ export default function GalleryProjects() {
|
|||
<ImageUploadField
|
||||
value={form.cover_image_path}
|
||||
onChange={(path) => setForm({ ...form, cover_image_path: path })}
|
||||
aspect={4 / 3}
|
||||
label="Titelbild (optional)"
|
||||
/>
|
||||
<div>
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ export default function Services() {
|
|||
/>
|
||||
</div>
|
||||
|
||||
<ImageUploadField value={form.image_path} onChange={(path) => setForm({ ...form, image_path: path })} label="Bild" />
|
||||
<ImageUploadField value={form.image_path} onChange={(path) => setForm({ ...form, image_path: path })} aspect={4 / 3} label="Bild" />
|
||||
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Button-Text (optional)</label>
|
||||
|
|
|
|||
|
|
@ -245,6 +245,7 @@ export default function WebsiteSettings() {
|
|||
value={form.hero_image_path}
|
||||
onChange={(path) => update('hero_image_path', path)}
|
||||
label="Bild"
|
||||
aspect={16 / 9}
|
||||
/>
|
||||
</section>
|
||||
|
||||
|
|
|
|||
39
hifi-src/src/utils/cropImage.js
Normal file
39
hifi-src/src/utils/cropImage.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
function loadImage(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const image = new Image();
|
||||
image.addEventListener('load', () => resolve(image));
|
||||
image.addEventListener('error', (err) => reject(err));
|
||||
image.crossOrigin = 'anonymous';
|
||||
image.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getCroppedImageBlob(imageSrc, croppedAreaPixels, mimeType = 'image/png') {
|
||||
const image = await loadImage(imageSrc);
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = Math.round(croppedAreaPixels.width);
|
||||
canvas.height = Math.round(croppedAreaPixels.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
ctx.drawImage(
|
||||
image,
|
||||
croppedAreaPixels.x,
|
||||
croppedAreaPixels.y,
|
||||
croppedAreaPixels.width,
|
||||
croppedAreaPixels.height,
|
||||
0,
|
||||
0,
|
||||
canvas.width,
|
||||
canvas.height
|
||||
);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
canvas.toBlob((blob) => {
|
||||
if (!blob) {
|
||||
reject(new Error('Zuschneiden fehlgeschlagen'));
|
||||
return;
|
||||
}
|
||||
resolve(blob);
|
||||
}, mimeType);
|
||||
});
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
1
hifi/assets/index-D_qjU0tn.css
Normal file
1
hifi/assets/index-D_qjU0tn.css
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -43,9 +43,9 @@
|
|||
"sameAs": ["https://www.youtube.com/@hifiplanet2812"]
|
||||
}
|
||||
</script>
|
||||
<script type="module" crossorigin src="/assets/index-CWyHp-JT.js"></script>
|
||||
<script type="module" crossorigin src="/assets/index-CKoRo60o.js"></script>
|
||||
<link rel="modulepreload" crossorigin href="/assets/lucide-icons-Cg2IxtAO.js">
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-B1rtregG.css">
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-D_qjU0tn.css">
|
||||
</head>
|
||||
<body class="bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100">
|
||||
<div id="root"></div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue