Error: Image conversion to WebP failed. The original file has been kept.
';
});
}
}
return $upload;
}
add_action('admin_menu', 'zs_webp_settings_menu');
function zs_webp_settings_menu() {
add_submenu_page(
'upload.php', // Parent slug (Media menu)
'ZS WebP Settings', // Page title
'ZS WebP Settings', // Menu title
'manage_options', // Capability (admin only)
'zs-webp-settings', // Menu slug
'zs_webp_settings_page' // Callback to render page
);
}
function zs_webp_settings_page() {
if (!current_user_can('manage_options')) {
wp_die('Unauthorized user');
}
$option_key = 'zedsuite_webpconv_quality';
$default_quality = 75;
// Folder paths
$plugin_dir = plugin_dir_path(__FILE__);
$webp_dir = $plugin_dir . 'samples/'; // Your 10 images here
// Scan webp files
$webp_files = glob($webp_dir . '*.webp');
// Create option on first load
if (get_option($option_key) === false) {
add_option($option_key, $default_quality);
}
// Handle form actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['zs_webp_reset'])) {
update_option($option_key, $default_quality);
} elseif (isset($_POST['zs_webp_save']) && isset($_POST['webp_quality'])) {
$q = intval($_POST['webp_quality']);
if ($q >= 0 && $q <= 101) {
update_option($option_key, $q);
}
}
}
$quality = intval(get_option($option_key));
$plugin_data = get_plugin_data(__FILE__);
$version = esc_html($plugin_data['Version']);
?>
ZS WebP Converter Settings
Version:
This plugin automatically converts JPEG and PNG uploads to WebP format. You can adjust the quality of the output image below. Use a lower value for smaller files, or 101 for lossless compression.
Test
Comparison
WebP [ KBytes]
PNG [ KBytes]
⇄
admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('zs_webp_test_nonce'),
'quality' => intval(get_option('zedsuite_webpconv_quality', 75)),
'plugin_url' => plugin_dir_url(__FILE__),
]);
}
add_action('wp_ajax_zs_convert_png_to_webp', 'zs_ajax_convert_png_to_webp');
function zs_ajax_convert_png_to_webp() {
check_ajax_referer('zs_webp_test_nonce', 'nonce');
$basename = sanitize_file_name($_POST['basename'] ?? '');
#$quality = intval($_POST['quality'] ?? 75);
$quality = get_option('zedsuite_webpconv_quality', 75);
if ($basename === '') {
wp_send_json_error('Invalid basename');
}
$plugin_dir = plugin_dir_path(__FILE__);
$samples_dir = $plugin_dir . 'samples/';
$temp_dir = $plugin_dir . 'temp/';
$png_path = $samples_dir . $basename . '.png';
$webp_tmp_path = $temp_dir . $basename . '_temp.webp';
if (!file_exists($png_path)) {
wp_send_json_error('PNG file missing');
}
$image = imagecreatefrompng($png_path);
if (!$image) {
wp_send_json_error('Could not load PNG');
}
if ($quality === 101) {
$result = imagewebp($image, $webp_tmp_path, 100);
// Set lossless flag
// PHP GD does not natively support lossless, but imagick can.
// For simplicity, treat 101 as 100 quality here.
} else {
$result = imagewebp($image, $webp_tmp_path, $quality);
}
imagedestroy($image);
if (!$result) {
wp_send_json_error('Conversion failed');
}
$webp_url = plugin_dir_url(__FILE__) . "temp/{$basename}_temp.webp";
$png_url = plugin_dir_url(__FILE__) . "samples/{$basename}.png";
// Read NFO file content
$nfo_path = $samples_dir . $basename . '.nfo';
$source_info = '';
if (file_exists($nfo_path)) {
$source_info = trim(file_get_contents($nfo_path));
}
if (file_exists($png_path)) {
$sizeBytes = filesize($png_path); // Size in bytes
$PNGsizeKB = round($sizeBytes / 1024, 2); // Convert to KB, 2 decimal places
}
if (file_exists($webp_tmp_path)) {
$sizeBytes = filesize($webp_tmp_path); // Size in bytes
$WebPsizeKB = round($sizeBytes / 1024, 2); // Convert to KB, 2 decimal places
}
wp_send_json_success([
'webp_url' => $webp_url,
'webp_size' => $WebPsizeKB,
'png_url' => $png_url,
'png_size' => $PNGsizeKB,
'source_info' => $source_info,
]);
}