wp_generate_attachment_metadata()获取一个附件(图片/文件)的meta信息

目录

描述

This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen.

wp_generate_attachment_metadata() 描述

用法

 <?php wp_generate_attachment_metadata$attachment_id$file ); ?> 

wp_generate_attachment_metadata() 用法

参数

$attachment_id

(int) (必填) Attachment Id to process.

默认值: None

$file

(string) (必填) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. See wp_upload_dir()

默认值: None

wp_generate_attachment_metadata() 参数

返回值

(array) 

attachment metadata in the format required by wp_update_attachment_metadata()

The elements returned in the array are:

["width"] 

(string) Horizontal size of image attachment, in pixels.

["height"] 

(string) Vertical size of image attachment, in pixels.

["file"] 

(string) Path to image attachment, relative to the currently configured uploads directory.

["hwstring_small"] 

(string) Height/width string for HTML img tag used to display the Small size of this image.
For example: height='96' width='126'

["sizes"]["thumbnail"]["file"] 

(string) File name of Thumbnail-sized copy of image attachment.

["sizes"]["thumbnail"]["width"] 

(string) Horizontal size of Thumbnail-sized copy of image attachment, in pixels.

["sizes"]["thumbnail"]["height"] 

(string) Vertical size of Thumbnail-sized copy of image attachment, in pixels.

["sizes"]["medium"] 

(array) Same three elements as ["sizes"]["thumbnail"] but for Medium-sized copy of image attachment.

["sizes"]["large"] 

(array) Same three elements as ["sizes"]["thumbnail"] but for Large-sized copy of image attachment.

["sizes"]["post-thumbnail"] 

(array) Same three elements as ["sizes"]["thumbnail"] but for Post Thumbnail-sized copy of image attachment.

["sizes"]["large-feature"] 

(array) Same three elements as ["sizes"]["thumbnail"] but for Large Feature-sized copy of image attachment.

["sizes"]["small-feature"] 

(array) Same three elements as ["sizes"]["thumbnail"] but for Small Feature-sized copy of image attachment.

["image_meta"] 

(array) Image attachment Metadata returned by wp_read_image_metadata()

wp_generate_attachment_metadata() 返回值

注意

This function can be used to regenerate thumbnail and intermediate sizes of the image after changes have been made on the Settings_Media_Screen but it does not check or delete intermediate sizes it previously created for the same image.

Thumbnail and intermediate sizes of the image, and ["sizes"] elements in the array returned by this function, are only generated when the intermediate size is smaller than the size of the image.

The function should be used in conjunction with wp_update_attachment_metadata().

If this function is undefined in the environment where it is to be used, such as within a Shortcode, use the include function:

wp_generate_attachment_metadata() 注意

源文件

wp_generate_attachment_metadata() 函数的代码位于
wp-admin/includes/image.php.

wp_generate_attachment_metadata() 源文件