wp_read_image_metadata

Get extended image metadata, exif or iptc as available.

Description

(bool|array) wp_read_image_metadata( (string) $file ); 

Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso created_timestamp, focal_length, shutter_speed, and title.

The IPTC metadata that is retrieved is APP13, credit, byline, created date and time, caption, copyright, and title. Also includes FNumber, Model, DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.

Returns (bool|array)

False on failure. Image metadata array on success.

Parameters (1)

0. $file (string)
The file.

Usage

  1. if ( !function_exists( 'wp_read_image_metadata' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image.php'; 
  3.  
  4. // The file. 
  5. $file = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_read_image_metadata($file); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/image.php  
  1. function wp_read_image_metadata( $file ) { 
  2. if ( ! file_exists( $file ) ) 
  3. return false; 
  4.  
  5. list( , , $sourceImageType ) = getimagesize( $file ); 
  6.  
  7. /** 
  8. * EXIF contains a bunch of data we'll probably never need formatted in ways 
  9. * that are difficult to use. We'll normalize it and just extract the fields 
  10. * that are likely to be useful. Fractions and numbers are converted to 
  11. * floats, dates to unix timestamps, and everything else to strings. 
  12. */ 
  13. $meta = array( 
  14. 'aperture' => 0,  
  15. 'credit' => '',  
  16. 'camera' => '',  
  17. 'caption' => '',  
  18. 'created_timestamp' => 0,  
  19. 'copyright' => '',  
  20. 'focal_length' => 0,  
  21. 'iso' => 0,  
  22. 'shutter_speed' => 0,  
  23. 'title' => '',  
  24. 'orientation' => 0,  
  25. 'keywords' => array(),  
  26. ); 
  27.  
  28. $iptc = array(); 
  29. /** 
  30. * Read IPTC first, since it might contain data not available in exif such 
  31. * as caption, description etc. 
  32. */ 
  33. if ( is_callable( 'iptcparse' ) ) { 
  34. getimagesize( $file, $info ); 
  35.  
  36. if ( ! empty( $info['APP13'] ) ) { 
  37. $iptc = iptcparse( $info['APP13'] ); 
  38.  
  39. // Headline, "A brief synopsis of the caption." 
  40. if ( ! empty( $iptc['2#105'][0] ) ) { 
  41. $meta['title'] = trim( $iptc['2#105'][0] ); 
  42. /** 
  43. * Title, "Many use the Title field to store the filename of the image,  
  44. * though the field may be used in many ways." 
  45. */ 
  46. } elseif ( ! empty( $iptc['2#005'][0] ) ) { 
  47. $meta['title'] = trim( $iptc['2#005'][0] ); 
  48.  
  49. if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption 
  50. $caption = trim( $iptc['2#120'][0] ); 
  51.  
  52. $caption_length = strlen( $caption ); 
  53.  
  54. if ( empty( $meta['title'] ) && $caption_length < 80 ) { 
  55. // Assume the title is stored in 2:120 if it's short. 
  56. $meta['title'] = $caption; 
  57.  
  58. $meta['caption'] = $caption; 
  59.  
  60. if ( ! empty( $iptc['2#110'][0] ) ) // credit 
  61. $meta['credit'] = trim( $iptc['2#110'][0] ); 
  62. elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline 
  63. $meta['credit'] = trim( $iptc['2#080'][0] ); 
  64.  
  65. if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time 
  66. $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 
  67.  
  68. if ( ! empty( $iptc['2#116'][0] ) ) // copyright 
  69. $meta['copyright'] = trim( $iptc['2#116'][0] ); 
  70.  
  71. if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array 
  72. $meta['keywords'] = array_values( $iptc['2#025'] ); 
  73.  
  74. /** 
  75. * Filters the image types to check for exif data. 
  76. * @since 2.5.0 
  77. * @param array $image_types Image types to check for exif data. 
  78. */ 
  79. 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 ) ) ) ) { 
  80. $exif = @exif_read_data( $file ); 
  81.  
  82. if ( ! empty( $exif['ImageDescription'] ) ) { 
  83. $description_length = strlen( $exif['ImageDescription'] ); 
  84.  
  85. if ( empty( $meta['title'] ) && $description_length < 80 ) { 
  86. // Assume the title is stored in ImageDescription 
  87. $meta['title'] = trim( $exif['ImageDescription'] ); 
  88.  
  89. if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { 
  90. $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); 
  91.  
  92. if ( empty( $meta['caption'] ) ) { 
  93. $meta['caption'] = trim( $exif['ImageDescription'] ); 
  94. } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { 
  95. $meta['caption'] = trim( $exif['Comments'] ); 
  96.  
  97. if ( empty( $meta['credit'] ) ) { 
  98. if ( ! empty( $exif['Artist'] ) ) { 
  99. $meta['credit'] = trim( $exif['Artist'] ); 
  100. } elseif ( ! empty($exif['Author'] ) ) { 
  101. $meta['credit'] = trim( $exif['Author'] ); 
  102.  
  103. if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { 
  104. $meta['copyright'] = trim( $exif['Copyright'] ); 
  105. if ( ! empty( $exif['FNumber'] ) ) { 
  106. $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); 
  107. if ( ! empty( $exif['Model'] ) ) { 
  108. $meta['camera'] = trim( $exif['Model'] ); 
  109. if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { 
  110. $meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); 
  111. if ( ! empty( $exif['FocalLength'] ) ) { 
  112. $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); 
  113. if ( ! empty( $exif['ISOSpeedRatings'] ) ) { 
  114. $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; 
  115. $meta['iso'] = trim( $meta['iso'] ); 
  116. if ( ! empty( $exif['ExposureTime'] ) ) { 
  117. $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); 
  118. if ( ! empty( $exif['Orientation'] ) ) { 
  119. $meta['orientation'] = $exif['Orientation']; 
  120.  
  121. foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { 
  122. if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { 
  123. $meta[ $key ] = utf8_encode( $meta[ $key ] ); 
  124.  
  125. foreach ( $meta['keywords'] as $key => $keyword ) { 
  126. if ( ! seems_utf8( $keyword ) ) { 
  127. $meta['keywords'][ $key ] = utf8_encode( $keyword ); 
  128.  
  129. $meta = wp_kses_post_deep( $meta ); 
  130.  
  131. /** 
  132. * Filters the array of meta data read from an image's exif data. 
  133. * @since 2.5.0 
  134. * @since 4.4.0 The `$iptc` parameter was added. 
  135. * @param array $meta Image meta data. 
  136. * @param string $file Path to image file. 
  137. * @param int $sourceImageType Type of image. 
  138. * @param array $iptc IPTC data. 
  139. */ 
  140. return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType, $iptc ); 
  141.