XMP data.Invalid argument supplied for foreach()

Locked
steinmb
User
Posts:5
Joined:Wed May 04, 2011 8:59 am
Are you a spambot?:no
XMP data.Invalid argument supplied for foreach()

Post by steinmb » Wed May 04, 2011 10:07 am

Hi
I'm new to getID3 but found something that to me looks like a bug:
Reading this file (or other from this set): http://smbjorklund.no/sites/smbjorklund ... 3847_o.jpg (also attaching two example files)

Code: Select all

warning: warning: Invalid argument supplied for foreach() in /libraries/getid3/getid3/module.graphic.jpg.php on line 157..
New to getID3 but I had to try some debugging :) dumped out $xmp_raw but it is empty though the Image_XMP I think is correct.

Code: Select all

object(Image_XMP)#173 (3) {
  ["_sFilename"]=>
  string(113) "/Users/steinmb/htdocs/sites/norskreiseliv/drupal/sites/default/files/brukere/1/bilder/4543378945_2ec0af3847_o.jpg"
  ["_aXMP"]=>
  bool(false)
  ["_bXMPParse"]=>
  bool(true)
}
So... getAllTags() is not returning an array? The problem is actually in XMPparse() and/or Image_XMP()? The images is jpg exports from Apple Aperture and taken with a Nikon D200.

Edit: Tested on getID3 1.8.5
Attachments
4544012776_db0f661ce2_o.jpg
4544012776_db0f661ce2_o.jpg (87.68KiB)Viewed 19374 times
4543378945_2ec0af3847_o.jpg
4543378945_2ec0af3847_o.jpg (153.51KiB)Viewed 19374 times

steinmb
User
Posts:5
Joined:Wed May 04, 2011 8:59 am
Are you a spambot?:no

Re: XMP data.Invalid argument supplied for foreach()

Post by steinmb » Wed May 04, 2011 12:18 pm

getID3 1.8.5 and PHP 5.3.x or PHP 5.2.x

I did a little more debugging. From Image_XMP() and the $xmp_data:

Code: Select all

xmp_data variable dump from Image_XMP() => <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about=""
            xmlns:xmp="http://ns.adobe.com/xap/1.0/">
         <xmp:Rating>3</xmp:Rating>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta>
aaaah, so we need to check that _aXMP is defined event if there where xmp_data present (see the prev. post where it is False). So all we need is to wrap it in a if?

Code: Select all

diff --git module.graphic.jpg.php module.graphic.jpg.php
index d74507f..a4794a4 100644
--- module.graphic.jpg.php
+++ module.graphic.jpg.php
@@ -153,11 +153,13 @@ class getid3_jpg
 			if (isset($ThisFileInfo['filenamepath'])) {
 				$image_xmp = new Image_XMP($ThisFileInfo['filenamepath']);
 				$xmp_raw = $image_xmp->getAllTags();
-				foreach ($xmp_raw as $key => $value) {
-					list($subsection, $tagname) = explode(':', $key);
-					$ThisFileInfo['xmp'][$subsection][$tagname] = $this->CastAsAppropriate($value);
-				}
-			}
+        if ( (array) $image_xmp->_aXMP  === $image_xmp->_aXMP ) {
+        	foreach ($xmp_raw as $key => $value) {
+        		list($subsection, $tagname) = explode(':', $key);
+        		$ThisFileInfo['xmp'][$subsection][$tagname] = $this->CastAsAppropriate($value);
+        	}
+        }			
+      }
 		}
 
 		if (!$returnOK) {
Attachments
patch_xmp.txt
(864Bytes)Downloaded 1064 times

Pernod
getID3() contributor
Posts:103
Joined:Sat Mar 21, 2009 5:30 pm
Are you a spambot?:no
Location:London, UK
Contact:

Re: XMP data.Invalid argument supplied for foreach()

Post by Pernod » Wed May 04, 2011 2:34 pm

Wouldn't replacing your:

Code: Select all

if ( (array) $image_xmp->_aXMP  === $image_xmp->_aXMP ) {
with

Code: Select all

if ( $image_xmp->isValid() ) {
be a better solution?

steinmb
User
Posts:5
Joined:Wed May 04, 2011 8:59 am
Are you a spambot?:no

Re: XMP data.Invalid argument supplied for foreach()

Post by steinmb » Wed May 04, 2011 3:28 pm

Hi Pernod and thanx for your swift reply :)

You might be right, I'm not a PHP ninja :) (still wearing pink)
I found the method reading up on http://php.net/manual/en/function.is-array.php. It is a quicker (performance wise) way to test if it really is an array before we go on an iterate over it. For more details on this way of doing it, search in http://php.net/manual/en/function.is-array.php after "hperrin at gmail dot com 28-May-2010"

Pernod
getID3() contributor
Posts:103
Joined:Sat Mar 21, 2009 5:30 pm
Are you a spambot?:no
Location:London, UK
Contact:

Re: XMP data.Invalid argument supplied for foreach()

Post by Pernod » Wed May 04, 2011 4:02 pm

I am not disputing your method of testing whether it is actually an array, although it should be even if it is empty.

What I'm saying is that the $image_xmp->isValid() flag should be true only if we have valid XMP data so there is no need to test the array, I haven't tested this yet though.

steinmb
User
Posts:5
Joined:Wed May 04, 2011 8:59 am
Are you a spambot?:no

Re: XMP data.Invalid argument supplied for foreach()

Post by steinmb » Fri May 06, 2011 8:20 am

Sorry about the delay :-/ Did a few tests with $image_xmp->isValid() but it slips through. Just to make sure that you understand the problem:

Code: Select all

object(Image_XMP)#174 (3) {
  ["_sFilename"]=>
  string(115) "/Users/steinmb/htdocs/sites/norskreiseliv/drupal/sites/default/files/brukere/1/bilder/4543378945_2ec0af3847_o_4.jpg"
  ["_aXMP"]=>
  bool(false)
  ["_bXMPParse"]=>
  bool(true)
} 
Is the non valid condition (uploaded one of the attached images), that we should not iterate over.

Pernod
getID3() contributor
Posts:103
Joined:Sat Mar 21, 2009 5:30 pm
Are you a spambot?:no
Location:London, UK
Contact:

Re: XMP data.Invalid argument supplied for foreach()

Post by Pernod » Fri May 06, 2011 6:40 pm

There's another issue here. I've been looking at your test images and they do contain a XMP rating which is not being identified. I will spend some more time on this and report back...

Pernod
getID3() contributor
Posts:103
Joined:Sat Mar 21, 2009 5:30 pm
Are you a spambot?:no
Location:London, UK
Contact:

Re: XMP data.Invalid argument supplied for foreach()

Post by Pernod » Fri May 06, 2011 10:16 pm

The issue was caused by the XMP block being null terminated. This is not part of the Adobe specification for XMP so maybe a quirk of the Apple software you use to rate the photos?

This null terminated block was causing the PHP XML parser to fail with a 'Invalid document end' error. The attached solution simply trims the $xmp_data of null bytes before passing it to the XML parser.
Attachments
module.tag.xmp.php
(19.72KiB)Downloaded 1095 times

steinmb
User
Posts:5
Joined:Wed May 04, 2011 8:59 am
Are you a spambot?:no

Re: XMP data.Invalid argument supplied for foreach()

Post by steinmb » Tue May 10, 2011 11:51 am

Hi
I have tested the changes, and they look good, RTBC from me. You are right, this is a much better place to fix the problem then where I started out, then fixing stuff there is more hackish :)

The image export is done http://www.apple.com/aperture/what-is.html and is a pro. tool, but what version that created it do I not know. Might have been Aperture 2.x or 3.x then I have had both, and like most other programs does this also include it's bugs. I did some research on Aperture XMP-related issues - http://www.controlledvocabulary.com/ima ... ture3.html
and a overview http://spreadsheets.google.com/pub?key= ... utput=html
but without knowing what release the export came from is this a dead end.

Look at the bright side, edge cases like this makes the code more bulletproof ;) Thanx for all your help and your quick replies. Do you have an ETA on when you guys roll the next release?

Best regard
Stein M. B
Norway

James Heinrich
getID3() v1 developer
Posts:1477
Joined:Fri May 04, 2001 4:00 pm
Are you a spambot?:no
Location:Northern Ontario, Canada
Contact:

Re: XMP data.Invalid argument supplied for foreach()

Post by James Heinrich » Tue May 10, 2011 2:59 pm

steinmb wrote:Do you have an ETA on when you guys roll the next release?
It's stuck in the quality-control department right now. So it could be anywhere from a couple days to several more weeks, depending on how things go. Sorry I can't be more precise than that.

Locked