当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/wp generate attachment metadata

提供: WordPress Codex 日本語版
< 関数リファレンス
2018年5月4日 (金) 13:05時点におけるGblsm (トーク | 投稿記録)による版 (en:Function Reference/wp_generate_attachment_metadata 03:12, 30 September 2017‎ Karzin 版を流し込み。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

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) (必須) Attachment Id to process.
初期値: なし
$file
string) (必須) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be in the uploads directory. See wp_upload_dir()
初期値: なし

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 
if ( ! function_exists( 'wp_crop_image' ) ) {
	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()

最新英語版: WordPress Codex » Function Reference/wp_generate_attachment_metadata最新版との差分