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:
2026-09-15 15:10:43 -07:00
parent 524860df1f
commit 3a1433b71e
16 changed files with 655 additions and 42 deletions
+38
View File
@@ -89,6 +89,34 @@
// ------------------------------------------------------------------
var photoUploading = false;
function applyPhotoCropStyle() {
var previewImg = document.getElementById('photo-preview');
var fx = document.getElementById('photo_focal_x').value;
var fy = document.getElementById('photo_focal_y').value;
var zoom = document.getElementById('photo_zoom').value;
previewImg.style.cssText =
'object-position:' + fx + '% ' + fy + '%;transform:scale(' + zoom + ');transform-origin:' + fx + '% ' + fy + '%;';
}
var repositionBtn = document.getElementById('photo-reposition');
if (repositionBtn) {
repositionBtn.addEventListener('click', function () {
var 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();
}
});
});
}
if (photoInput) {
photoInput.addEventListener('change', async function () {
if (!this.files || !this.files[0]) return;
@@ -115,6 +143,16 @@
uploadStatus.textContent = 'Upload failed: ' + data.error;
} else {
document.getElementById('photo_path').value = data.path;
// A newly uploaded photo starts centered/unzoomed.
document.getElementById('photo_focal_x').value = 50;
document.getElementById('photo_focal_y').value = 50;
document.getElementById('photo_zoom').value = 1;
var previewImg = document.getElementById('photo-preview');
var previewWrap = document.querySelector('#field-photo .photo-preview-wrap');
previewImg.src = '/' + data.path.replace(/^\//, '');
applyPhotoCropStyle();
previewWrap.style.display = '';
document.getElementById('photo-reposition').style.display = '';
// Clear the file input so the file is not re-sent with the main form
photoInput.value = '';
uploadStatus.className = 'upload-status success';