WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/wp generate attachment metadata

Contents

Description

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.

Usage

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

Parameters

$attachment_id
(int) (required) Attachment Id to process.
Default: None
$file
(string) (required) 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()
Default: None

Return Values

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

Example

To generate attachment metadata for attachment with parent post ID 37:

<?php
  $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
?>

Notes

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:

<?php include( ABSPATH . 'wp-admin/includes/image.php' ); ?>

Source File

wp_generate_attachment_metadata() is located in wp-admin/includes/image.php.

Related

wp_update_attachment_metadata(), wp_get_attachment_metadata(), wp_read_image_metadata(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), wp_attachment_is_image(), wp_insert_attachment(), wp_delete_attachment(), wp_get_attachment_link(), wp_get_attachment_url(), is_attachment(), wp_insert_post()