handleInputChange( 'webpQuality', value || '' ) }
- min="1"
- max="100"
- step="1"
- __next40pxDefaultSize
- allowReset
- initialPosition={ 80 }
- help={ __( 'Set the quality / compression level for generated .webp images. Default is 80%. Higher values mean better quality and larger file size; lower values reduce file size with more compression but lower quality.', 'cimo-image-optimizer' ) }
+ { /* Smart Optimization */ }
+
+
+ { __( 'Smart Optimization', 'cimo-image-optimizer' ) }
+ { buildType === 'free' && (
+
+ { __( 'Premium', 'cimo-image-optimizer' ) }
+
+ ) }
+
+ }
+ checked={ buildType === 'free' ? false : ( settings.smartOptimization === 1 ) }
+ disabled={ buildType === 'free' }
+ onChange={ checked => handleInputChange( 'smartOptimization', checked ? 1 : 0 ) }
+ help={ __( 'Smart Optimization uses our advanced algorithms to choose the best compression and quality settings per image. This adds a small overhead to the upload process, but the results are even smaller file sizes and faster loading times.', 'cimo-image-optimizer' ) }
/>
+
+ { /* WebP Image Quality */ }
+ { ( buildType === 'free' || settings.smartOptimization === 0 ) && (
+
+ handleInputChange( 'webpQuality', value || '' ) }
+ min="1"
+ max="100"
+ step="1"
+ __next40pxDefaultSize
+ allowReset
+ initialPosition={ 80 }
+ help={ __( 'Set the quality / compression level for generated .webp images. Default is 80%. Higher values mean better quality and larger file size; lower values reduce file size with more compression but lower quality.', 'cimo-image-optimizer' ) }
+ />
+
+ ) }
+
{ /* Maximum Image Dimension */ }
} Promise resolving with the converted file and optional metadata.
+ */
+ async optimize() {
+ return await this.convert()
+ }
+
/**
* Cancel the current conversion.
* Subclasses should override this to implement actual cancellation logic.
diff --git a/src/shared/converters/image-converter.js b/src/shared/converters/image-converter.js
index 13a838f..04d5839 100644
--- a/src/shared/converters/image-converter.js
+++ b/src/shared/converters/image-converter.js
@@ -215,11 +215,10 @@ class ImageConverter extends Converter {
/**
* Convert an image file to the desired format and options.
- * @param {boolean} [force=false] - Force conversion even if the file is already in the desired format.
- * @param {Object} [options] - Options for the conversion.
+ * @param {Object} [options] - Options for the conversion.
* @return {Promise<{file: File|Blob, metadata?: Object}>} Promise resolving to the converted file and optional metadata.
*/
- async convert( force = false, options = {} ) {
+ async convert( options = {} ) {
const file = this.file
const {
quality = this.options?.quality || 0.8,
@@ -239,15 +238,7 @@ class ImageConverter extends Converter {
}
}
- // Skip if already the desired format
const formatInfo = supportedFormats.find( f => f.value === formatTo )
- if ( ! force && formatInfo && file.type === formatInfo.mimeType ) {
- return {
- file,
- metadata: null,
- reason: 'same-format',
- }
- }
// Check if the browser supports the desired output format
const testCanvas = document.createElement( 'canvas' )
@@ -359,7 +350,31 @@ class ImageConverter extends Converter {
}
async optimize() {
- return await applyFiltersAsync( 'cimo.imageConverter.optimize', this )
+ const smartOptimization =
+ Boolean( window.cimoSettings?.isPremium ) &&
+ String( window.cimoSettings?.smartOptimization ?? '1' ) !== '0'
+ let result = null
+
+ if ( smartOptimization ) {
+ result = await applyFiltersAsync( 'cimo.imageConverter.optimize', {
+ file: this.file,
+ metadata: null,
+ reason: 'no-optimizer',
+ }, this )
+ }
+
+ if ( ! result || result.reason === 'no-optimizer' ) {
+ result = await this.convert()
+ } else if ( result.metadata && typeof result.metadata === 'object' ) {
+ result = {
+ ...result,
+ metadata: {
+ ...result.metadata,
+ smartOptimized: 1,
+ },
+ }
+ }
+ return result
}
}
diff --git a/src/shared/converters/index.js b/src/shared/converters/index.js
index 7a58c50..74fa862 100644
--- a/src/shared/converters/index.js
+++ b/src/shared/converters/index.js
@@ -38,13 +38,16 @@ export const getFileConverter = _file => {
}
if ( file.type.startsWith( 'image/' ) ) {
- // If the browser doesn't support webp, then we can't convert it.
- if ( ! isFormatSupported( 'webp' ) ) {
- return new NullConverter( file )
+ let format = 'webp'
+
+ // If webp is not supported, use the same format of the file.
+ if ( ! isFormatSupported( format ) ) {
+ format = file.type
}
+
if ( ImageConverter.supportsMimeType( file.type ) ) {
return new ImageConverter( file, {
- format: 'webp',
+ format,
quality: window.cimoSettings?.webpQuality || 0.8,
maxDimension: window.cimoSettings?.maxImageDimension || 0,
} )