1
0
mirror of https://github.com/Intervention/image.git synced 2025-02-07 14:20:37 +01:00

add support for exif to imagick extension without requiring exif extension

This commit is contained in:
Frederik Bosch 2017-01-07 17:48:52 +01:00
parent 4064a98032
commit e6ec5dcfd9
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace Intervention\Image\Imagick\Commands;
use Intervention\Image\Commands\ExifCommand as BaseCommand;
class ExifCommand extends BaseCommand
{
/**
* Read Exif data from the given image
*
* @param \Intervention\Image\Image $image
* @return boolean
*/
public function execute($image)
{
$core = $image->getCore();
// when getImageProperty is not supported fallback to default exif command
if ( ! method_exists($core, 'getImageProperties')) {
return parent::execute($image);
}
$requestedKey = $this->argument(0)->value();
if ($requestedKey !== null) {
$this->setOutput($core->getImageProperty('exif:' . $requestedKey));
return true;
}
$exif = [];
$properties = $core->getImageProperties();
foreach ($properties as $key => $value) {
if (substr($key, 0, 5) !== 'exif:') {
continue;
}
$exif[substr($key, 6)] = $value;
}
$this->setOutput($exif);
return true;
}
}

View File

@ -57,4 +57,55 @@ class ExifCommandTest extends PHPUnit_Framework_TestCase
$this->assertTrue($command->hasOutput());
$this->assertEquals(null, $command->getOutput());
}
public function testImagickFetchAll()
{
$image = new Image;
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array());
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertInternalType('array', $command->getOutput());
}
public function testImagickFetchDefined()
{
$image = new Image;
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array('Artist'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals('Oliver Vogel', $command->getOutput());
}
public function testImagickNonExisting()
{
$image = new Image;
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array('xxx'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals(null, $command->getOutput());
}
public function testImagickFallbackToExifExtenstion()
{
$imagick = Mockery::mock('stdClass');
$image = Mockery::mock('Intervention\Image\Image');
$image->shouldReceive('getCore')->once()->andReturn($imagick);
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array('Artist'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals('Oliver Vogel', $command->getOutput());
}
}