Solved

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

Posted on 2012-03-15
15
396 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 109

Expert Comment

by:Ray Paseur
ID: 37724137
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
ID: 37724183
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 109

Expert Comment

by:Ray Paseur
ID: 37724206
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:sofuscomer
ID: 37724228
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 109

Expert Comment

by:Ray Paseur
ID: 37724235
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
ID: 37724247
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 109

Expert Comment

by:Ray Paseur
ID: 37724302
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
 

Author Comment

by:sofuscomer
ID: 37724309
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 109

Expert Comment

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

Author Comment

by:sofuscomer
ID: 37724343
"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 109

Expert Comment

by:Ray Paseur
ID: 37724374
Try changing line 20 to this:
utf8_DEcode

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37724378
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
ID: 37724418
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 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37724465
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
ID: 37724478
Didnt make it, as I wanted, But accepted Rays point and advice on getting out of Dreamweaver Code.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SubQuery link 4 36
simple html dom php accessing table/cell  values 4 23
Formating field inside mysql query 2 15
Unwanted output from my query 5 17
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…

820 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