Skip to content

Commit

Permalink
Merge pull request #16 from klaari/parse-frameright-metadata
Browse files Browse the repository at this point in the history
Parse Frameright app generated region metafields
  • Loading branch information
lourot authored Oct 9, 2023
2 parents 323273d + 3752981 commit 457b8f7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public function getIDCMetadata(
'radius' => $region->rbRx,

'vertices' => [],

'regionDefinitionId' => $region->regionDefinitionId,
'regionName' => $region->regionName,
];

if ($region->rbVertices) {
Expand All @@ -131,6 +134,8 @@ public function getIDCMetadata(
if ($essentialOnly) {
unset($idc_metadata_region['types']);
unset($idc_metadata_region['roles']);
unset($idc_metadata_region['regionDefinitionId']);
unset($idc_metadata_region['regionName']);

if ($idc_metadata_region['unit'] === 'relative') {
unset($idc_metadata_region['imageWidth']);
Expand Down
6 changes: 6 additions & 0 deletions src/Metadata/Xmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Xmp
*/
const PHOTO_MECHANIC_NS = "http://ns.camerabits.com/photomechanic/1.0/";

/**
*
*/
const FRAMERIGHT_IDC_NS = 'http://ns.frameright.io/idc/1.0/';

/**
* @var \DomDocument
*/
Expand Down Expand Up @@ -80,6 +85,7 @@ class Xmp
'xmpRights' => self::XMP_RIGHTS_NS,
'Iptc4xmpCore' => self::IPTC4_XMP_CORE_NS,
'Iptc4xmpExt' => self::IPTC4_XMP_EXT_NS,
'FramerightIdc' => self::FRAMERIGHT_IDC_NS,
'photomechanic' => self::PHOTO_MECHANIC_NS
];

Expand Down
27 changes: 27 additions & 0 deletions src/Metadata/Xmp/ImageRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ class ImageRegion
*/
public $rbVertices;


/**
* Region's definition ID from the Frameright service.
*
* @var string|null
*/
public $regionDefinitionId;

/**
* Region's definition name from the Frameright service.
*
* @var string|null
*/
public $regionName;

/**
* Initialize members if a node is provided.
*
Expand Down Expand Up @@ -111,6 +126,18 @@ public function __construct($xpath = null, $node = null)
$node
);

$this->regionDefinitionId = self::getNodeValue(
$xpath,
'FramerightIdc:RegionDefinitionId',
$node
);

$this->regionName = self::getNodeValue(
$xpath,
'FramerightIdc:RegionName',
$node
);

$xpathToRb = 'Iptc4xmpExt:RegionBoundary';

foreach ([
Expand Down
Binary file added tests/Fixtures/frameright.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/Format/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function testGetIDCMetadata()
$inputRectangleRegion->rbXY = new Point(0.31, 0.18);
$inputRectangleRegion->rbH = 0.385;
$inputRectangleRegion->rbW = 0.127;
$inputRectangleRegion->regionDefinitionId = 'rectangleregiondefid';
$inputRectangleRegion->regionName = 'rectangleregionname';

$inputCircleRegion = new ImageRegion();
$inputCircleRegion->id = 'persltr3';
Expand All @@ -97,6 +99,8 @@ public function testGetIDCMetadata()
$inputCircleRegion->rbUnit = 'relative';
$inputCircleRegion->rbXY = new Point(0.59, 0.426);
$inputCircleRegion->rbRx = 0.068;
$inputCircleRegion->regionDefinitionId = 'circleregiondefid';
$inputCircleRegion->regionName = 'circleregionname';

$inputPolygonRegion = new ImageRegion();
$inputPolygonRegion->id = 'persltr1';
Expand All @@ -119,6 +123,8 @@ public function testGetIDCMetadata()
new Point(0.148, 0.041),
new Point(0.375, 0.863),
];
$inputPolygonRegion->regionDefinitionId = 'polygonregiondefid';
$inputPolygonRegion->regionName = 'polygonregionname';

$xmp->expects($this->atLeastOnce())->method('getImageRegions')->with(
ShapeFilter::ANY,
Expand Down Expand Up @@ -152,6 +158,8 @@ public function testGetIDCMetadata()
'height' => 0.385,
'radius' => null,
'vertices' => [],
'regionDefinitionId' => 'rectangleregiondefid',
'regionName' => 'rectangleregionname',
];

$expectedCircleRegion = [
Expand All @@ -177,6 +185,8 @@ public function testGetIDCMetadata()
'height' => null,
'radius' => 0.068,
'vertices' => [],
'regionDefinitionId' => 'circleregiondefid',
'regionName' => 'circleregionname',
];

$expectedPolygonRegion = [
Expand Down Expand Up @@ -215,6 +225,8 @@ public function testGetIDCMetadata()
'y' => 0.863,
],
],
'regionDefinitionId' => 'polygonregiondefid',
'regionName' => 'polygonregionname',
];

$this->assertEquals([
Expand All @@ -233,6 +245,8 @@ public function testGetIDCMetadata()
unset($expectedRectangleRegion['imageHeight']);
unset($expectedRectangleRegion['radius']);
unset($expectedRectangleRegion['vertices']);
unset($expectedRectangleRegion['regionDefinitionId']);
unset($expectedRectangleRegion['regionName']);

unset($expectedCircleRegion['types']);
unset($expectedCircleRegion['roles']);
Expand All @@ -241,6 +255,8 @@ public function testGetIDCMetadata()
unset($expectedCircleRegion['width']);
unset($expectedCircleRegion['height']);
unset($expectedCircleRegion['vertices']);
unset($expectedCircleRegion['regionDefinitionId']);
unset($expectedCircleRegion['regionName']);

unset($expectedPolygonRegion['types']);
unset($expectedPolygonRegion['roles']);
Expand All @@ -251,6 +267,8 @@ public function testGetIDCMetadata()
unset($expectedPolygonRegion['width']);
unset($expectedPolygonRegion['height']);
unset($expectedPolygonRegion['radius']);
unset($expectedPolygonRegion['regionDefinitionId']);
unset($expectedPolygonRegion['regionName']);

$this->assertEquals([
$expectedRectangleRegion,
Expand Down
33 changes: 33 additions & 0 deletions tests/Metadata/XmpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,39 @@ public function testGetImageRegions()
], $xmp->getImageRegions(ShapeFilter::RECTANGLE, RoleFilter::CROP));
}

/**
* @covers ::getImageRegions
*/
public function testGetImageRegionFromFramerightImage()
{
$jpeg = JPEG::fromFile(
__DIR__ . '/../Fixtures/frameright.jpg');


$xmp = $jpeg->getXmp();

$expectedRegion = new ImageRegion();
$expectedRegion->regionDefinitionId = 'definition-7a54f275-6872-435e-befc-b52d97653a28';
$expectedRegion->regionName = '4:3 Horizontal';
$expectedRegion->id = 'crop-e789b6b8-ee15-45c0-a0c5-3ad38858db14';
$expectedRegion->names = null;
$expectedRegion->types = null;
$expectedRegion->roles = [
'http://cv.iptc.org/newscodes/imageregionrole/cropping',
];
$expectedRegion->rbShape = 'rectangle';
$expectedRegion->rbUnit = 'relative';
$expectedRegion->rbXY = new Point(0.0375, 0);
$expectedRegion->rbRx = null;
$expectedRegion->rbH = '1';
$expectedRegion->rbW = '0.8890625';

$this->assertEquals([
$expectedRegion,
], $xmp->getImageRegions());

}

/**
* @param Xmp $xmp
*/
Expand Down

0 comments on commit 457b8f7

Please sign in to comment.