//ini_set('error_reporting', E_ALL); //ini_set('display_errors', 1); //ini_set('display_startup_errors', 1); //ini_set('log_errors', 'On'); class MakeWebP { protected $url = ''; protected $original = ''; //protected $cwebp = CWEBP_PATH; protected $im = ''; public $image = ''; public $imgType = ''; public function __construct(){ $this->url = urldecode($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']); $this->init(); } protected function createImage($imgOriginal){ $this->original = $imgOriginal; $this->imgType = mime_content_type($this->original); if($this->imgType == 'image/jpg'){ $this->im = imagecreatefromjpeg($this->original); } elseif($this->imgType == 'image/jpeg'){ $this->im = imagecreatefromjpeg($this->original); } elseif($this->imgType == 'image/png'){ $this->im = imagecreatefrompng($this->original); imagepalettetotruecolor($this->im); imagealphablending($this->im, true); imagesavealpha($this->im, true); } } private function setOriginal(){ preg_match('/^(.*\.)webp$/', $this->url, $match); $jpg = $match[1] . 'jpg'; $jpeg = $match[1] . 'jpeg'; $png = $match[1] . 'png'; if(file_exists($jpg)){ $this->createImage($jpg); } elseif(file_exists($jpeg)){ $this->createImage($jpeg); } elseif(file_exists($png)){ $this->createImage($png); } } protected function convert(){ if($this->im != ''){ $convert = imagewebp($this->im, $this->url); imagedestroy($this->im); } } protected function init(){ $this->setOriginal(); $this->convert(); if( !file_exists($this->url) ){ $this->image = file_get_contents( $this->original ); }else{ $this->image = file_get_contents( $this->url ); } } } $webp = new MakeWebP(); $content_type = "content-type: " . $webp->imgType; header($content_type); echo $webp->image;