wp_read_image_metadata( string $file ): array|false
- Since
- 2.5.0
- Source
wp-admin/includes/image.php:825
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
- Heavy
- Scaling
- Scales with input
- Instructions
- 47–142
- Plugin surface
- 2 hooks
- Called by
- 4
Reads or writes the filesystem via file_exists().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 401.
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.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()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 · 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.
| When | Instructions | Calls it makes |
|---|---|---|
file_exists() && empty($info) | 47–52 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($exif) && empty($value) && !empty($meta) | 90–121 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) | 99–121 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 105–129 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 105–126 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), reset(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) | 114–129 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) | 114–123 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_date2ts(), reset(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 120–134 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), reset(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 120–137 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), wp_exif_frac2dec(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 120–134 | file_exists(), wp_getimagesize(), wp_getimagesize(), apply_filters(), is_callable(), exif_read_data(), wp_exif_frac2dec(), reset(), wp_kses_post_deep(), apply_filters() |
file_exists() && empty($info) && is_callable() && $image_type && !defined('WP_DEBUG') && is_array($exif) && empty($value) && !empty($meta) | 135–142 | file_exists(), wp_getimagesize(), wp_getimagesize(), 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 400 | 47–166 | 67 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 401 | 47–142 | 67 | |
| 8.4 | 401 | 47–142 | 67 | 36 fewer instructions than PHP 8.3 |
| 8.3 | 437 | 47–147 | 67 | |
| 8.2 | 437 | 47–147 | 67 | 1 fewer instruction than PHP 8.1 |
| 8.1 | 438 | 48–148 | 67 | 7 fewer instructions than PHP 7.4 |
| 7.4 | 445 | 48–152 | 67 |
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:
- apply_filters( wp_read_image_metadata_types )filterline 933 (+108 into the body)
Filters the image types to check for exif data.
- apply_filters( wp_read_image_metadata )filterline 1068 (+243 into the body)
Filters the array of meta data read from an image's exif data.
Uses · 8
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
- mbstring_binary_safe_encoding()Sets the mbstring internal encoding to a binary safe encoding when func_overload is enabled.
- reset_mbstring_encoding()Resets the mbstring internal encoding to a users previously set encoding.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_exif_frac2dec()Converts a fraction string to a decimal.
- wp_exif_date2ts()Converts the exif date format to a unix timestamp.
- seems_utf8()Checks to see if a string is utf8 encoded.
- wp_kses_post_deep()Navigates through an array, object, or scalar, and sanitizes content for allowed HTML tags for post content.
Used by · 4
- WP_REST_Attachments_Controller::insert_attachment()Inserts the attachment post in the database. Does not update the attachment meta.
- media_handle_sideload()Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
- media_handle_upload()Saves a file submitted from a POST request and create an attachment post for it.
- wp_create_image_subsizes()Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
Source code
function wp_read_image_metadata( $file ) { if ( ! file_exists( $file ) ) { return false; } list( , , $image_type ) = wp_getimagesize( $file ); /* * 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; } $meta['caption'] = $caption; } if ( ! empty( $iptc['2#110'][0] ) ) { // Credit. $meta['credit'] = trim( $iptc['2#110'][0] );Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.