Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/nicojmb/CodeIgniter4 int…
Browse files Browse the repository at this point in the history
…o webp_support
  • Loading branch information
michalsn committed Jun 27, 2020
2 parents 275ee07 + dd5edd7 commit eaf9f02
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions system/Images/Handlers/GDHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ public function save(string $target = null, int $quality = 90): bool
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_WEBP:
if (! function_exists('imagewebp'))
{
throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported'));
}

if (! @imagewebp($this->resource, $target))
{
throw ImageException::forSaveFailed();
}
break;
default:
throw ImageException::forInvalidImageCreate();
}
Expand Down Expand Up @@ -389,6 +400,13 @@ protected function createImage(string $path = '', string $imageType = '')
}

return imagecreatefrompng($path);
case IMAGETYPE_WEBP:
if (! function_exists('imagecreatefromwebp'))
{
throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported'));
}

return imagecreatefromwebp($path);
default:
throw ImageException::forInvalidImageCreate('Ima');
}
Expand Down Expand Up @@ -416,6 +434,9 @@ protected function ensureResource()
case IMAGETYPE_PNG:
$this->resource = imagecreatefrompng($path);
break;
case IMAGETYPE_WEBP:
$this->resource = imagecreatefromwebp($path);
break;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ protected function getResourcePath()
protected function ensureResource()
{
$this->getResourcePath();

switch ($this->image()->imageType)
{
case IMAGETYPE_WEBP:
if (! in_array('WEBP', \Imagick::queryFormats()))
{
throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported'));
}
break;
}
}

//--------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion system/Images/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public function getProperties(bool $return = false)
$types = [
1 => 'gif',
2 => 'jpeg',
3 => 'png',
4 => 'png',
32 => 'webp'
];

$mime = 'image/' . ($types[$vals[2]] ?? 'jpg');
Expand Down
1 change: 1 addition & 0 deletions system/Language/en/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'gifNotSupported' => 'GIF images are often not supported due to licensing restrictions. You may have to use JPG or PNG images instead.',
'jpgNotSupported' => 'JPG images are not supported.',
'pngNotSupported' => 'PNG images are not supported.',
'webpNotSupported' => 'WEBP images are not supported.',
'fileNotSupported' => 'The supplied file is not a supported image type.',
'unsupportedImageCreate' => 'Your server does not support the GD function required to process this type of image.',
'jpgOrPngRequired' => 'The image resize protocol specified in your preferences only works with JPEG or PNG image types.',
Expand Down

0 comments on commit eaf9f02

Please sign in to comment.