1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-01 09:52:59 +02:00

webp decoding

This commit is contained in:
Oliver Vogel
2017-07-03 16:31:54 +02:00
parent 961c58a995
commit baeeac5125
3 changed files with 47 additions and 11 deletions

View File

@@ -14,40 +14,54 @@ class Decoder extends \Intervention\Image\AbstractDecoder
*/
public function initFromPath($path)
{
$info = @getimagesize($path);
if ($info === false) {
if ( ! file_exists($path)) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image from file ({$path})."
"Unable to find file ({$path})."
);
}
// get mime type of file
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
// define core
switch ($info[2]) {
case IMAGETYPE_PNG:
switch (strtolower($mime)) {
case 'image/png':
case 'image/x-png':
$core = @imagecreatefrompng($path);
break;
case IMAGETYPE_JPEG:
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
$core = @imagecreatefromjpeg($path);
if (!$core) {
$core= @imagecreatefromstring(file_get_contents($path));
}
break;
case IMAGETYPE_GIF:
case 'image/gif':
$core = @imagecreatefromgif($path);
break;
case 'image/webp':
case 'image/x-webp':
if ( ! function_exists('imagecreatefromwebp')) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unsupported image type. GD/PHP installation does not support WebP format."
);
}
$core = @imagecreatefromwebp($path);
break;
default:
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image type. GD driver is only able to decode JPG, PNG or GIF files."
"Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files."
);
}
if (empty($core)) {
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to read image from file ({$path})."
"Unable to decode image from file ({$path})."
);
}
@@ -55,7 +69,7 @@ class Decoder extends \Intervention\Image\AbstractDecoder
// build image
$image = $this->initFromGdResource($core);
$image->mime = $info['mime'];
$image->mime = $mime;
$image->setFileInfoFromPath($path);
return $image;

View File

@@ -28,6 +28,14 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
$this->manager()->make('tests/images/broken.png');
}
/**
* @expectedException \Intervention\Image\Exception\NotReadableException
*/
public function testMakeFromNotExisting()
{
$this->manager()->make('tests/images/not_existing.png');
}
public function testMakeFromString()
{
$str = file_get_contents('tests/images/circle.png');
@@ -75,6 +83,20 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
$this->assertEquals(10, $img->getHeight());
}
public function testMakeFromWebp()
{
$img = $this->manager()->make('tests/images/test.webp');
$this->assertInstanceOf('Intervention\Image\Image', $img);
$this->assertInternalType('resource', $img->getCore());
$this->assertEquals(16, $img->getWidth());
$this->assertEquals(16, $img->getHeight());
$this->assertEquals('image/webp', $img->mime);
$this->assertEquals('tests/images', $img->dirname);
$this->assertEquals('test.webp', $img->basename);
$this->assertEquals('webp', $img->extension);
$this->assertEquals('test', $img->filename);
}
public function testCanvas()
{
$img = $this->manager()->canvas(30, 20);

BIN
tests/images/test.webp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 B