[codeblock]

// আপলোড হওয়া ছবি কনভার্ট করে WebP বানানো
add_filter(‘wp_handle_upload’, ‘custom_convert_to_webp_on_upload’);

function custom_convert_to_webp_on_upload($image_data) {
$file_type = $image_data[‘type’];
$supported_types = [‘image/jpeg’, ‘image/png’];

if (in_array($file_type, $supported_types)) {
$image_path = $image_data[‘file’];
$webp_image_path = custom_convert_to_webp($image_path, $file_type);

if ($webp_image_path) {
wp_delete_file($image_path); // পুরানো ছবি ডিলিট
$image_data[‘file’] = $webp_image_path;
$image_data[‘type’] = ‘image/webp’;
$image_data[‘url’] = str_replace(basename($image_path), basename($webp_image_path), $image_data[‘url’]);
}
}

return $image_data;
}

// ইমেজ কনভার্ট করার মেইন ফাংশন
function custom_convert_to_webp($image_path, $mime_type) {
switch ($mime_type) {
case ‘image/jpeg’:
$image = imagecreatefromjpeg($image_path);
break;
case ‘image/png’:
$image = imagecreatefrompng($image_path);
// ট্রান্সপারেন্সি হ্যান্ডেল
$new_image = imagecreatetruecolor(imagesx($image), imagesy($image));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, imagesx($image), imagesy($image), $transparent);
imagecopy($new_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$image = $new_image;
break;
default:
return false;
}

$webp_image_path = preg_replace(‘/\.(jpg|jpeg|png)$/i’, ‘.webp’, $image_path);

if (imagewebp($image, $webp_image_path, 80)) {
imagedestroy($image);
return $webp_image_path;
}

return false;
}

// WebP সাপোর্ট এড করা
add_filter(‘mime_types’, function($mimes) {
$mimes[‘webp’] = ‘image/webp’;
return $mimes;
});

add_filter(‘upload_mimes’, function($mimes) {
$mimes[‘webp’] = ‘image/webp’;
return $mimes;
});

[/codeblock]

Leave a Reply

Your email address will not be published. Required fields are marked *

Awesome!

Thank you for confirming. Please check your spam email!