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, 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
16

Executed per call on PHP 8.5. The body compiles to 458.

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

Further down the call graph this can also reach cache, query, option, serialize 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 · 1 distinct outcome

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
always16->get_span()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev45716671 fewer instruction than PHP 8.5
8.54581667
8.4458166736 fewer instructions than PHP 8.3
8.34941667
8.249416671 fewer instruction than PHP 8.1
8.149516678 fewer instructions than PHP 7.4
7.45031667

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 939 (+114 into the body)

    Filters the image types to check for exif data.

  2. apply_filters( wp_read_image_metadata )filterline 1074 (+249 into the body)

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

Uses · 9

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(),	); 	$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 6.9.7 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.