Skip to content

Commit 022a25c

Browse files
Merge pull request #227 from hempalex/master
Fix preserving WEBP transparency and formats magic numbers
2 parents 0c87fde + 54f0bc0 commit 022a25c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

phpthumb.class.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,11 +3898,11 @@ public function SourceImageToGD() {
38983898
$this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData);
38993899
if (!$this->gdimg_source) {
39003900
if (substr($this->rawImageData, 0, 2) === 'BM') {
3901-
$this->getimagesizeinfo[2] = 6; // BMP
3901+
$this->getimagesizeinfo[2] = IMAGETYPE_BMP; // BMP
39023902
} elseif (substr($this->rawImageData, 0, 4) === 'II'."\x2A\x00") {
3903-
$this->getimagesizeinfo[2] = 7; // TIFF (littlendian)
3903+
$this->getimagesizeinfo[2] = IMAGETYPE_TIFF_II; // TIFF (littlendian)
39043904
} elseif (substr($this->rawImageData, 0, 4) === 'MM'."\x00\x2A") {
3905-
$this->getimagesizeinfo[2] = 8; // TIFF (bigendian)
3905+
$this->getimagesizeinfo[2] = IMAGETYPE_TIFF_MM; // TIFF (bigendian)
39063906
}
39073907
$this->DebugMessage('SourceImageToGD.ImageCreateFromStringReplacement() failed with unknown image type "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).')', __FILE__, __LINE__);
39083908
// return $this->ErrorImage('Unknown image type identified by "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).') in SourceImageToGD()['.__LINE__.']');
@@ -3914,9 +3914,10 @@ public function SourceImageToGD() {
39143914
return $this->ErrorImage('$this->md5s != md5(sourceFilename)'."\n".'"'.$this->md5s.'" != '."\n".'"'.phpthumb_functions::md5_file_safe($this->sourceFilename).'"');
39153915
}
39163916
switch (@$this->getimagesizeinfo[2]) {
3917-
case 1:
3918-
case 3:
3919-
// GIF or PNG input file may have transparency
3917+
case IMAGETYPE_GIF:
3918+
case IMAGETYPE_PNG:
3919+
case IMAGETYPE_WEBP:
3920+
// GIF, PNG of WEBP input file may have transparency
39203921
$this->is_alpha = true;
39213922
break;
39223923
}
@@ -3992,18 +3993,23 @@ public function SourceImageToGD() {
39923993
$gd_info = gd_info();
39933994
$GDreadSupport = false;
39943995
switch (@$this->getimagesizeinfo[2]) {
3995-
case 1:
3996+
case IMAGETYPE_GIF:
39963997
$imageHeader = 'Content-Type: image/gif';
39973998
$GDreadSupport = (bool) @$gd_info['GIF Read Support'];
39983999
break;
3999-
case 2:
4000+
case IMAGETYPE_JPEG:
40004001
$imageHeader = 'Content-Type: image/jpeg';
40014002
$GDreadSupport = (bool) @$gd_info['JPG Support'];
40024003
break;
4003-
case 3:
4004+
case IMAGETYPE_PNG:
40044005
$imageHeader = 'Content-Type: image/png';
40054006
$GDreadSupport = (bool) @$gd_info['PNG Support'];
40064007
break;
4008+
case IMAGETYPE_WEBP:
4009+
$imageHeader = 'Content-Type: image/webp';
4010+
$GDreadSupport = (bool) @$gd_info['WebP Support '];
4011+
break;
4012+
40074013
}
40084014
if ($imageHeader) {
40094015
// cannot create image for whatever reason (maybe imagecreatefromjpeg et al are not available?)
@@ -4044,7 +4050,7 @@ public function SourceImageToGD() {
40444050
//switch (substr($this->rawImageData, 0, 2)) {
40454051
// case 'BM':
40464052
switch (@$this->getimagesizeinfo[2]) {
4047-
case 6:
4053+
case IMAGETYPE_BMP:
40484054
ob_start();
40494055
if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
40504056
ob_end_clean();
@@ -4071,8 +4077,8 @@ public function SourceImageToGD() {
40714077
//switch (substr($this->rawImageData, 0, 4)) {
40724078
// case 'II'."\x2A\x00":
40734079
// case 'MM'."\x00\x2A":
4074-
case 7:
4075-
case 8:
4080+
case IMAGETYPE_TIFF_II:
4081+
case IMAGETYPE_TIFF_MM:
40764082
return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on TIFF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
40774083
break;
40784084

0 commit comments

Comments
 (0)