tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.22k stars 1.52k forks source link

Support for WebP with transparency #567

Open piatkowski opened 1 year ago

piatkowski commented 1 year ago

Hi, I need to work with WebP and PDFs and I ended up with code, which adds conversion WebP to PNG. Please check is it correct?

I changed line around tcpdf.php#L7136

and I changed if statement to the code below. If the file type is WebP then open image in Imagick, convert to PNG32. In case of WebP the _toPNG method returns 'pngalpha' so I should return ImagePngAlpha from some temporary PNG32 file.

if (($type == 'gif') OR ($type == 'png') or ($type == 'webp')) {
  $info = TCPDF_IMAGES::_toPNG($img, TCPDF_STATIC::getObjFilename('img', $this->file_id));
  if($type == 'webp'/* && $info == 'pngalpha'*/) {
      $webp = new Imagick($file);
      $webp->setFormat('png32');
      $tmpfile = TCPDF_STATIC::getObjFilename('img', $this->file_id);
      $webp->writeImageFile($tmpfile);
      $webppng = $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
      unlink($tmpfile);
      return $webppng;
  }
}