Jump to content

iptcembed & keywords array


micheall76

Recommended Posts

Ok, so first, I'm a little more experienced than a newb, but far from an expert.  And I'm fairly certain when yall help me through this I'm going to bang my head on the desk and chastize myself for not thinking of it.  So here we go.

 

I'm attempting to use some examples I found on php.net for the iptc embed & iptcparse functions.  What I've done is taken a class file that was posted (just a collection of the snippets), corrected it for the most part to function how it should.  It will read the IPTC data from a jpg just fine.  It'll even write to the IPTC fields.  The problem comes with the IPTC_KEYWORDS field.  That field needs to be an array and I can't get it to write properly.  Right now the only thing it's writing is the word "array".

 

Also, I'm a bit confused as to what one of the functions is trying to do and I think it's the function that's causing the mess.  Below are the classfile and then the test file making the call.

<?

/*
Examples taken from php.net
*/

    DEFINE('IPTC_OBJECT_NAME', '005');
    DEFINE('IPTC_EDIT_STATUS', '007');
    DEFINE('IPTC_PRIORITY', '010');
    DEFINE('IPTC_CATEGORY', '015');
    DEFINE('IPTC_SUPPLEMENTAL_CATEGORY', '020');
    DEFINE('IPTC_FIXTURE_IDENTIFIER', '022');
    DEFINE('IPTC_KEYWORDS', '025');
    DEFINE('IPTC_RELEASE_DATE', '030');
    DEFINE('IPTC_RELEASE_TIME', '035');
    DEFINE('IPTC_SPECIAL_INSTRUCTIONS', '040');
    DEFINE('IPTC_REFERENCE_SERVICE', '045');
    DEFINE('IPTC_REFERENCE_DATE', '047');
    DEFINE('IPTC_REFERENCE_NUMBER', '050');
    DEFINE('IPTC_CREATED_DATE', '055');
    DEFINE('IPTC_CREATED_TIME', '060');
    DEFINE('IPTC_ORIGINATING_PROGRAM', '065');
    DEFINE('IPTC_PROGRAM_VERSION', '070');
    DEFINE('IPTC_OBJECT_CYCLE', '075');
    DEFINE('IPTC_BYLINE', '080');
    DEFINE('IPTC_BYLINE_TITLE', '085');
    DEFINE('IPTC_CITY', '090');
    DEFINE('IPTC_PROVINCE_STATE', '095');
    DEFINE('IPTC_COUNTRY_CODE', '100');
    DEFINE('IPTC_COUNTRY', '101');
    DEFINE('IPTC_ORIGINAL_TRANSMISSION_REFERENCE',     '103');
    DEFINE('IPTC_HEADLINE', '105');
    DEFINE('IPTC_CREDIT', '110');
    DEFINE('IPTC_SOURCE', '115');
    DEFINE('IPTC_COPYRIGHT_STRING', '116');
    DEFINE('IPTC_CAPTION', '120');
    DEFINE('IPTC_LOCAL_CAPTION', '121');

    class iptc {
        var $meta=Array();
        var $hasmeta=false;
        var $file=false;
        
        
        function iptc($filename) {
		echo 'IPTC Loading for: '.$filename.'<br />';
            $size = getimagesize($filename,$info);
            $this->hasmeta = isset($info["APP13"]);
            if($this->hasmeta)
                $this->meta = iptcparse($info["APP13"]);
            $this->file = $filename;
        }

        function set($tag, $data) {
		echo 'Updating IPTC Tag.<br />';
            $this->meta ["2#$tag"]= Array( $data );
            $this->hasmeta=true;
        }

        function get($tag) {
		echo 'Getting IPTC data.<br />';
            return isset($this->meta["2#$tag"]) ? $this->meta["2#$tag"][0] : false;
        }

        function view() {
		echo 'Print IPTC Data.<br />';
			foreach(array_keys($this->meta) as $s) {              
				$c = count ($this->meta[$s]);
				for ($i=0; $i <$c; $i++) 
				{
               			echo $s.' = '.$this->meta[$s][$i].'<br />';
           			}
       			}	                  
    	}

        function binary() {
		echo 'Setting new binary block for IPTC writing.<br />';
            $iptc_new = '';
            foreach (array_keys($this->meta) as $s) {
		$c = count ($this->meta[$s]);
		for ($i=0; $i <$c; $i++)
		    {
				$tag = str_replace("2#", "", $s);
			    $iptc_new .= $this->iptc_maketag(2, $tag, $this->meta[$s][$i]);
		    }
            }        
            return $iptc_new;   
        }

        function iptc_maketag($rec,$dat,$val) {
		echo 'Making IPTC Tag<br />';
            $len = strlen($val);
            if ($len < 0x8000) {
                   return chr(0x1c).chr($rec).chr($dat).
                   chr($len >> .
                   chr($len & 0xff).
                   $val;
            } else {
                   return chr(0x1c).chr($rec).chr($dat).
                   chr(0x80).chr(0x04).
                   chr(($len >> 24) & 0xff).
                   chr(($len >> 16) & 0xff).
                   chr(($len >> 8 ) & 0xff).
                   chr(($len ) & 0xff).
                   $val;      
            }
        }    
        function write() {
		echo 'Writing file...<br />';
            if(!function_exists('iptcembed')) return false;
            $mode = 0;
            $content = iptcembed($this->binary(), $this->file, $mode);    
            $filename = $this->file;
                
            @unlink($filename); #delete if exists
            
            $fp = fopen($filename, "w");
            fwrite($fp, $content);
            fclose($fp);
        }    
        
        #requires GD library installed
        function removeAllTags() {
		'Removing previous IPTC tags to re-write new data.<br />';
            $this->hasmeta=false;
            $this->meta=Array();
            $img = imagecreatefromstring(implode(file($this->file)));
            @unlink($this->file); #delete if exists
            imagejpeg($img,$this->file,100);
        }
    };
?>

 

And here's the test-call:

<?php
require_once("iptceasy.php");

$i = new iptc("fortworden.jpg");
$keywords = array( "keywords" => "updatedkey1", "updatedkey2", "updatedkey3" , "updatedkey4" );
echo $i->set(IPTC_KEYWORDS, $keywords);
$i->write();
echo 'Done.';
?>

 

If I try to set any of the other IPTC fields with a string, the functions work fine.  I'm fairly certain the error is either in the set() function or the iptc_maketag() function.  And the iptc_maketag() function is the one I'm stumped about.  Is that binary that it's prepending to the data?

 

Thanks for any light you can shed and help you can give on this in advance!

-Mike

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.