1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-01 11:30:16 +02:00

Merge branch 'katzefudder-master'

This commit is contained in:
Oliver Vogel
2014-12-29 12:58:35 +01:00
4 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace Intervention\Image\Commands;
class IptcCommand extends AbstractCommand
{
/**
* Read Iptc data from the given image
*
* @param \Intervention\Image\Image $image
* @return boolean
*/
public function execute($image)
{
if ( ! function_exists('iptcparse')) {
throw new \Intervention\Image\Exception\NotSupportedException(
"Reading Iptc data is not supported by this PHP installation."
);
}
$key = $this->argument(0)->value();
$info = array();
@getimagesize($image->dirname .'/'. $image->basename, $info);
$data = array();
if (array_key_exists('APP13', $info)) {
$iptc = iptcparse($info['APP13']);
if (is_array($iptc)) {
$data['DocumentTitle'] = isset($iptc["2#005"][0]) ? $iptc["2#005"][0] : null;
$data['Urgency'] = isset($iptc["2#010"][0]) ? $iptc["2#010"][0] : null;
$data['Category'] = isset($iptc["2#015"][0]) ? $iptc["2#015"][0] : null;
$data['Subcategories'] = isset($iptc["2#020"][0]) ? $iptc["2#020"][0] : null;
$data['Keywords'] = isset($iptc["2#025"][0]) ? $iptc["2#025"][0] : null;
$data['SpecialInstructions'] = isset($iptc["2#040"][0]) ? $iptc["2#040"][0] : null;
$data['CreationDate'] = isset($iptc["2#055"][0]) ? $iptc["2#055"][0] : null;
$data['AuthorByline'] = isset($iptc["2#080"][0]) ? $iptc["2#080"][0] : null;
$data['AuthorTitle'] = isset($iptc["2#085"][0]) ? $iptc["2#085"][0] : null;
$data['City'] = isset($iptc["2#090"][0]) ? $iptc["2#090"][0] : null;
$data['State'] = isset($iptc["2#095"][0]) ? $iptc["2#095"][0] : null;
$data['Country'] = isset($iptc["2#101"][0]) ? $iptc["2#101"][0] : null;
$data['OTR'] = isset($iptc["2#103"][0]) ? $iptc["2#103"][0] : null;
$data['Headline'] = isset($iptc["2#105"][0]) ? $iptc["2#105"][0] : null;
$data['Source'] = isset($iptc["2#110"][0]) ? $iptc["2#110"][0] : null;
$data['PhotoSource'] = isset($iptc["2#115"][0]) ? $iptc["2#115"][0] : null;
$data['Copyright'] = isset($iptc["2#116"][0]) ? $iptc["2#116"][0] : null;
$data['Caption'] = isset($iptc["2#120"][0]) ? $iptc["2#120"][0] : null;
$data['CaptionWriter'] = isset($iptc["2#120"][0]) ? $iptc["2#122"][0] : null;
}
}
if (! is_null($key) && is_array($data)) {
$data = array_key_exists($key, $data) ? $data[$key] : false;
}
$this->setOutput($data);
return true;
}
}

View File

@@ -15,6 +15,7 @@ namespace Intervention\Image;
* @method void destroy() Frees memory associated with the current image instance before the PHP script ends. Normally resources are destroyed automatically after the script is finished.
* @method \Intervention\Image\Image ellipse(integer $width, integer $height, integer $x, integer $y, \Closure $callback = null) Draw a colored ellipse at given x, y, coordinates. You can define width and height and set the appearance of the circle by an optional closure callback.
* @method mixed exif(string $key = null) Read Exif meta data from current image.
* @method mixed iptc(string $key = null) Read Iptc meta data from current image.
* @method \Intervention\Image\Image fill(mixed $filling, integer $x = null, integer $y = null) Fill current image with given color or another image used as tile for filling. Pass optional x, y coordinates to start at a certain point.
* @method \Intervention\Image\Image flip(mixed $mode = 'h') Mirror the current image horizontally or vertically by specifying the mode.
* @method \Intervention\Image\Image fit(integer $width, integer $height = null, \Closure $callback = null, string $position = 'center') Combine cropping and resizing to format image in a smart way. The method will find the best fitting aspect ratio of your given width and height on the current image automatically, cut it out and resize it to the given dimension. You may pass an optional Closure callback as third parameter, to prevent possible upsizing and a custom position of the cutout as fourth parameter.

71
tests/IptcCommandTest.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
use Intervention\Image\Commands\IptcCommand;
class IptcCommandTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
Mockery::close();
}
public function testFetchAll()
{
$image = Mockery::mock('Intervention\Image\Image');
$image->dirname = __DIR__.'/images';
$image->basename = 'iptc.jpg';
$command = new IptcCommand(array());
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertInternalType('array', $command->getOutput());
}
public function testFetchDefined()
{
$image = Mockery::mock('Intervention\Image\Image');
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new IptcCommand(array('AuthorByline'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals('Oliver Vogel', $command->getOutput());
}
public function testFetchNonExisting()
{
$image = Mockery::mock('Intervention\Image\Image');
$image->dirname = __DIR__.'/images';
$image->basename = 'exif.jpg';
$command = new IptcCommand(array('xxx'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals(null, $command->getOutput());
}
public function testFetchFromPng()
{
$image = Mockery::mock('Intervention\Image\Image');
$image->dirname = __DIR__.'/images';
$image->basename = 'star.png';
$command = new IptcCommand(array('Orientation'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals(null, $command->getOutput());
}
public function testReturnNullOnIptcReadFail()
{
$image = Mockery::mock('Intervention\Image\Image');
$command = new IptcCommand(array('Orientation'));
$result = $command->execute($image);
$this->assertTrue($result);
$this->assertTrue($command->hasOutput());
$this->assertEquals(null, $command->getOutput());
}
}

BIN
tests/images/iptc.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB