From cc6161cc4f15e0af8c24dd4cb25bdfdafd34d85e Mon Sep 17 00:00:00 2001 From: Florian Dehn Date: Wed, 24 Dec 2014 10:14:37 +0100 Subject: [PATCH] added iptc command --- .../Image/Commands/IptcCommand.php | 62 +++++++++++++++ src/Intervention/Image/Image.php | 1 + tests/IptcCommandTest.php | 71 ++++++++++++++++++ tests/images/iptc.jpg | Bin 0 -> 10526 bytes 4 files changed, 134 insertions(+) create mode 100644 src/Intervention/Image/Commands/IptcCommand.php create mode 100644 tests/IptcCommandTest.php create mode 100644 tests/images/iptc.jpg diff --git a/src/Intervention/Image/Commands/IptcCommand.php b/src/Intervention/Image/Commands/IptcCommand.php new file mode 100644 index 00000000..b3c284ef --- /dev/null +++ b/src/Intervention/Image/Commands/IptcCommand.php @@ -0,0 +1,62 @@ +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#115"][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; + } +} \ No newline at end of file diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 6b85bacb..5184676b 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -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. diff --git a/tests/IptcCommandTest.php b/tests/IptcCommandTest.php new file mode 100644 index 00000000..d1ca865f --- /dev/null +++ b/tests/IptcCommandTest.php @@ -0,0 +1,71 @@ +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()); + } +} diff --git a/tests/images/iptc.jpg b/tests/images/iptc.jpg new file mode 100644 index 0000000000000000000000000000000000000000..849dd0a3f9bf8420a4cd8501fac3935f3ebe6e5c GIT binary patch literal 10526 zcmeG?TW}l4k+X{z9}q=~lqEYX7Ysce2gCv(KoT(}5g;Yf5o{U&E!%ZfuEp*ETxqcj z?+!?cA1PZ_;v_$bUwKt3`qVXh1NO$*XGugDnf+)n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