diff --git a/src/PNGMetadata.php b/src/PNGMetadata.php index 3f02921..c88e1f6 100644 --- a/src/PNGMetadata.php +++ b/src/PNGMetadata.php @@ -74,13 +74,23 @@ class PNGMetadata extends ArrayObject { * @var array * @access private */ - private $_tagsToFatten = [ - '@attributes', 'stEvt', 'stRef', 'dc', 'xmp', 'xmpRights', 'xmpMM', 'xmpBJ', 'xmpTPg', - 'xmpDM', 'pdf', 'photoshop', 'crs', 'crss', 'tiff', 'exif', 'exifEX', 'aux', 'Iptc4xmpCore', - 'Iptc4xmpExt', 'plus', 'mwg-rs', 'mwg-kw', 'dwc', 'dcterms', 'digiKam', 'kipi', 'GPano', - 'lr', 'acdsee', 'mediapro', 'expressionmedia', 'MicrosoftPhoto', 'MP', 'MPRI', 'MPReg' + private $_tagsXMP = [ + 'dc', 'xmp', 'xmpRights', 'xmpMM', 'xmpBJ', 'xmpTPg', 'xmpDM', 'pdf', 'photoshop', + 'crs', 'crss', 'tiff', 'exif', 'exifEX', 'aux', 'Iptc4xmpCore', 'Iptc4xmpExt', + 'plus', 'mwg-rs', 'mwg-kw', 'dwc', 'dcterms', 'digiKam', 'kipi', 'GPano', 'lr', + 'acdsee', 'mediapro', 'expressionmedia', 'MicrosoftPhoto', 'MP', 'MPRI', 'MPReg' ]; + /** + * The list of XMP prefix and suffix to remove. + * + * The prefix and suffix that are not necessary to be seen in the output but if its values. + * + * @var array + * @access private + */ + private $_prefSuffXMP = [ 'stRef', 'rdf', 'li', 'Alt', 'stEvt', 'Bag', 'Seq', 'crs' ]; + /** * Initializes the functions required for metadata extraction. * @@ -508,26 +518,11 @@ private function extractXMP() { } - $result = $this->extractNodesXML( $dom->documentElement ); - $this->flattenAttributes( $result ); - - foreach ( $result as $tag => $value ) { - - if ( is_array( $value ) && count( $value ) === 1 ) { - - if ( isset( $value[ 0 ] ) ) { - - $result[ $tag ] = current( $value ); - - } - - } - - } + $result = $this->flatten( $this->extractNodesXML( $dom->documentElement ) ); if ( ! empty( $result ) ) { - $this->_metadata[ 'xmp' ] = array_merge( $this->_metadata , $result ); + $this->_metadata[ 'xmp' ] = $result; } @@ -536,86 +531,88 @@ private function extractXMP() { } /** - * Extract the tag attributes and insert them in the first level of the array base. + * Extract the properties with the key '0' and insert them in the first level of the array. * * @access private * - * @param array $base Array where the attributes will be inserted. - * @param array $values Matrix that contains the attributes. + * @param array $array Matrix that contains the proprietary. * * @return void */ - private function flattenAttributes( &$base, &$values = null ) { - - foreach ( $this->_tagsToFatten as $flatten ) { + private function flatten( $array ){ - $this->flatten( $flatten, $base, $values ); + foreach( $array as $key => $value ){ - } - - } + if ( is_array( $value ) ){ - /** - * Extract the proprietary of a tag specific and insert them in the first level of the array base. - * - * @access private - * - * @param array $base Array where the attributes will be inserted. - * @param array $values Matrix that contains the attributes. - * - * @return void - */ - private function flatten( $flatten, &$base, &$values ) { + if ( isset( $value[ 0 ] ) && count( $value ) == 1 ) { - if ( $values ) { + $array[ $key ] = $value[ 0 ]; - if ( isset( $values[ $flatten ] ) ) { + if ( is_array( $array[ $key ] ) ) { - foreach ( $values[ $flatten ] as $key => $value ) { + $array[ $key ] = $this->flatten( $array[ $key ] ); - if ( isset( $base[ $key ] ) ) { + } - if ( is_array( $value ) ) { + }else{ - $this->flatten( $key, $base[ $key ], $values[ $flatten ] ); + $array[ $key ] = $this->flatten( $value ); - } else { + } - if ( $base[ $key ] != $value ) { + } - $base[ $key ] .= ',' . $value; + } - } + return $array; - } + } - } else { + /** + * Merge one or more arrays more string concatenation. + * + * @access private + * + * @param array $baseArray Array where the attributes will be inserted. + * @param array $array Matrix that contains the attributes. + * + * @return array + */ + private function arrayMerge($baseArray, $array){ - $base[ $key ] = $value; + foreach ( $array as $key => $value ) { - } + if (is_object($value)) { + $value = $value->value; + } - } + if ( isset( $baseArray[ $key ] ) ) { - unset( $values[ $flatten ] ); + if ( is_array( $value ) ) { - } + $baseArray[ $key ] = $this->arrayMerge( $baseArray[ $key ], $array[ $key ] ); - } else { + } else { - if ( isset( $base[ $flatten ] ) ) { + if ( $baseArray[ $key ] != $value ) { - foreach ( $base[ $flatten ] as $key => $value) { + $baseArray[ $key ] .= ',' . $value; - $base[ $key ] = $value; + } } - unset( $base[ $flatten ] ); + } else { + + $baseArray[ $key ] = $value; } + } + return $baseArray; + } /** @@ -640,60 +637,67 @@ function extractNodesXML( $node ) { for ( $i = 0; $i < $node->childNodes->length; $i++ ) { $child = $node->childNodes->item( $i ); - $values = $this->extractNodesXML( $child ) ; + $childValues = $this->extractNodesXML( $child ) ; if ( isset( $child->tagName ) ) { - if ( strpos( $child->tagName, 'rdf:' ) === 0 ) { + list( $prefixTagName, $suffixTagName ) = explode( ':', $child->tagName ); - if ( is_array( $values ) ) { + if ( is_array( $childValues ) ) { - $this->flattenAttributes( $output, $values ); + if ( in_array( $prefixTagName, $this->_tagsXMP ) ) { - $output = array_merge( $output, $values ); + if ( !isset( $output[ $suffixTagName ] ) ) { + + $output[ $suffixTagName ] = []; + + } + + $output[ $suffixTagName ] = + $this->arrayMerge( $output[ $suffixTagName ], $childValues ); } else { - $output[] = $values; + $output = $this->arrayMerge( $output, $childValues ); } } else { - $this->flattenAttributes( $values ); + if ( in_array( $prefixTagName, $this->_prefSuffXMP ) || + in_array( $prefixTagName, $this->_tagsXMP ) ) { - $prefixs = explode( ':', $child->tagName ); + if ( in_array( $suffixTagName, $this->_prefSuffXMP ) ) { - $output[ $prefixs[ 0 ] ][ $prefixs[ 1 ] ] = $values; + $output[] = $childValues; - } + } else { - } elseif ( $values || $values === '0' ) { + $output[ $suffixTagName ][] = $childValues; - $output = ( string ) $values; + } - } + } else { - } + $output[ $prefixTagName ][ $suffixTagName ][] = $childValues; - if ( $node->attributes->length && !is_array( $output ) ) { + } - $output = [ '@content' => $output ]; - } + } - if ( is_array( $output ) ) { + } elseif ( $childValues || $childValues === '0' ) { - if ( $node->attributes->length ) { + $output = ( string ) $childValues; - foreach ( $node->attributes as $attrName => $attrNode ) { + } - if ( $attrNode->value ) { + } - $output[ '@attributes' ][ $attrName ] = $attrNode->value; + if ( is_array( $output ) ) { - } + if ( $node->attributes->length ) { - } + $output = $this->arrayMerge( $output, $node->attributes ); }