title: How to convert png file to webp file
body: <p>I need to convert an image <strong>(png)</strong> to <strong>(webp)</strong> file.</p>
  
  <p>After uploading a <strong>png</strong> file, the <strong>webp</strong> image has been generated, but the <strong>webp</strong> file didn't copy the transparency of a <strong>png</strong> file, instead it creates a black background.</p>
  
  <p><strong>This is my php code:</strong></p>
  
  <pre><code>$type = wp_check_filetype($file, null);
  $ext = $type['ext'];
  if ($ext === 'png') {
      $im = imagecreatefrompng($file);
      imagepalettetotruecolor($im);
      $webp = imagewebp($im, str_replace('png', 'webp', $file));
  }
  imagedestroy($im);
  </code></pre>
  
  <blockquote>
    <p>The version of PHP is 5.6</p>
  </blockquote>