title: Converting gif/jpeg/png to webp using PHP's imagewebp
body: <p>I have read several issues and possible solutions on stackoverflow/github/php docs/etc., and I still can't seem to find or figure a way to convert images (gif/jpeg/png) to webp format. Most recently I stumbled upon <a href="https://stackoverflow.com/questions/26314508/convert-jpg-to-webp-using-imagewebp">this question</a>; which I tried to recreate something similar with this function I wrote:</p>
  <pre><code>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-&gt;log-&gt;debug(&quot;image_properties&quot;, json_encode($image_properties, JSON_PRETTY_PRINT));
  18        $this-&gt;log-&gt;debug(&quot;image_info&quot;, json_encode($image_info, JSON_PRETTY_PRINT));
  19        $this-&gt;log-&gt;debug(&quot;img&quot;, $img);
  20        $this-&gt;log-&gt;debug(&quot;new_path&quot;, $new_path);
  21        $webp = imagewebp($img, $new_path);  // with or without (..., quality= int)
  22        $this-&gt;log-&gt;debug(&quot;webp&quot;, $webp);
  23        imagedestroy($img);
  24    }
  </code></pre>
  <p>The code runs up until line 22, which it doesn't log.</p>
  <p>I also saw that you can use <a href="https://stackoverflow.com/a/32666832/12706523">imagecreatefromstring()</a> that I wanted to incorporate rather than using the switch statement, but can't seem to figure out my first issue (getting this thing to work).</p>