// get png in question
$pngimg = imagecreatefrompng($file);
// get dimens of image
$w = imagesx($pngimg);
$h = imagesy($pngimg);;
// create a canvas
$im = imagecreatetruecolor ($w, $h);
imageAlphaBlending($im, false);
imageSaveAlpha($im, true);
// By default, the canvas is black, so make it transparent
$trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans);
// copy png to canvas
imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h);
// lastly, save canvas as a webp
imagewebp($im, str_replace('png', 'webp', $file));
// done
imagedestroy($im);
Code Example |
---|
Php :: string between two strings |
Php :: Remove prefix on category title |
Php :: html pagination php |
Php :: wordpress debug mode |
Php :: laravel store file |
Php :: imagick php |
Php :: job execute async laravel |
Php :: Laravel jwt check token sent by request is valid |
Php :: laravel bd query |
Php :: how to upload large video file in php |
Php :: array push in php |
Php :: php string to date |
Php :: how to send data from html to php |
Php :: vs code php tag shortcut |
Php :: php variable as javascript function parameter in echo |
Php :: array_merge |
Php :: update cart subtotal woocommerce |
Php :: laravel tinker insert db record |
Php :: php pdo example |
Php :: nested with laravel |
Php :: laravel add request |
Php :: Main features of php |
Php :: remove invalid characters from a string laravel |
Php :: remove square brackets from string php |
Php :: acos() php |
Php :: database, counts |
Php :: count cpt wp |
Php :: how to run php in js |
Php :: laravel faker |
Php :: laravel crud |