返回介绍

wp_read_image_metadata()

发布于 2017-09-11 12:40:55 字数 11117 浏览 979 评论 0 收藏 0

wp_read_image_metadata( string $file )

Get 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.


参数

$file

(string) (Required)


返回值

(bool|array) False on failure. Image metadata array on success.


源代码

File: wp-admin/includes/image.php

function wp_read_image_metadata( $file ) {
	if ( ! file_exists( $file ) )
		return false;

	list( , , $源代码ImageType ) = 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();
	/*
	 * Read IPTC first, since it might contain data not available in exif such
	 * as caption, description etc.
	 */
	if ( is_callable( 'iptcparse' ) ) {
		getimagesize( $file, $info );

		if ( ! empty( $info['APP13'] ) ) {
			$iptc = iptcparse( $info['APP13'] );

			// 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] );
			elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
				$meta['credit'] = trim( $iptc['2#080'][0] );

			if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time
				$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );

			if ( ! empty( $iptc['2#116'][0] ) ) // copyright
				$meta['copyright'] = trim( $iptc['2#116'][0] );

			if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array
				$meta['keywords'] = array_values( $iptc['2#025'] );
			}
		 }
	}

	/**
	 * Filters the image types to check for exif data.
	 *
	 * @since 2.5.0
	 *
	 * @param array $image_types Image types to check for exif data.
	 */
	if ( is_callable( 'exif_read_data' ) && in_array( $源代码ImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) {
		$exif = @exif_read_data( $file );

		if ( ! empty( $exif['Imagedescription'] ) ) {
			mbstring_binary_safe_encoding();
			$description_length = strlen( $exif['Imagedescription'] );
			reset_mbstring_encoding();

			if ( empty( $meta['title'] ) && $description_length < 80 ) {
				// Assume the title is stored in Imagedescription
				$meta['title'] = trim( $exif['Imagedescription'] );
			}

			if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) {
				$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
			}

			if ( empty( $meta['caption'] ) ) {
				$meta['caption'] = trim( $exif['Imagedescription'] );
			}
		} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
			$meta['caption'] = trim( $exif['Comments'] );
		}

		if ( empty( $meta['credit'] ) ) {
			if ( ! empty( $exif['Artist'] ) ) {
				$meta['credit'] = trim( $exif['Artist'] );
			} elseif ( ! empty($exif['Author'] ) ) {
				$meta['credit'] = trim( $exif['Author'] );
			}
		}

		if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) {
			$meta['copyright'] = trim( $exif['Copyright'] );
		}
		if ( ! empty( $exif['FNumber'] ) ) {
			$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
		}
		if ( ! empty( $exif['Model'] ) ) {
			$meta['camera'] = trim( $exif['Model'] );
		}
		if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) {
			$meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] );
		}
		if ( ! empty( $exif['FocalLength'] ) ) {
			$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
		}
		if ( ! empty( $exif['ISOSpeedRatings'] ) ) {
			$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
			$meta['iso'] = trim( $meta['iso'] );
		}
		if ( ! empty( $exif['ExposureTime'] ) ) {
			$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
		}
		if ( ! empty( $exif['Orientation'] ) ) {
			$meta['orientation'] = $exif['Orientation'];
		}
	}

	foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
		if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) {
			$meta[ $key ] = utf8_encode( $meta[ $key ] );
		}
	}

	foreach ( $meta['keywords'] as $key => $keyword ) {
		if ( ! seems_utf8( $keyword ) ) {
			$meta['keywords'][ $key ] = utf8_encode( $keyword );
		}
	}

	$meta = wp_kses_post_deep( $meta );

	/**
	 * Filters the array of meta data read from an image's exif data.
	 *
	 * @since 2.5.0
	 * @since 4.4.0 The `$iptc` parameter was added.
	 *
	 * @param array  $meta            Image meta data.
	 * @param string $file            Path to image file.
	 * @param int    $源代码ImageType Type of image.
	 * @param array  $iptc            IPTC data.
	 */
	return apply_filters( 'wp_read_image_metadata', $meta, $file, $源代码ImageType, $iptc );

}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/kses.php: wp_kses_post_deep()
  • wp-admin/includes/image.php: wp_exif_frac2dec()
  • wp-admin/includes/image.php: wp_exif_date2ts()
  • wp-admin/includes/image.php: wp_read_image_metadata_types
  • wp-admin/includes/image.php: wp_read_image_metadata
  • wp-includes/formatting.php: seems_utf8()
  • wp-includes/functions.php: mbstring_binary_safe_encoding()
  • wp-includes/functions.php: reset_mbstring_encoding()
  • wp-includes/plugin.php: apply_filters()
  • Show 4 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/media.php: media_handle_upload()
  • wp-admin/includes/media.php: media_handle_sideload()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Additional note to Return

    The elements returned in the array are:

    ["aperture"]
    (string) Set to the EXIF FNumber field.

    ["credit"]
    (string) Set to the first non-empty value found by looking through the following fields:

    1. IPTC Credit field (2#110)
    2. IPTC Creator field (2#080)
    3. EXIF Artist field
    4. EXIF Author field

    ["camera"]
    (string) Set to the EXIF Model field.

    ["caption"]
    (string) Set to a non-empty value of one of the following fields (see 源代码 code for the precise logic involved):

    1. IPTC description field (2#120)
    2. EXIF UserComment field if [“title”] is unset AND EXIF:Imagedescription is less than 80 characters
    3. EXIF Imagedescription field if [“title”] is set OR EXIF:Imagedescription is more than 80 characters
    4. EXIF Comments field if [“title”] does not equal EXIF:Comments

    ["created_timestamp"]
    (string) Set to the first non-empty value found by looking through the following fields:

    1. EXIF field DateTimeDigitized
    2. IPTC Date and Time fields (2#055 and 2#060)

    ["copyright"]
    (string) Set to the first non-empty value found by looking through the following fields:

    1. IPTC Copyright field (2#116)
    2. EXIF Copyright field

    ["focal_length"]
    (string) Set to the EXIF FocalLength field.

    ["iso"]
    (string) Set to the EXIF ISOSpeedRatings field.

    ["shutter_speed"]
    (string) Set to the EXIF ExposureTime field.

    ["title"]
    (string) Set to the first non-empty value found by looking through the following fields:

    1. IPTC Headline field (2#105)
    2. IPTC Title field (2#005)
    3. IPTC description field (2#120) but only if less than 80 characters
    4. EXIF Title field
    5. EXIF Imagedescription field but only if less than 80 characters

    The (2#nnn) value shown after each IPTC field (above) is the key of the array returned by PHP’s iptcparse function for that particular IPTC field.

  2. When you receive error upon calling this function

    PHP Fatal error: Uncaught Error: Call to undefined function wp_read_image_metadata() ...

    Make sure to include the wp-admin/includes/image.php file

    require_once ABSPATH . '/wp-admin/includes/image.php';
    
    $path = '/path/to/file.jpg';
    
    wp_read_image_metadata( $path );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文