Skip to content

Commit f0134da

Browse files
fix: improve responsive options for image cropping
1 parent 7b2275a commit f0134da

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

js/file-upload.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ function ppom_set_croppie_options(file_name, viewport, image_id) {
344344
option = get_responsive_croppie_options(option, file_list_preview_containers[file_name]['container_width']);
345345
// console.log($filelist_DIV[file_name]['croppie'][image_id]);
346346
file_list_preview_containers[file_name]['croppie'][image_id].croppie(option);
347-
file_list_preview_containers[file_name]['croppie'][image_id].find('.ppom-cropping-size').css('width', '100%');
348347
}
349348
});
350349
}
@@ -746,31 +745,45 @@ function ppom_generate_cropper_data_for_cart(field_name) {
746745
function get_responsive_croppie_options(baseOptions, max_width) {
747746
const boundary = baseOptions.boundary || {};
748747

749-
if ( boundary.width && boundary.height && max_width >= boundary.width ) {
748+
const boundaryWidthOriginal = Number(boundary.width);
749+
const boundaryHeightOriginal = Number(boundary.height);
750+
if ( !Number.isFinite(boundaryWidthOriginal) || !Number.isFinite(boundaryHeightOriginal) || boundaryWidthOriginal <= 0 || boundaryHeightOriginal <= 0 ) {
751+
return baseOptions;
752+
}
753+
if ( max_width >= boundaryWidthOriginal ) {
754+
return baseOptions;
755+
}
756+
if ( !Number.isFinite(max_width) ) {
750757
return baseOptions;
751758
}
752-
753759
// Use container width as the boundary.
754-
const aspectRatio = boundary.height / boundary.width || 1; // original height/width ratio
760+
const aspectRatio = boundaryHeightOriginal / boundaryWidthOriginal || 1; // original height/width ratio
755761
const boundaryWidth = Math.floor(max_width);
756762
const boundaryHeight = Math.floor(boundaryWidth * aspectRatio);
757763

758-
if ( baseOptions.viewport && baseOptions.viewport.width && baseOptions.viewport.height ) {
759-
const scale = boundaryWidth / boundary.width;
760-
const viewportWidth = Math.floor(baseOptions.viewport.width * scale);
761-
const viewportHeight = Math.floor(baseOptions.viewport.height * scale);
762-
763-
// Scale viewport proportionally and ensure it fits within the new boundary.
764-
baseOptions.viewport.width = Math.min(viewportWidth, boundaryWidth - 2);
765-
baseOptions.viewport.height = Math.min(viewportHeight, boundaryHeight - 2);
766-
}
767-
768-
return {
764+
// Build the result as a new object so baseOptions (and its nested viewport,
765+
// which is a live reference into ppom_file_vars.croppie_options) is never mutated.
766+
const result = {
769767
...baseOptions,
770768
boundary: {
771769
...baseOptions.boundary,
772770
width: boundaryWidth,
773771
height: boundaryHeight,
774772
},
775773
};
774+
775+
if ( baseOptions.viewport && baseOptions.viewport.width && baseOptions.viewport.height ) {
776+
const scale = boundaryWidth / boundaryWidthOriginal;
777+
const viewportWidth = Math.floor(baseOptions.viewport.width * scale);
778+
const viewportHeight = Math.floor(baseOptions.viewport.height * scale);
779+
780+
// Scale viewport proportionally and ensure it fits within the new boundary.
781+
result.viewport = {
782+
...baseOptions.viewport,
783+
width: Math.min(viewportWidth, boundaryWidth - 2),
784+
height: Math.min(viewportHeight, boundaryHeight - 2),
785+
};
786+
}
787+
788+
return result;
776789
}

templates/frontend/inputs/cropper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class="btn btn-primary <?php echo esc_attr( $btn_class ); ?>"
6363
<?php
6464
if ( $options && count( $options ) > 0 ) {
6565

66-
$croppie_options = ppom_get_croppie_options( $field_meta );
67-
6866
$select_css = 'width:100%;';
6967
$select_css .= 'margin:5px auto;display:none;';
7068

0 commit comments

Comments
 (0)