wppaste
WordPress

wp_read_image_metadata( string $file ): array|false

Since
2.5.0
Source
wp-admin/includes/image.php:825
Gets extended image metadata, exif or iptc as available.

Description

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, alt, and title. Also includes FNumber, Model, DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filestring

Return value

array|false
Image metadata array on success, false on failure.

Performance profile

How much work a call to wp_read_image_metadata() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
56–151

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 410.

Plugin surface
2 hooks

Third-party callbacks on 'wp_read_image_metadata_types', 'wp_read_image_metadata' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_read_image_metadata() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
file_exists() && $image_size !== false && empty($info)56–61file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($exif) && empty($value) && !empty($meta)99–130file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value)108–130file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)114–138file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)114–135file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), reset(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value)123–138file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value)123–132file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), reset(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)129–143file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), reset(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)129–146file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)129–143file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), reset(), wp_kses_post_deep(), apply_filters()
file_exists() && $image_size !== false && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta)144–151file_exists(), wp_getimagesize(), wp_getimagesize(), wp_get_image_alttext(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), reset(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev40956–150671 fewer instruction than PHP 8.5
8.541056–15167
8.441056–1516736 fewer instructions than PHP 8.3
8.344656–15667
8.244656–156671 fewer instruction than PHP 8.1
8.144757–157677 fewer instructions than PHP 7.4
7.445457–16167

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_read_image_metadata() runs, in this order:

  1. apply_filters( wp_read_image_metadata_types )filterline 942 (+117 into the body)

    Filters the image types to check for exif data.

  2. apply_filters( wp_read_image_metadata )filterline 1077 (+252 into the body)

    Filters the array of meta data read from an image's exif data.

Uses · 10

Used by · 4

Source code

function wp_read_image_metadata( $file ) {	if ( ! file_exists( $file ) ) {		return false;	} 	$image_size = wp_getimagesize( $file ); 	if ( false === $image_size ) {		return false;	} 	list( , , $image_type ) = $image_size; 	/*	 * EXIF contains a bunch of data we'll probably never need formatted in ways	 * that are difficult to use. We'll normalize it and just extract the fields	 * that are likely to be useful. Fractions and numbers are converted to	 * floats, dates to unix timestamps, and everything else to strings.	 */	$meta = array(		'aperture'          => 0,		'credit'            => '',		'camera'            => '',		'caption'           => '',		'created_timestamp' => 0,		'copyright'         => '',		'focal_length'      => 0,		'iso'               => 0,		'shutter_speed'     => 0,		'title'             => '',		'orientation'       => 0,		'keywords'          => array(),		'alt'               => '',	); 	$iptc = array();	$info = array();	/*	 * Read IPTC first, since it might contain data not available in exif such	 * as caption, description etc.	 */	if ( is_callable( 'iptcparse' ) ) {		wp_getimagesize( $file, $info ); 		if ( ! empty( $info['APP13'] ) ) {			// Don't silence errors when in debug mode, unless running unit tests.			if ( defined( 'WP_DEBUG' ) && WP_DEBUG				&& ! defined( 'WP_RUN_CORE_TESTS' )			) {				$iptc = iptcparse( $info['APP13'] );			} else {				// Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480				$iptc = @iptcparse( $info['APP13'] );			} 			if ( ! is_array( $iptc ) ) {				$iptc = array();			} 			// Headline, "A brief synopsis of the caption".			if ( ! empty( $iptc['2#105'][0] ) ) {				$meta['title'] = trim( $iptc['2#105'][0] );				/*				* Title, "Many use the Title field to store the filename of the image,				* though the field may be used in many ways".				*/			} elseif ( ! empty( $iptc['2#005'][0] ) ) {				$meta['title'] = trim( $iptc['2#005'][0] );			} 			if ( ! empty( $iptc['2#120'][0] ) ) { // Description / legacy caption.				$caption = trim( $iptc['2#120'][0] ); 				mbstring_binary_safe_encoding();				$caption_length = strlen( $caption );				reset_mbstring_encoding(); 				if ( empty( $meta['title'] ) && $caption_length < 80 ) {					// Assume the title is stored in 2:120 if it's short.					$meta['title'] = $caption;

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/image.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.