Search
 
SCRIPT & CODE EXAMPLE
 

PHP

convert png to webp in php

// 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);
Comment

png and jpg convert webp using php

1 function create_webp_image($image_path) {
2         $image_properties   = @getimagesize($image_path);
3         $image_info         = pathinfo($image_path);
4         $image_type         = $image_properties[2];
5         switch ($image_type) {
6             case 1:
7                 $img  = @imagecreatefromgif($image_path);
8                 break;
9             case 2:
10                $img  = @imagecreatefromjpeg($image_path);
11                break;
12            case 3:
13                $img  = @imagecreatefrompng($image_path);
14                break;
15        }
16        $new_path   = $image_info['dirname'].'/'.$image_info['filename'].'.webp';
17        $this->log->debug("image_properties", json_encode($image_properties, JSON_PRETTY_PRINT));
18        $this->log->debug("image_info", json_encode($image_info, JSON_PRETTY_PRINT));
19        $this->log->debug("img", $img);
20        $this->log->debug("new_path", $new_path);
21        $webp = imagewebp($img, $new_path);  // with or without (..., quality= int)
22        $this->log->debug("webp", $webp);
23        imagedestroy($img);
24    }
Comment

PREVIOUS NEXT
Code Example
Php :: Remove prefix on category title 
Php :: wp php get rows number from mysql 
Php :: array value search in php 
Php :: how to get the root domain in laravel 
Php :: laravel inline if else if 
Php :: laravel model uploaded file name 
Php :: php get index of string 
Php :: php implode associative array 
Php :: php slice array in half 
Php :: add month to date 
Php :: filter wordpress 
Php :: php dom get element innerhtml 
Php :: laravel include config 
Php :: php hour between 
Php :: php Program to check if a given year is leap year 
Php :: php xml to json 
Php :: how to free session variable in php codeigniter 
Php :: php pdo 
Php :: php curl detect 404 
Php :: php display json in browser 
Php :: link to internal pages in wp php 
Php :: apache use public folder as root 
Php :: spaceship operator php 
Php :: -store() laravel change name 
Php :: math functions and predefined constants php 
Php :: file upload in laravel 
Php :: 2 days left format in laravel 
Php :: encryption and decryption in php example 
Php :: call_user_func() 
Php :: datatables 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =