/**
* Extracts image metadata from the image specified by its path.
*
* @return structured array with all available metadata
*/
public function get_meta_by_path( $name, $tmp_name = NULL ) {
if ( !$tmp_name ) {
$tmp_name = $name;
}
$this->metadata = array();
// extract metadata from file
// the $meta variable will be populated with it
$size = getimagesize( $tmp_name, $meta );
// extract pathinfo and merge with size
$this->metadata['Image'] = array_merge( $size, pathinfo( $name ) );
// remove index 'dirname'
unset($this->metadata['Image']['dirname']);
// parse iptc
// IPTC is stored in the APP13 key of the extracted metadata
$iptc = iptcparse( $meta['APP13'] );
if ( $iptc ) {
// symplify array structure
foreach ( $iptc as &$i ) {
// if the array has only one item
if ( count( $i ) <= 1 ) {
$i = $i[0];
}
}
// add named copies to all found IPTC items
foreach ( $iptc as $key => $value ) {
if ( isset( $this->IPTC_MAPPING[ $key ] ) ) {
$name = $this->IPTC_MAPPING[ $key ];
// add "Caption" alias to "Caption-Caption-Abstract"
if ( $key == '2#120' ) {
/*
2013.05.23
Coded by: Olav Alexander Mjelde
Compatabilityfix for *** Local Caption *** from FotoWare FotoStation
*/
$arrCaption = array_filter(explode(" *** Local Caption *** ", utf8_encode($value)));
$nCap = count($arrCaption);
$pri = 1; /* Which to prioritize? 0 = regular caption, 1 = *** Local Caption *** from FotoStation */
switch ($nCap) {
case 0:
$value = $value;
break;
default:
(($pri <= $nCap - 1) && ($pri >= 0)) ? $value = $arrCaption[1] : $value = $arrCaption[0];
}
/* End of Compatabilityfix by Olav Alexander Mjelde*/
$this->insert_next_to_key( $iptc, $key, array( 'Caption' => $value ) );
}
$this->insert_next_to_key( $iptc, $key, array( $name => utf8_encode($value)) ); /* UTF8 encoding added 25.05.2013 by Olav Alexander Mjelde */
}
}
}
if ( $iptc ) {
$this->metadata['IPTC'] = $iptc;
}
// parse exif
$exif = NULL;
// the exif_read_data() function throws a warning if it is passed an unsupported file format.
// This warning is impossible to catch so we have to check the file mime type manually
$safe_file_formats = array(
'image/jpg',
'image/jpeg',
'image/tif',
'image/tiff',
);
if ( in_array( $size['mime'], $safe_file_formats ) ) {
$exif = exif_read_data( $tmp_name );
if ( is_array( $exif ) ) {
// add named copies of UndefinedTag:0x0000 items to $exif array
foreach ( $exif as $key => $value ) {
// check case insensitively if key begins with "UndefinedTag:"
if ( strtolower( substr( $key, 0, 13 ) ) == 'undefinedtag:' ) {
// get EXIF tag name by ID and convert it to base 16 integer
$id = intval( substr( $key, 13 ), 16 );
if ( isset( $this->EXIF_MAPPING[ $id ] ) ) {
// create copy with EXIF tag name as key
$name = $this->EXIF_MAPPING[ $id ];
//$exif[ $name ] = $value;
$this->insert_next_to_key( $exif, $key, array( $name => $value ) );
}
}
}
}
}
if ( $exif ) {
$this->metadata['EXIF'] = $exif;
}
// no need for return but good for testing
return $this->metadata;
}