Skip to content

Commit

Permalink
webp encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jul 3, 2017
1 parent dec6422 commit 961c58a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Intervention/Image/AbstractEncoder.php
Expand Up @@ -74,6 +74,13 @@ abstract protected function processBmp();
*/
abstract protected function processIco();

/**
* Processes and returns image as WebP encoded string
*
* @return string
*/
abstract protected function processWebp();

/**
* Process a given image
*
Expand Down Expand Up @@ -145,6 +152,12 @@ public function process(Image $image, $format = null, $quality = null)
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(
Expand Down
17 changes: 17 additions & 0 deletions src/Intervention/Image/Gd/Encoder.php
Expand Up @@ -55,6 +55,23 @@ protected function processGif()
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
*
Expand Down
22 changes: 22 additions & 0 deletions src/Intervention/Image/Imagick/Encoder.php
Expand Up @@ -66,6 +66,28 @@ protected function processGif()
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
*
Expand Down
22 changes: 22 additions & 0 deletions tests/EncoderTest.php
Expand Up @@ -46,6 +46,18 @@ public function testProcessGifGd()
$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
*/
Expand Down Expand Up @@ -155,6 +167,16 @@ public function testProcessGifImagick()
$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');
Expand Down
13 changes: 13 additions & 0 deletions tests/GdSystemTest.php
Expand Up @@ -1474,6 +1474,13 @@ public function testEncodeGif()
$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');
Expand Down Expand Up @@ -1643,4 +1650,10 @@ private function manager()
'driver' => 'gd'
));
}

private function getMime($data)
{
$finfo = new finfo(FILEINFO_MIME);
return $finfo->buffer($data);
}
}

0 comments on commit 961c58a

Please sign in to comment.