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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sofuscomerAuthor Commented:
Didnt make it, as I wanted, But accepted Rays point and advice on getting out of Dreamweaver Code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.