Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -630,6 +631,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 @@ -943,6 +957,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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.