mirror of
https://github.com/processwire/processwire.git
synced 2025-08-18 20:41:16 +02:00
add webp support to GD
This commit is contained in:
@@ -322,32 +322,73 @@ class ImageSizerEngineGD extends ImageSizerEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// optionally apply interlace bit to the final image.
|
// write to file(s)
|
||||||
// this will result in progressive JPEGs
|
$result = false;
|
||||||
if($this->interlace && \IMAGETYPE_JPEG == $this->imageType) {
|
switch($this->imageType) {
|
||||||
|
|
||||||
|
case \IMAGETYPE_GIF:
|
||||||
|
// correct gamma from linearized 1.0 back to 2.0
|
||||||
|
$this->gammaCorrection($thumb, false);
|
||||||
|
|
||||||
|
// If only a WebP file is required
|
||||||
|
if($this->webpOnly) {
|
||||||
|
$result = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// optionally save an additional WebP file
|
||||||
|
if($this->webpAdd) {
|
||||||
|
$resultWebp = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the final GIF image file
|
||||||
|
$result = imagegif($thumb, $dstFilename);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case \IMAGETYPE_PNG:
|
||||||
|
// optionally correct gamma from linearized 1.0 back to 2.0
|
||||||
|
if(!$this->hasAlphaChannel()) $this->gammaCorrection($thumb, false);
|
||||||
|
|
||||||
|
// If only a WebP file is required
|
||||||
|
if($this->webpOnly) {
|
||||||
|
$result = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// optionally save an additional WebP file
|
||||||
|
if($this->webpAdd) {
|
||||||
|
$resultWebp = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the final PNG image file and always use highest compression level (9) per @horst
|
||||||
|
$result = imagepng($thumb, $dstFilename, 9);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case \IMAGETYPE_JPEG:
|
||||||
|
// correct gamma from linearized 1.0 back to 2.0
|
||||||
|
$this->gammaCorrection($thumb, false);
|
||||||
|
|
||||||
|
// If only a WebP file is required
|
||||||
|
if($this->webpOnly) {
|
||||||
|
$result = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// optionally save an additional WebP file
|
||||||
|
if($this->webpAdd) {
|
||||||
|
$resultWebp = $this->imSaveWebP($thumb, $srcFilename, $this->webpQuality);
|
||||||
|
}
|
||||||
|
|
||||||
|
// optionally apply interlace bit to the final image. this will result in progressive JPEGs
|
||||||
|
if($this->interlace) {
|
||||||
if(0 == imageinterlace($thumb, 1)) {
|
if(0 == imageinterlace($thumb, 1)) {
|
||||||
// log that setting the interlace bit has failed ?
|
// log that setting the interlace bit has failed ?
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to file
|
// save the final JPEG image file
|
||||||
$result = false;
|
|
||||||
switch($this->imageType) {
|
|
||||||
case \IMAGETYPE_GIF:
|
|
||||||
// correct gamma from linearized 1.0 back to 2.0
|
|
||||||
$this->gammaCorrection($thumb, false);
|
|
||||||
$result = imagegif($thumb, $dstFilename);
|
|
||||||
break;
|
|
||||||
case \IMAGETYPE_PNG:
|
|
||||||
if(!$this->hasAlphaChannel()) $this->gammaCorrection($thumb, false);
|
|
||||||
// always use highest compression level for PNG (9) per @horst
|
|
||||||
$result = imagepng($thumb, $dstFilename, 9);
|
|
||||||
break;
|
|
||||||
case \IMAGETYPE_JPEG:
|
|
||||||
// correct gamma from linearized 1.0 back to 2.0
|
|
||||||
$this->gammaCorrection($thumb, false);
|
|
||||||
$result = imagejpeg($thumb, $dstFilename, $this->quality);
|
$result = imagejpeg($thumb, $dstFilename, $this->quality);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,6 +398,24 @@ class ImageSizerEngineGD extends ImageSizerEngine {
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create WebP image (@horst)
|
||||||
|
* Is requested by image options: ["webpAdd" => true] OR ["webpOnly" => true]
|
||||||
|
*
|
||||||
|
* @param resource $im
|
||||||
|
* @param string $dstFilename
|
||||||
|
* @param int $quality
|
||||||
|
*
|
||||||
|
* @return boolean true | false
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function imSaveWebP($im, $filename, $quality = 90) {
|
||||||
|
if(!function_exists('imagewebp')) return false;
|
||||||
|
$newBasename = str_replace(array('.jpg', '.jpeg', '.png', '.gif'), '.webp', basename($filename));
|
||||||
|
return imagewebp($im, dirname($filename) . '/' . $newBasename, $quality);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate image (@horst)
|
* Rotate image (@horst)
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user