Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified version of get_meta_by_path to work with FotoStation comments #4

Open
olavxxx opened this issue May 23, 2013 · 3 comments
Open

Comments

@olavxxx
Copy link

olavxxx commented May 23, 2013

/**
 * 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;
}
@olavxxx olavxxx closed this as completed May 23, 2013
@olavxxx olavxxx reopened this May 23, 2013
@olavxxx
Copy link
Author

olavxxx commented May 23, 2013

"Kalmargjerdet *** Local Caption *** Kalmargjerdet sett mot Engen før 1916. Den Nationale Scene til høyre. Foto: Brundtland. Arkivet etter arkitekt Einar Oscar Schou, Bergen Byarkiv."

More examples:
http://kart.bergenbyarkiv.no/iptc.php

The local caption is not always there, but I needed the Local Caption if it is there.
If it is not there, it should revert to the regular caption and never display both.
My code addition above does this.. You have the $pri that is set to 1 (local caption).
If $pri is between range of array, it is selected. If not, the first index is selected.

Feel free to implement this in a more flexible way, but it would be nice to be mentioned as a contributor :)

@peterhudec
Copy link
Owner

@olavxxx Sorry, for the delayed response. I don't know why I, but somehow I get no notifications about new issues.

So as I understand it, FotoStation is using the caption for storing 2 kinds of information divided by *** Local Caption *** right?

I would suggest that we keep the $this->metadata['IPTC']['Caption'] and add $this->metadata['IPTC']['FotoStation']['Caption']['Regular'] and $this->metadata['IPTC']['FotoStation']['Caption']['Local']. This would allow for maximum flexibility. The users could choose all of them in a template tag. And if there are any other FotoStation specific issues, they would go to the ['FotoStation'] namespace.

No problem by mentioning you as a contributor anywhere.
But, don't you wanna do it through a fork/pull?

@primitives
Copy link

I don't want to hijack this thread, but I've been looking everywhere for an official explanation of the Local Caption field and I can't find anything on it. Even ExifTool's Phil Harvey doesn't seem to know what it is, as he says on his site (Quote: "I haven't found a reference for the format of tags 121, 184-188 and 225-232, so I have just make them writable as strings with reasonable length. Beware that if this is wrong, other utilities won't be able to read these tags as written by ExifTool"). Can someone please point me to the official info on that tag? What is it used for? What is its format? Max length?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants