auto optimize wp media advacements needed...
need media settings to ability to disable generated wodpress default generation sizes show the list and show checkboxes enabled as default users can uncheck and disable it easily.
* WebP Converter
*
* Automatically converts uploaded images (JPEG, PNG, GIF, HEIC) to optimized WebP format.
* This snippet is perfect for handling large image uploads - it intelligently resizes images
* to your specified dimensions and converts them to WebP for maximum compression while
* maintaining quality. Original files are replaced with optimized versions to save server space.
*
* Features:
* - Automatic WebP conversion with size comparison (only converts if WebP is smaller)
* - Configurable max width/height with aspect ratio preservation
* - Adjustable compression quality (1-100)
* - EXIF orientation correction (fixes rotated images)
* - Replaces originals with optimized files (saves storage space)
* - Admin settings panel in Media options
* - Smart fallback (keeps original if WebP conversion fails or is larger)
*/
add_action('init', 'snn_plugin_init');
function snn_plugin_init() {
add_action('admin_notices', 'snn_admin_notices');
add_action('admin_init', 'snn_register_settings');
add_filter('wp_handle_upload', 'snn_handle_upload_convert_to_webp');
}
/**
* Display admin notices for Imagick extension and WebP conversion settings.
*/
function snn_admin_notices() {
$screen = get_current_screen();
if ('upload' === $screen->id || 'media' === $screen->id) {
if (!extension_loaded('imagick')) {
echo "<div class='notice notice-warning is-dismissible'><p><strong>Warning:</strong> The Imagick PHP extension required for WebP conversion is not installed or enabled. Please install or enable Imagick.</p></div>";
} else {
$settings_url = admin_url('options-media.php');
$webp_conversion_enabled = get_option('snn_convert_to_webp', false);
if ($webp_conversion_enabled) {
echo "<div class='notice notice-info is-dismissible'><p><strong>Enabled:</strong> WebP image conversion is active. You can disable this feature on the <a href='" . esc_url($settings_url) . "'>Settings > Media</a> page.</p></div>";
} else {
echo "<div class='notice notice-info is-dismissible'><p><strong>Disabled:</strong> WebP image conversion is not active. You can enable this feature on the <a href='" . esc_url($settings_url) . "'>Settings > Media</a> page.</p></div>";
}
}
}
}
/**
* Register settings for WebP conversion and image processing.
*/
function snn_register_settings() {
register_setting('media', 'snn_convert_to_webp', [
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'default' => false,
]);
register_setting('media', 'snn_max_width', [
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 1920,
]);
register_setting('media', 'snn_max_height', [
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 1080,
]);
register_setting('media', 'snn_compression_quality', [
'type' => 'integer',
'sanitize_callback' => function($value) {
return max(1, min(100, absint($value)));
},
'default' => 80,
]);
add_settings_field(
'snn_convert_to_webp',
__('Convert Uploaded Images to WebP', 'snn'),
'snn_field_callback',
'media',
'default',
['label_for' => 'snn_convert_to_webp']
);
add_settings_field(
'snn_image_settings',
__('Image Processing Settings', 'snn'),
'snn_image_settings_callback',
'media',
'default'
);
}
/**
* Display the image processing settings fields.
*/
function snn_image_settings_callback() {
$maxWidth = get_option('snn_max_width', 1920);
$maxHeight = get_option('snn_max_height', 1080);
$compressionQuality = get_option('snn_compression_quality', 80);
echo '<fieldset>';
echo '<p><label for="snn_max_width">Max Width (pixels):</label><br>';
echo '<input type="number" id="snn_max_width" name="snn_max_width" value="' . esc_attr($maxWidth) . '" min="1" max="9999" /></p>';
echo '<p><label for="snn_max_height">Max Height (pixels):</label><br>';
echo '<input type="number" id="snn_max_height" name="snn_max_height" value="' . esc_attr($maxHeight) . '" min="1" max="9999" /></p>';
echo '<p><label for="snn_compression_quality">Compression Quality (1-100):</label><br>';
echo '<input type="number" id="snn_compression_quality" name="snn_compression_quality" value="' . esc_attr($compressionQuality) . '" min="1" max="100" /></p>';
echo '</fieldset>';
}
/**
* Display the WebP conversion setting field.
*/
function snn_field_callback() {
$value = get_option('snn_convert_to_webp', false);
echo '<input type="checkbox" id="snn_convert_to_webp" name="snn_convert_to_webp" ' . checked(1, $value, false) . ' value="1">';
echo '<label for="snn_convert_to_webp">' . esc_html__('Enable automatic conversion of uploaded images to WebP format.', 'snn') . '</label>';
}
/**
* Handle the image upload and convert to WebP format.
*
* @param array $upload Array containing the upload data.
* @return array Modified upload data.
*/
function snn_handle_upload_convert_to_webp($upload) {
if (!get_option('snn_convert_to_webp')) {
return $upload; // Skip the conversion if not enabled
}
$maxWidth = get_option('snn_max_width', 1920);
$maxHeight = get_option('snn_max_height', 1080);
$compressionQuality = get_option('snn_compression_quality', 80);
$valid_types = ['image/jpeg', 'image/png', 'image/gif', 'image/heic'];
if (in_array($upload['type'], $valid_types)) {
$file_path = $upload['file'];
if (extension_loaded('imagick')) {
try {
$imagick = new Imagick($file_path);
// Adjust orientation based on EXIF data
switch ($imagick->getImageOrientation()) {
case Imagick::ORIENTATION_BOTTOMRIGHT:
$imagick->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$imagick->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$imagick->rotateImage("#000", -90);
break;
}
$imagick->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
$originalWidth = $imagick->getImageWidth();
$originalHeight = $imagick->getImageHeight();
$aspectRatio = $originalWidth / $originalHeight;
$newWidth = $originalWidth;
$newHeight = $originalHeight;
if ($originalWidth > $maxWidth || $originalHeight > $maxHeight) {
if ($newWidth > $maxWidth) {
$newWidth = $maxWidth;
$newHeight = $newWidth / $aspectRatio;
}
if ($newHeight > $maxHeight) {
$newHeight = $maxHeight;
$newWidth = $newHeight * $aspectRatio;
}
}
$imagick->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);
$imagick->setImageFormat('webp');
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality($compressionQuality);
$file_info = pathinfo($file_path);
$dirname = $file_info['dirname'];
$filename = $file_info['filename'];
$new_file_path = $dirname . '/' . $filename . '.webp';
$imagick->writeImage($new_file_path);
// Compare file sizes
$original_size = filesize($file_path);
$new_size = filesize($new_file_path);
if ($new_size < $original_size) {
if (file_exists($new_file_path)) {
$upload['file'] = $new_file_path;
$upload['url'] = str_replace(basename($upload['url']), basename($new_file_path), $upload['url']);
$upload['type'] = 'image/webp';
@unlink($file_path);
}
} else {
@unlink($new_file_path);
}
$imagick->clear();
$imagick->destroy();
} catch (Exception $e) {
// Log error or handle gracefully
error_log('WebP conversion failed: ' . $e->getMessage());
}
} else {
// Only show this message to admins in debug mode
if (defined('WP_DEBUG') && WP_DEBUG && current_user_can('manage_options')) {
add_action('admin_notices', function() {
echo '<div class="notice notice-error"><p>Imagick is not installed. WebP conversion cannot be performed.</p></div>';
});
}
}
}
return $upload;
}
auto optimize wp media advacements needed...
need media settings to ability to disable generated wodpress default generation sizes show the list and show checkboxes enabled as default users can uncheck and disable it easily.
hmm 🧠🧠