Skip to content

Commit 509545b

Browse files
author
Alexander Tolochko
committed
Fix keeping transparency when resizing WebP image with alpha
1 parent 8d92ddb commit 509545b

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

Model/Image/Adapter/Gd2.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,26 @@ public function checkAlpha($fileName)
415415
return (ord(file_get_contents((string)$fileName, false, null, 25, 1)) & 6 & 4) == 4;
416416
}
417417

418+
/**
419+
* Gives true for a WebP with alpha, false otherwise
420+
*
421+
* @param string $filename
422+
* @return bool
423+
*/
424+
public function checkAlphaWebp($filename)
425+
{
426+
$buf = file_get_contents((string) $filename, false, null, 0, 25);
427+
if ($buf[15] === 'L') {
428+
// Simple File Format (Lossless)
429+
return (bool) (!!(ord($buf[24]) & 0x00000010));
430+
} elseif ($buf[15] === 'X') {
431+
// Extended File Format
432+
return (bool) (!!(ord($buf[20]) & 0x00000010));
433+
}
434+
435+
return false;
436+
}
437+
418438
/**
419439
* Checks if image has alpha transparency
420440
*
@@ -445,12 +465,18 @@ private function _getTransparency($imageResource, $fileType, &$isAlpha = false,
445465
return $transparentIndex;
446466
}
447467
}
468+
if (IMAGETYPE_JPEG === $fileType) {
469+
$isTrueColor = true;
470+
}
471+
448472
/*
449-
* FIX: added '|| IMAGETYPE_WEBP === $fileType' for the condition
450-
*/
451-
if (IMAGETYPE_JPEG === $fileType || IMAGETYPE_WEBP === $fileType) {
473+
* FIX: prepare transparency data for WebP image
474+
*/
475+
if (IMAGETYPE_WEBP === $fileType) {
452476
$isTrueColor = true;
477+
$isAlpha = $this->checkAlphaWebp($this->_fileName);
453478
}
479+
454480
return false;
455481
}
456482

0 commit comments

Comments
 (0)