[SOLVED] Need to add IPTC info to end of Description field on import

Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Thu, 2009-10-29 23:50

I'm trying to figure out how to add IPTC/City & IPTC/State info to the end of my description on image import. Basically I want my location info searchable in the database. Is it possible to import three fields into one? I think the answer lies in EXIF/ExifDescriptionOption.inc around #115 but I can't figure it out. Can someone help with the code? Here's the original.

Quote:
$itemDescription = '';
if (!empty($exifData[$itemId]['IPTC/Caption']['value'])) {
$itemDescription = $exifData[$itemId]['IPTC/Caption']['value'];
}
else if (!empty($exifData[$itemId]['ImageDescription']['value'])) {
$itemDescription = $exifData[$itemId]['ImageDescription']['value'];
}
else if (!empty($exifData[$itemId]['UserComment']['value'])) {
$itemDescription = $exifData[$itemId]['UserComment']['value'];
}

Thanks. -Matt

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Sat, 2009-10-31 01:39

Can I use CONCAT for this? I need ['IPTC/City']','['IPTC/ProvinceState'] at the end of ['IPTC/Caption'].

Any guidance on this is really appreciated.

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16503
Posted: Mon, 2009-11-02 23:31
Quote:
Basically I want my location info searchable in the database

AFAIK, the EXIF/IPTC info is not stored in the database and actually read every time the image is loaded. Which is one reason it's advised to not use the EXIF module on a heavily trafficked site as it will become a huge performance hit.
____________________________________________
Like Gallery? Like the support? Donate now!!! See G2 live here

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16503
Posted: Mon, 2009-11-02 23:35

Oh wait, I think I see what you're doing. So you're having that info imported into Gallery's description? I don't know the best approach to do that in the code.

I don't think you can use "CONCAT" for that as I don't think it's a valid PHP function, but...
http://php.net/manual/en/language.operators.string.php

____________________________________________
Like Gallery? Like the support? Donate now!!! See G2 live here

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Tue, 2009-11-03 01:15

That's what I'm looking for! I added a condition to grab the city & state info unless it's empty. Here's my change for the curious. Line #64 of ExifDescriptionOption.inc
Add

Quote:
$needed[] = 'IPTC/City';
$needed[] = 'IPTC/ProvinceState';

Line #117 change

Quote:
$itemDescription = '';
if (!empty($exifData[$itemId]['IPTC/City']['value'])) {
$itemDescription = $exifData[$itemId]['IPTC/Caption']['value'] . "-" . $exifData[$itemId]['IPTC/City']['value'] . ", " . $exifData[$itemId]['IPTC/ProvinceState']['value'];
}
else if (!empty($exifData[$itemId]['IPTC/Caption']['value'])) {
$itemDescription = $exifData[$itemId]['IPTC/Caption']['value'];
}
else if (!empty($exifData[$itemId]['ImageDescription']['value'])) {
$itemDescription = $exifData[$itemId]['ImageDescription']['value'];
}
else if (!empty($exifData[$itemId]['UserComment']['value'])) {
$itemDescription = $exifData[$itemId]['UserComment']['value'];
}

Thanks for the help. Works great in the search.

 
bplgonzo

Joined: 2009-04-03
Posts: 4
Posted: Mon, 2010-03-01 08:13

cron enhancement for adding images with IPTC data, custom fields and watermarking:

change part that import classes to add more needed classes

//import needed Gallery classes
        GalleryCoreApi::requireOnce('modules/core/classes/GalleryController.class');
        GalleryCoreApi::requireOnce('modules/core/classes/GalleryView.class');
        GalleryCoreApi::requireOnce('modules/core/ItemAdd.inc');
        GalleryCoreApi::requireOnce('modules/itemadd/ItemAddFromServer.inc');
	GalleryCoreApi::requireOnce('modules/exif/module.inc');
	GalleryCoreApi::requireOnce('modules/exif/classes/ExifExtractor.class');
	GalleryCoreApi::requireOnce('modules/customfield/classes/CustomFieldHelper.class');

change FOREACH loop that adds items to process IPTC to Description, IPTC to custom fields (in this example Author name and Country name from IPTC populates values for Custom Fields) and to automatically watermark images.

	    $i = 0;
            foreach ($files as $file)
            {    
                try {
                $ret = $this->itemAdd->addItem($this->album_id, $file, false, $this->text_fields, $this->status, $this->error);		
		$itemId = $this->status['addedFiles'][$i]['id'];
		$i++;
		$this->check($ret);		
		$exifExtractor = new ExifExtractor();
		$exifData = $exifExtractor->getMetaData(array($itemId));
		// load gallery item
                list ($ret, $item) = GalleryCoreApi::loadEntitiesById($itemId, 'GalleryItem');
            	if ($ret) {
            		return array($ret, null);
            	}
            	// lock gallery item
                list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock(array($itemId));
            		if ($ret) {
            		    return array($ret, null, null);
            		}
            	// set properties
            	$item->setDescription($exifData[1][$itemId]['IPTC/Caption']['value']);
            	$item->setKeywords($exifData[1][$itemId]['IPTC/Keywords']['value']);
            	// save
            	$ret = $item->save();
            	// unlock gallery item
                if ($ret) {
        		GalleryCoreApi::releaseLocks(array($lockId));
        		return array($ret, null, null);
        	}
                $ret = GalleryCoreApi::releaseLocks(array($lockId));
            	if ($ret) {
            		return array($ret, null, null);
            	}
            	// set custom parameters: author, country
            	$extraParams = array(
            	    'Country' => $exifData[1][$itemId]['IPTC/CountryName']['value'], 
            	    'Author' => $exifData[1][$itemId]['IPTC/Byline']['value']
            	);
            	CustomFieldHelper::setCustomFieldValues($itemId, $extraParams);
            	// add watermark to resized image
            	// load watermark images
            	GalleryCoreApi::requireOnce('modules/watermark/classes/WatermarkHelper.class');
            	list ($ret, $watermarks) = WatermarkHelper::fetchWatermarks();
            	$watermark = array_shift($watermarks);
            	$which = array(
            	    DERIVATIVE_TYPE_IMAGE_PREFERRED => false,
            	    DERIVATIVE_TYPE_IMAGE_RESIZE => true,
            	    DERIVATIVE_TYPE_IMAGE_THUMBNAIL => false
            	);
            	$ret = WatermarkHelper::watermarkItem($watermark, $item, $watermark->xPercentage, $watermark->yPercentage, $which);
        		if ($ret) {
        			return array($ret, null, null, null);
        		}
                } catch (Exception $x) {
                    $this->log_error($x->getMessage() . ': ' . $x->getTraceAsString());
                }
            }