Skip to content

Commit

Permalink
Merge pull request joomla#31048 from SharkyKZ/j4/impr/image/webp
Browse files Browse the repository at this point in the history
[4.0] Add WebP support to Image library
  • Loading branch information
rdeutz committed Oct 16, 2020
2 parents c2468ab + a42f7e6 commit f5c3531
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
26 changes: 22 additions & 4 deletions libraries/src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ public function __construct($source = null)
}

// Determine which image types are supported by GD, but only once.
if (!isset(static::$formats[IMAGETYPE_JPEG]))
if (empty(static::$formats))
{
$info = gd_info();
static::$formats[IMAGETYPE_JPEG] = ($info['JPEG Support']) ? true : false;
static::$formats[IMAGETYPE_PNG] = ($info['PNG Support']) ? true : false;
static::$formats[IMAGETYPE_GIF] = ($info['GIF Read Support']) ? true : false;
static::$formats[IMAGETYPE_JPEG] = $info['JPEG Support'];
static::$formats[IMAGETYPE_PNG] = $info['PNG Support'];
static::$formats[IMAGETYPE_GIF] = $info['GIF Read Support'];
static::$formats[IMAGETYPE_WEBP] = $info['WebP Support'];
}

/**
Expand Down Expand Up @@ -633,6 +634,19 @@ public function loadFile($path)

break;

case 'image/webp':
// Make sure the image type is supported.
if (empty(static::$formats[IMAGETYPE_WEBP]))
{
throw new \RuntimeException('Attempting to load an image of unsupported type WebP.');
}

// Attempt to create the image handle.
$handle = imagecreatefromwebp($path);
$type = 'WebP';

break;

default:
throw new \InvalidArgumentException('Attempting to load an image of unsupported type ' . $properties->mime);
}
Expand Down Expand Up @@ -946,6 +960,10 @@ public function toFile($path, $type = IMAGETYPE_JPEG, array $options = [])
case IMAGETYPE_PNG:
return imagepng($this->getHandle(), $path, (\array_key_exists('quality', $options)) ? $options['quality'] : 0);
break;

case IMAGETYPE_WEBP:
return imagewebp($this->getHandle(), $path, (\array_key_exists('quality', $options)) ? $options['quality'] : 100);
break;
}

// Case IMAGETYPE_JPEG & default
Expand Down
69 changes: 69 additions & 0 deletions tests/Unit/Libraries/Cms/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected function setUp(): void
$this->testFilePng = __DIR__ . '/stubs/koala.png';

$this->testFileBmp = __DIR__ . '/stubs/koala.bmp';

$this->testFileWebp = __DIR__ . '/stubs/koala.webp';
}

/**
Expand Down Expand Up @@ -248,6 +250,33 @@ public function testloadFilePng()
$this->assertEquals($this->testFilePng, $image->getPath());
}

/**
* Test the Joomla\CMS\Image\Image::loadFile method
*
* Makes sure WebP images are loaded correctly
*
* In this case we are taking the simple approach of loading an image file
* and asserting that the dimensions are correct.
*
* @return void
*
* @covers Joomla\CMS\Image\Image::loadFile
*
* @since __DEPLOY_VERSION__
*/
public function testloadFileWebp()
{
// Get a new Image inspector.
$image = new ImageInspector;
$image->loadFile($this->testFileWebp);

// Verify that the cropped image is the correct size.
$this->assertEquals(341, imagesy($image->getClassProperty('handle')));
$this->assertEquals(500, imagesx($image->getClassProperty('handle')));

$this->assertEquals($this->testFileWebp, $image->getPath());
}

/**
* Test the Joomla\CMS\Image\Image::loadFile method
*
Expand Down Expand Up @@ -534,6 +563,46 @@ public function testToFileJpg()
unlink($outFileJpg);
}

/**
* Test the Joomla\CMS\Image\Image::toFile method
*
* Make sure that a new image is properly written to file.
*
* When performing this test using a lossy compression we are not able
* to open and save the same image and then compare the checksums as the checksums
* may have changed. Therefore we are limited to comparing the image properties.
*
* @return void
*
* @covers Joomla\CMS\Image\Image::toFile
*
* @since __DEPLOY_VERSION__
*/
public function testToFileWebp()
{
$outFileWebp = __DIR__ . '/tmp/out.webp';

$image = new ImageInspector($this->testFile);
$image->toFile($outFileWebp, IMAGETYPE_WEBP);

$a = Image::getImageFileProperties($this->testFile);
$b = Image::getImageFileProperties($outFileWebp);

// Assert that properties that should be equal are equal.
$this->assertEquals($a->width, $b->width);
$this->assertEquals($a->height, $b->height);
$this->assertEquals($a->attributes, $b->attributes);
$this->assertEquals($a->bits, $b->bits);

// Assert that properties that should be different are different.
$this->assertEquals('image/webp', $b->mime);
$this->assertEquals(IMAGETYPE_WEBP, $b->type);
$this->assertNull($b->channels);

// Clean up after ourselves.
unlink($outFileWebp);
}

/**
* Test the Joomla\CMS\Image\Image::toFile method
*
Expand Down
Binary file added tests/Unit/Libraries/Cms/Image/stubs/koala.webp
Binary file not shown.

0 comments on commit f5c3531

Please sign in to comment.