IPTC keyword field 2#025, please help me output comma separated keywords

Hi folks.

I am really stuck on a good solution for parsing images (jpgs) for their keywords 2#025.
The keywords are comma separated like this:
Hello there, water, wind power, æble, how are you today?, lovely color that is, etc, etc

This is the code I am using now. This code only grabs the first word or sentence: Hello there and then it stops. So it doesn't like commas?

        $store_exif_keywords = array();
        if (isset($image_info["APP13"]))
        {
            $iptc = iptcparse($image_info["APP13"]);
            $store_exif_keywords = $iptc["2#025"][0];
        }
        else
        {
            $store_exif_keywords = "";
        }    
		
        $store_exif_keywords = str_replace(chr(0xbe), "æ", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xae), "Æ", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xbf), "ø", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xaf), "Ø", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x81), "Å", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x8c), "å", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x8a), "ä", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x80), "Ä", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x9a), "ö", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x85), "Ö", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x9f), "ü", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x86), "Ü", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xa7), "ß", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xad), "'", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xac), "¨", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xab), "´", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xa9), "©", $store_exif_keywords);
		
		
        $store_exif_keywords = utf8_decode($store_exif_keywords);

Open in new window


Please advise on how I may output the keywords in a list form. Help me describe where I put in a \n for new line for each keyword or if I want a comma for each as well. Need to be flexible.

Thank you so much.
sofuscomerAsked:
Who is Participating?
 
Ray PaseurCommented:
This appears to be a completely different issue, something about the way DreamWeaver functions may be damaging the data strings.  Sorry to have to say this, but DreamWeaver contains some of the worst PHP code ever written, and it is a struggle to get it to work in a sensible manner.  You might want to post a separate question here at EE about how to use DreamWeaver functions to construct MySQL queries.

If I were stuck with a DreamWeaver site, I might try printing out the intermediate variables to see what DreamWeaver is doing to them  Example:
var_dump( GetSQLValueString($store_exif_keywords), "text") );

Open in new window


If this were my web site, I would have separate tables for the images and the keywords.  I would have a junction table containing the keys of the rows in the image table and the keywords table.  Each row of the junction table would contain one relationship between an image and a keyword.  You can have as many images as you want and as many keywords as you want.  The junction table is what makes the data base "relational."
0
 
Ray PaseurCommented:
After line 4, please insert this statement and post the output here.
var_dump($iptc);

Open in new window


Even better: Please post a link to the test image you are using, so we can recreate the tests and experiment with different ways of looking at the data.

thanks, ~Ray
0
 
sofuscomerAuthor Commented:
Hi Ray, and thanks for helping out here.

I am still a novice when handling arrays. Cant get my head around it yet.

So I made a new test page, that only has the code i sent you:

<?php
		$file = "14-03-2012_048.jpg";
		$image_size = getimagesize($file, 
            $image_info);
		

		$iptc = iptcparse($image_info["APP13"]);
			var_dump($iptc);
			
            $store_exif_keywords = $iptc["2#025"][0];
        }
        else
        {
            $store_exif_keywords = "";
        }    
		
        $store_exif_keywords = str_replace(chr(0xbe), "æ", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xae), "Æ", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xbf), "ø", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xaf), "Ø", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x81), "Å", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x8c), "å", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x8a), "ä", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x80), "Ä", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x9a), "ö", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x85), "Ö", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x9f), "ü", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0x86), "Ü", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xa7), "ß", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xad), "'", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xac), "¨", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xab), "´", $store_exif_keywords);
        $store_exif_keywords = str_replace(chr(0xa9), "©", $store_exif_keywords);
		
		
        $store_exif_keywords = utf8_decode($store_exif_keywords);
		
?>

Open in new window


The page with this code is live here: http://sofuscomer.com/raypaseur/parsekeywords.php

My test image which has keywords in it is here: http://sofuscomer.com/raypaseur/14-03-2012_048.jpg

Regards Sofus
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Ray PaseurCommented:
Adorable kid!

The output here will contain the var_dump() information.  I just use that for data visualization to make sure my programming is operating on useful data.  You can ignore most of it and just look at the bottom of the resulting web page.
http://www.laprbass.com/RAY_iptc.php

Try this script, and please post back if you have any questions.  Best regards, ~Ray
<?php // RAY_iptc.php
error_reporting(E_ALL);
echo "<pre>";

// IMAGE
$url = 'http://sofuscomer.com/raypaseur/14-03-2012_048.jpg';

// MAN PAGE http://php.net/manual/en/function.getimagesize.php
$arr = getImageSize($url, $app);

// MAN PAGE http://php.net/manual/en/function.iptcparse.php
$i13 = IPTCParse($app['APP13']);

echo "getImageSize() RETURN ARRAY: ";
echo PHP_EOL;
var_dump($arr);
echo PHP_EOL . PHP_EOL;

echo "getImageSize() ADDITIONAL INFO: ";
echo PHP_EOL;
var_dump($app);
echo PHP_EOL . PHP_EOL;

echo "IPTCParse('APP13') RETURN ARRAY: ";
echo PHP_EOL;
var_dump($i13);
echo PHP_EOL . PHP_EOL;


// PROCESS THE KEYWORDS
$exif_keywords = $i13["2#025"];
foreach ($exif_keywords as $kw)
{
    // DECODE AND PRINT EACH OF THE KEYWORDS
    echo PHP_EOL . utf8_decode($kw);
}

Open in new window

0
 
sofuscomerAuthor Commented:
Thanks,
I tried this, with your exact code, and I get this result on:
http://sofuscomer.com/raypaseur/parsekeywords.php

getImageSize() RETURN ARRAY:
bool(false)


getImageSize() ADDITIONAL INFO:
array(0) {
}


IPTCParse('APP13') RETURN ARRAY:
bool(false)


Should it not output some more keywords and info?
0
 
Ray PaseurCommented:
Interesting.  Is "allow_url_fopen" set to "on" in your PHP installation?
http://php.net/manual/en/function.getimagesize.php#102902
0
 
sofuscomerAuthor Commented:
No it was turned off. I just turned it on and in about 15 minutes I will run the test again. Thanks so far :-)
0
 
Ray PaseurCommented:
That may do the trick for you.  When getimagesize() returns FALSE, there is almost certainly something wrong with its effort to read the image file.  Perhaps the file was not found.  It might be not found because the name was spelled wrong, but it could also be not found because the system did not allow the script to open it.  

You may need to restart the server after you change the allow_url_fopen directive (not sure about that).
0
 
sofuscomerAuthor Commented:
Hi Ray, now its working nicely.

http://sofuscomer.com/raypaseur/parsekeywords.php

This is really great!!!

Now all I need is to integrate it into the code i am working with.
Will try this and get back to you.
0
 
Ray PaseurCommented:
Please show me the code that produced this page:
http://sofuscomer.com/raypaseur/parsekeywords.php
0
 
sofuscomerAuthor Commented:
"Please show me the code that produced this page:"
Here is the code:
<?php // RAY_iptc.php
error_reporting(E_ALL);
echo "<pre>";

// IMAGE
$url = 'http://sofuscomer.com/raypaseur/14-03-2012_048.jpg';

// MAN PAGE http://php.net/manual/en/function.getimagesize.php
$arr = getImageSize($url, $app);

// MAN PAGE http://php.net/manual/en/function.iptcparse.php
$i13 = IPTCParse($app['APP13']);


// PROCESS THE KEYWORDS
$exif_keywords = $i13["2#025"];
foreach ($exif_keywords as $kw)
{
    // DECODE AND PRINT EACH OF THE KEYWORDS
    echo utf8_encode($kw).", ";
}
?>

Open in new window

0
 
Ray PaseurCommented:
Try changing line 20 to this:
utf8_DEcode

Open in new window

0
 
Ray PaseurCommented:
I believe that there is a unicode string in the last position of the exif keywords.  The PHP function to decode that string may make it readable on a web page.
0
 
sofuscomerAuthor Commented:
Ok, I can now echo the keywords thanks to your script.
But when I insert the keywords, It only inserts the first keyword before the first comma.
I am guessing that there is something in my query insert statement???
Could it be that I need some implode code in this code GetSQLValueString($store_exif_keywords), "text"), on the second bottom code line below?

I use MYSQL

//Read EXIF data Keyword(s)
		// IMAGE
		$url = $file;

		$arr = getImageSize($url, $app);

		$i13 = IPTCParse($app['APP13']);

		// PROCESS THE KEYWORDS
		$exif_keywords = $i13["2#025"];
		foreach ($exif_keywords as $kw)
		{
    	// DECODE AND PRINT EACH OF THE KEYWORDS
	    $store_exif_keywords =  PHP_EOL . utf8_DEcode($kw);
    	
		}

Open in new window


And here is my insert :

//Assemble Insert Query
		$queryinsert = sprintf("INSERT INTO my_table (keywords) VALUES (%s,%s,%s)",
            GetSQLValueString($store_title."\n".$store_besk, "text"),
            GetSQLValueString($store_exif_keywords), "text"),
            GetSQLValueString($some_id, "int"));	
	    mysql_query($queryinsert);

Open in new window

0
 
sofuscomerAuthor Commented:
Didnt make it, as I wanted, But accepted Rays point and advice on getting out of Dreamweaver Code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.