Add drag-to-pan + zoom photo repositioning for card/avatar crops
Every photo upload (session setup, novena group, Rosary Builder title photo) gets shown two ways: full-size on the presentation cover slide (unaffected, stays untouched), and cropped to a fixed box everywhere else — home page cards, profile cards, the novena day-picker's circular hero photo, and each admin form's own preview thumbnail. All of those crops used to just take the image's dead center, with no way to control what part of the photo that was — cropping out people's heads on portrait photos. - New sessions/novena_groups columns: photo_focal_x, photo_focal_y (0-100%), photo_zoom (1-3x), defaulting to 50/50/1 — today's exact centered/ unzoomed behavior, so this is fully backward compatible until someone actively repositions a photo. - New assets/js/photo-crop.js: a reusable drag-to-pan + zoom modal editor. The crop frame renders with the *exact* CSS recipe used at final render time (object-position + transform:scale/transform-origin), so the editor is a truthful live preview, not an approximation. A reference thumbnail shows the full photo dimmed outside a rectangle marking the current crop. All math reads actual rendered box dimensions rather than assuming fixed pixel sizes, so it holds up responsively at any viewport width — caught and fixed a real mismatch bug here by testing the widget standalone in a browser before wiring it into any PHP form. - New includes/photo.php: photo_crop_style() builds the inline style="..." from a session/group row, used everywhere a crop is displayed. - Wired into all three upload locations (admin/setup.php, admin/novena_group.php, admin/builder.php) with a "Reposition" button, and persisted through api/save_session.php, admin/novena_group.php's save handler, and api/builder_session.php. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -97,6 +97,79 @@
|
||||
|
||||
// Save session
|
||||
document.getElementById('btn-save').addEventListener('click', saveSession);
|
||||
|
||||
// Title page photo upload
|
||||
document.getElementById('photo-file').addEventListener('change', uploadPhoto);
|
||||
document.getElementById('photo-reposition').addEventListener('click', repositionPhoto);
|
||||
}
|
||||
|
||||
/* ─────────────────────────────────────────────────────────
|
||||
Title page photo
|
||||
───────────────────────────────────────────────────────── */
|
||||
function applyPhotoCropStyle() {
|
||||
const previewImg = document.getElementById('photo-preview');
|
||||
const fx = document.getElementById('photo-focal-x').value;
|
||||
const fy = document.getElementById('photo-focal-y').value;
|
||||
const zoom = document.getElementById('photo-zoom').value;
|
||||
previewImg.style.cssText =
|
||||
'object-position:' + fx + '% ' + fy + '%;transform:scale(' + zoom + ');transform-origin:' + fx + '% ' + fy + '%;';
|
||||
}
|
||||
|
||||
function uploadPhoto() {
|
||||
const fileInput = document.getElementById('photo-file');
|
||||
const photoHidden = document.getElementById('photo-path');
|
||||
const focalXHidden = document.getElementById('photo-focal-x');
|
||||
const focalYHidden = document.getElementById('photo-focal-y');
|
||||
const zoomHidden = document.getElementById('photo-zoom');
|
||||
const photoStatus = document.getElementById('photo-status');
|
||||
const previewWrap = document.querySelector('#photo-group .photo-preview-wrap');
|
||||
const previewImg = document.getElementById('photo-preview');
|
||||
const repositionBtn = document.getElementById('photo-reposition');
|
||||
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('photo', file);
|
||||
fd.append('csrf_token', CSRF_TOKEN);
|
||||
photoStatus.textContent = 'Uploading…';
|
||||
|
||||
fetch(BASE_URL + '/api/upload_photo.php', { method: 'POST', body: fd })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (data) {
|
||||
if (data.path) {
|
||||
photoHidden.value = data.path;
|
||||
// A newly uploaded photo starts centered/unzoomed — it's
|
||||
// a different photo than whatever crop was set before.
|
||||
focalXHidden.value = 50;
|
||||
focalYHidden.value = 50;
|
||||
zoomHidden.value = 1;
|
||||
previewImg.src = '/' + data.path.replace(/^\//, '');
|
||||
applyPhotoCropStyle();
|
||||
previewWrap.style.display = '';
|
||||
repositionBtn.style.display = '';
|
||||
photoStatus.textContent = 'Photo ready.';
|
||||
} else {
|
||||
photoStatus.textContent = 'Upload failed: ' + (data.error || 'unknown error');
|
||||
}
|
||||
})
|
||||
.catch(function () { photoStatus.textContent = 'Upload failed.'; });
|
||||
}
|
||||
|
||||
function repositionPhoto() {
|
||||
const previewImg = document.getElementById('photo-preview');
|
||||
PhotoCrop.open({
|
||||
imageUrl: previewImg.src,
|
||||
focalX: parseFloat(document.getElementById('photo-focal-x').value),
|
||||
focalY: parseFloat(document.getElementById('photo-focal-y').value),
|
||||
zoom: parseFloat(document.getElementById('photo-zoom').value),
|
||||
onApply: function (fx, fy, zoom) {
|
||||
document.getElementById('photo-focal-x').value = fx;
|
||||
document.getElementById('photo-focal-y').value = fy;
|
||||
document.getElementById('photo-zoom').value = zoom;
|
||||
applyPhotoCropStyle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* ─────────────────────────────────────────────────────────
|
||||
@@ -453,6 +526,10 @@
|
||||
subject_name: document.getElementById('subject-name').value.trim(),
|
||||
subject_pronoun: document.getElementById('subject-pronoun').value,
|
||||
subject_dates: document.getElementById('subject-dates').value.trim(),
|
||||
photo_path: document.getElementById('photo-path').value.trim(),
|
||||
photo_focal_x: parseFloat(document.getElementById('photo-focal-x').value) || 50,
|
||||
photo_focal_y: parseFloat(document.getElementById('photo-focal-y').value) || 50,
|
||||
photo_zoom: parseFloat(document.getElementById('photo-zoom').value) || 1,
|
||||
steps: STEPS.map(s => s.step_type === 'bead'
|
||||
? { step_type: 'bead', bead_type: s.bead_type }
|
||||
: { step_type: 'prayer', prayer_id: s.prayer_id, attribution: s.attribution, bead_type: s.bead_type || null }),
|
||||
|
||||
Reference in New Issue
Block a user