<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[13244] trunk/wp-admin/includes: Improved IPTC and EXIF handling.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13244">13244</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-02-20 09:17:34 +0000 (Sat, 20 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Improved IPTC and EXIF handling. Drag a wider net and also try to avoid numeric attachment slugs where possible. Props miqrogroove. See <a href="http://trac.wordpress.org/ticket/11417">#11417</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesimagephp">trunk/wp-admin/includes/image.php</a></li>
<li><a href="#trunkwpadminincludesmediaphp">trunk/wp-admin/includes/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesimagephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/image.php (13243 => 13244)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/image.php        2010-02-20 08:19:44 UTC (rev 13243)
+++ trunk/wp-admin/includes/image.php        2010-02-20 09:17:34 UTC (rev 13244)
</span><span class="lines">@@ -252,26 +252,68 @@
</span><span class="cx">         // as caption, description etc
</span><span class="cx">         if ( is_callable('iptcparse') ) {
</span><span class="cx">                 getimagesize($file, $info);
</span><ins>+
</ins><span class="cx">                 if ( !empty($info['APP13']) ) {
</span><span class="cx">                         $iptc = iptcparse($info['APP13']);
</span><ins>+
+                        if ( ! empty($iptc['2#105'][0] ) ) // headline, "A brief synopsis of the caption."
+                                $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) );
+                        elseif ( !empty($iptc['2#005'][0]) ) // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
+                                $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) );
+
+                        if ( !empty( $iptc['2#120'][0] ) ) { // description / legacy caption
+                                $caption = utf8_encode( trim( $iptc['2#120'][0] ) );
+                                if ( empty( $meta['title'] ) ) {
+                                        // Assume the title is stored in 2:120 if it's short.
+                                        if ( strlen( $caption ) < 80 )
+                                                $meta['title'] = $caption;
+                                        else
+                                                $meta['caption'] = $caption;
+                                } elseif ( $caption != $meta['title'] ) {
+                                        $meta['caption'] = $caption;
+                                }
+                        }
+
</ins><span class="cx">                         if ( !empty($iptc['2#110'][0]) ) // credit
</span><span class="cx">                                 $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
</span><del>-                        elseif ( !empty($iptc['2#080'][0]) ) // byline
</del><ins>+                        elseif ( !empty($iptc['2#080'][0]) ) // creator / legacy byline
</ins><span class="cx">                                 $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
</span><ins>+
</ins><span class="cx">                         if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time
</span><span class="cx">                                 $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
</span><del>-                        if ( !empty($iptc['2#120'][0]) ) // caption
-                                $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
</del><ins>+
</ins><span class="cx">                         if ( !empty($iptc['2#116'][0]) ) // copyright
</span><span class="cx">                                 $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
</span><del>-                        if ( !empty($iptc['2#005'][0]) ) // title
-                                $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));
</del><span class="cx">                  }
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         // fetch additional info from exif if available
</span><span class="cx">         if ( is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)) ) ) {
</span><span class="cx">                 $exif = @exif_read_data( $file );
</span><ins>+
+                if ( !empty( $exif['Title'] ) )
+                        $meta['title'] = utf8_encode( trim( $exif['Title'] ) );
+
+                if ( ! empty( $exif['ImageDescription'] ) ) {
+                        if ( empty($meta['title']) && strlen( $exif['ImageDescription'] ) < 80 ) {
+                                // Assume the title is stored in ImageDescription
+                                $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) );
+                                if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
+                                        $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) );
+                        } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
+                                $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) );
+                        }
+                } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
+                        $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) );
+                }
+
+                if ( ! empty( $exif['Artist'] ) )
+                        $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) );
+                elseif ( ! empty($exif['Author'] ) )
+                        $meta['credit'] = utf8_encode( trim( $exif['Author'] ) );
+
+                if ( ! empty( $exif['Copyright'] ) )
+                        $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) );
</ins><span class="cx">                 if (!empty($exif['FNumber']))
</span><span class="cx">                         $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
</span><span class="cx">                 if (!empty($exif['Model']))
</span></span></pre></div>
<a id="trunkwpadminincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/media.php (13243 => 13244)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/media.php        2010-02-20 08:19:44 UTC (rev 13243)
+++ trunk/wp-admin/includes/media.php        2010-02-20 09:17:34 UTC (rev 13244)
</span><span class="lines">@@ -208,9 +208,9 @@
</span><span class="cx">
</span><span class="cx">         // use image exif/iptc data for title and caption defaults if possible
</span><span class="cx">         if ( $image_meta = @wp_read_image_metadata($file) ) {
</span><del>-                if ( trim($image_meta['title']) )
</del><ins>+                if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
</ins><span class="cx">                         $title = $image_meta['title'];
</span><del>-                if ( trim($image_meta['caption']) )
</del><ins>+                if ( trim( $image_meta['caption'] ) )
</ins><span class="cx">                         $content = $image_meta['caption'];
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -259,9 +259,9 @@
</span><span class="cx">
</span><span class="cx">         // use image exif/iptc data for title and caption defaults if possible
</span><span class="cx">         if ( $image_meta = @wp_read_image_metadata($file) ) {
</span><del>-                if ( trim($image_meta['title']) )
</del><ins>+                if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
</ins><span class="cx">                         $title = $image_meta['title'];
</span><del>-                if ( trim($image_meta['caption']) )
</del><ins>+                if ( trim( $image_meta['caption'] ) )
</ins><span class="cx">                         $content = $image_meta['caption'];
</span><span class="cx">         }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>