Skip to content

Commit 90d5e13

Browse files
authored
Merge pull request #44 from gambitph/fix/43-metadata-fix-on-scale
fix: get lesser between wp threshold and user max dimension
2 parents 9cea982 + b99dff7 commit 90d5e13

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/admin/class-admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function register_settings() {
118118
'show_in_rest' => [
119119
'schema' => [
120120
'type' => 'object',
121+
'additionalProperties' => true, // If we upgrade or downgrade, the settings can possibly show as null. Prevent this.
121122
'properties' => [
122123
// General settings
123124
'optimize_all_media' => [

src/admin/class-script-loader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public static function enqueue_cimo_assets() {
7777
// Get current settings
7878
$settings = get_option( 'cimo_options', [] );
7979

80+
// Expose threshold for WP Core's big image scaling.
81+
// Can be false when auto scaling is disabled.
82+
$threshold = apply_filters( 'big_image_size_threshold', 2560 );
83+
8084
// Localize script with REST API URL, nonce, and settings
8185
wp_localize_script(
8286
'cimo-script',
@@ -100,6 +104,7 @@ public static function enqueue_cimo_assets() {
100104
'svgUpload' => isset( $settings['svg_upload'] ) ? (int) $settings['svg_upload'] : 0,
101105
'svgOptimizationEnabled' => isset( $settings['svg_optimization_enabled'] ) ? (int) $settings['svg_optimization_enabled'] : 1,
102106
'stealthModeEnabled' => isset( $settings['stealth_mode_enabled'] ) ? (int) $settings['stealth_mode_enabled'] : 0,
107+
'wpScalingThreshold' => $threshold,
103108
]
104109
);
105110

src/admin/js/page/admin-settings.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,15 @@ const AdminSettings = () => {
591591
label={ __( 'Maximum Image Dimension', 'cimo-image-optimizer' ) }
592592
type="number"
593593
value={ settings.maxImageDimension }
594+
placeholder={ settings.disableWpScaling === 1 ? '2560' : undefined }
594595
onChange={ value => handleInputChange( 'maxImageDimension', value ) }
595-
help={ __( 'Maximum width or height in pixels for uploaded images. Images exceeding this dimension will be automatically resized while preserving aspect ratio. Leave empty to disable resizing. We recommend a value of 1920px.', 'cimo-image-optimizer' ) }
596+
help={ sprintf(
597+
__( 'Maximum width or height in pixels for uploaded images. Images exceeding this dimension will be automatically resized while preserving aspect ratio. Leave empty to %s. We recommend a value of 1920px.', 'cimo-image-optimizer' ),
598+
window.cimoSettings?.wpScalingThreshold
599+
? sprintf( __( "use WordPress's default auto-scaling at %spx", 'cimo-image-optimizer' ), window.cimoSettings.wpScalingThreshold )
600+
: __( 'disable auto-scaling', 'cimo-image-optimizer' )
601+
) }
602+
596603
__next40pxDefaultSize
597604
/>
598605
</div>

src/shared/converters/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export const getFileConverter = _file => {
3838
}
3939

4040
if ( file.type.startsWith( 'image/' ) ) {
41+
const max = parseFloat( window.cimoSettings?.maxImageDimension || 0 )
42+
const wp = parseFloat( window.cimoSettings?.wpScalingThreshold || 0 )
43+
44+
// Determine the final max dimension to use for conversion,
45+
// prioritizing the lower of the two thresholds if both are set.
46+
// 0 means no max dimension.
47+
const finalMaxDimension = max && wp
48+
? Math.min( max, wp )
49+
: max || wp || 0
50+
4151
// If the browser doesn't support webp, then we can't convert it.
4252
if ( ! isFormatSupported( 'webp' ) ) {
4353
return new NullConverter( file )
@@ -46,7 +56,7 @@ export const getFileConverter = _file => {
4656
return new ImageConverter( file, {
4757
format: 'webp',
4858
quality: window.cimoSettings?.webpQuality || 0.8,
49-
maxDimension: window.cimoSettings?.maxImageDimension || 0,
59+
maxDimension: finalMaxDimension,
5060
} )
5161
}
5262
}

0 commit comments

Comments
 (0)