Edit in JSFiddle

<?php 
header("Access-Control-Allow-Origin: *");
header("cache-control: public, max-age=31557600");  
header("etag: ".str_replace('.','',$_GET['img']));  

$link = "image/";
$i = str_replace('.webp','',$_GET['img']);
$url=$link.$i;
$tmp_names =$url;


if(!file_exists($url)){
	$url = $link."error.png";
	$tmp_names = $url;
} 

 
$extensionList = array("bmp", "jpg", "gif", "png", "jpeg","webp");
$pecah = explode(".", $url);

foreach($pecah as $new) {
	$new = trim($new);
	$ekstensi = strtolower($new);
}


if (in_array($ekstensi, $extensionList))
{

list($w, $h) = getimagesize($url);
$width = round($w / 2);
$height = round($h / 2);
$q = 70;


  $ratio = max($width/$w, $height/$h);
  $h = ceil($height / $ratio);
  $x = ($w - $width / $ratio) / 2;
  $y = ($h - $height / $ratio) / 2;
  $w = ceil($width / $ratio);
  /* new file name */
  /* read binary data from image file */
  $imgString = $url;
  /* create image from string */

  switch ($ekstensi) {
    case 'jpeg':
	$image = imagecreatefromjpeg($imgString);
	break;
    case 'jpg':
	$image = imagecreatefromjpeg($imgString);
	break;
    case 'png':
	$image = imagecreatefrompng($imgString);
	break;
    case 'gif':
	$image = imagecreatefromgif($imgString);
	break;
	default:
      exit;
      break;
  }
  

  
  


$tmp = imagecreatetruecolor($width, $height);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);


  imagecopyresampled($tmp, $image,
    0, 0,
    $x, $y,
    $width, $height,
    $w, $h);
	
header('Content-Type: image/webp');
  switch ($ekstensi) {
    case 'jpeg':
	imagewebp($tmp, null,$q);
      break;
    case 'jpg':  
	imagewebp($tmp, null,$q);
      break;
    case 'png':
	imagewebp($tmp, null,$q);
      break;
    case 'gif':
	imagewebp($tmp, null,$q);
 
      break;
    default:
      exit;
      break;
  }
  
}  


?>