1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 09:31:53 +02:00

webp encoding

This commit is contained in:
Oliver Vogel
2017-07-03 15:58:33 +02:00
parent dec642219b
commit 961c58a995
5 changed files with 87 additions and 0 deletions

View File

@@ -74,6 +74,13 @@ abstract class AbstractEncoder
*/
abstract protected function processIco();
/**
* Processes and returns image as WebP encoded string
*
* @return string
*/
abstract protected function processWebp();
/**
* Process a given image
*
@@ -145,6 +152,12 @@ abstract class AbstractEncoder
case 'image/vnd.adobe.photoshop':
$this->result = $this->processPsd();
break;
case 'webp':
case 'image/webp':
case 'image/x-webp':
$this->result = $this->processWebp();
break;
default:
throw new \Intervention\Image\Exception\NotSupportedException(

View File

@@ -55,6 +55,23 @@ class Encoder extends \Intervention\Image\AbstractEncoder
return $buffer;
}
protected function processWebp()
{
if ( ! function_exists('imagewebp')) {
throw new \Intervention\Image\Exception\NotSupportedException(
"Webp format is not supported by PHP installation."
);
}
ob_start();
imagewebp($this->image->getCore(), null, $this->quality);
$this->image->mime = defined('IMAGETYPE_WEBP') ? image_type_to_mime_type(IMAGETYPE_WEBP) : 'image/webp';
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/**
* Processes and returns encoded image as TIFF string
*

View File

@@ -66,6 +66,28 @@ class Encoder extends \Intervention\Image\AbstractEncoder
return $imagick->getImagesBlob();
}
protected function processWebp()
{
if ( ! \Imagick::queryFormats('WEBP')) {
throw new \Intervention\Image\Exception\NotSupportedException(
"Webp format is not supported by Imagick installation."
);
}
$format = 'webp';
$compression = \Imagick::COMPRESSION_JPEG;
$imagick = $this->image->getCore();
$imagick = $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_MERGE);
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
$imagick->setImageCompressionQuality($this->quality);
return $imagick->getImagesBlob();
}
/**
* Processes and returns encoded image as TIFF string
*

View File

@@ -46,6 +46,18 @@ class EncoderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('image/gif; charset=binary', $this->getMime($encoder->result));
}
public function testProcessWebpGd()
{
$core = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
$encoder = new GdEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$image->shouldReceive('getCore')->once()->andReturn($core);
$image->shouldReceive('setEncoded')->once()->andReturn($image);
$img = $encoder->process($image, 'webp', 90);
$this->assertInstanceOf('Intervention\Image\Image', $img);
$this->assertEquals('image/webp; charset=binary', $this->getMime($encoder->result));
}
/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
@@ -155,6 +167,16 @@ class EncoderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('mock-gif', $encoder->result);
}
/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
public function testProcessWebpImagick()
{
$encoder = new ImagickEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$img = $encoder->process($image, 'webp', 90);
}
public function testProcessTiffImagick()
{
$core = $this->getImagickMock('tiff');

View File

@@ -1474,6 +1474,13 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
$this->assertInternalType('resource', imagecreatefromstring($img->encoded));
}
public function testEncodeWebp()
{
$img = $this->manager()->make('tests/images/trim.png');
$data = (string) $img->encode('webp');
$this->assertEquals('image/webp; charset=binary', $this->getMime($data));
}
public function testEncodeDataUrl()
{
$img = $this->manager()->make('tests/images/trim.png');
@@ -1643,4 +1650,10 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
'driver' => 'gd'
));
}
private function getMime($data)
{
$finfo = new finfo(FILEINFO_MIME);
return $finfo->buffer($data);
}
}