mirror of
https://github.com/Intervention/image.git
synced 2025-02-07 14:20:37 +01:00
Merge pull request #659 from frederikbosch/master
add support for exif to imagick extension
This commit is contained in:
commit
84f793df71
62
src/Intervention/Image/Imagick/Commands/ExifCommand.php
Normal file
62
src/Intervention/Image/Imagick/Commands/ExifCommand.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Imagick\Commands;
|
||||
|
||||
use Intervention\Image\Commands\ExifCommand as BaseCommand;
|
||||
|
||||
class ExifCommand extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* Prefer extension or not
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $preferExtension = true;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function dontPreferExtension() {
|
||||
$this->preferExtension = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Exif data from the given image
|
||||
*
|
||||
* @param \Intervention\Image\Image $image
|
||||
* @return boolean
|
||||
*/
|
||||
public function execute($image)
|
||||
{
|
||||
if ($this->preferExtension && function_exists('exif_read_data')) {
|
||||
return parent::execute($image);
|
||||
}
|
||||
|
||||
$core = $image->getCore();
|
||||
|
||||
if ( ! method_exists($core, 'getImageProperties')) {
|
||||
throw new \Intervention\Image\Exception\NotSupportedException(
|
||||
"Reading Exif data is not supported by this PHP installation."
|
||||
);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
@ -57,4 +57,54 @@ class ExifCommandTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($command->hasOutput());
|
||||
$this->assertEquals(null, $command->getOutput());
|
||||
}
|
||||
|
||||
public function testImagickFetchAll()
|
||||
{
|
||||
$image = $this->imagick()->make(__DIR__.'/images/exif.jpg');
|
||||
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array());
|
||||
$command->dontPreferExtension();
|
||||
$result = $command->execute($image);
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue($command->hasOutput());
|
||||
$this->assertInternalType('array', $command->getOutput());
|
||||
}
|
||||
|
||||
public function testImagickFetchDefined()
|
||||
{
|
||||
$image = $this->imagick()->make(__DIR__.'/images/exif.jpg');
|
||||
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array('Artist'));
|
||||
$command->dontPreferExtension();
|
||||
$result = $command->execute($image);
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue($command->hasOutput());
|
||||
$this->assertEquals('Oliver Vogel', $command->getOutput());
|
||||
}
|
||||
|
||||
public function testImagickNonExisting()
|
||||
{
|
||||
$image = $this->imagick()->make(__DIR__.'/images/exif.jpg');
|
||||
$command = new \Intervention\Image\Imagick\Commands\ExifCommand(array('xx'));
|
||||
$command->dontPreferExtension();
|
||||
$result = $command->execute($image);
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue($command->hasOutput());
|
||||
$this->assertEquals(null, $command->getOutput());
|
||||
}
|
||||
|
||||
public function testImagickFallbackToExifExtenstion()
|
||||
{
|
||||
$image = $this->imagick()->make(__DIR__.'/images/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());
|
||||
}
|
||||
|
||||
private function imagick()
|
||||
{
|
||||
return new \Intervention\Image\ImageManager(array(
|
||||
'driver' => 'imagick'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -1465,7 +1465,7 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase
|
||||
$img = $this->manager()->make('tests/images/exif.jpg');
|
||||
$data = $img->exif();
|
||||
$this->assertInternalType('array', $data);
|
||||
$this->assertEquals(19, count($data));
|
||||
$this->assertGreaterThanOrEqual(13, count($data));
|
||||
}
|
||||
|
||||
public function testExifReadKey()
|
||||
|
Loading…
x
Reference in New Issue
Block a user