Solved

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

Posted on 2012-03-15
15
383 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:sofuscomer
  • 8
  • 7
15 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:sofuscomer
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:sofuscomer
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Interesting.  Is "allow_url_fopen" set to "on" in your PHP installation?
http://php.net/manual/en/function.getimagesize.php#102902
0
 

Author Comment

by:sofuscomer
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sofuscomer
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please show me the code that produced this page:
http://sofuscomer.com/raypaseur/parsekeywords.php
0
 

Author Comment

by:sofuscomer
Comment Utility
"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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Try changing line 20 to this:
utf8_DEcode

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:sofuscomer
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:sofuscomer
Comment Utility
Didnt make it, as I wanted, But accepted Rays point and advice on getting out of Dreamweaver Code.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now