wp_update_image_subsizes

函数
wp_update_image_subsizes ( $attachment_id )
参数
  • (int) $attachment_id The image attachment post ID.
    Required:
返回值
  • (array|WP_Error) The updated image meta data array or WP_Error object if both the image meta and the attached file are missing.
定义位置
相关方法
wp_create_image_subsizeswp_calculate_image_sizeswp_ajax_media_create_image_subsizes_wp_make_subsizeswp_get_missing_image_subsizes
引入
5.3.0
弃用
-

wp_update_image_subsizes是一个函数,用于更新WordPress中一个特定图片的子尺寸: 该函数检索图片的子尺寸,然后更新WordPress数据库中的子尺寸信息。这使得WordPress能够跟踪图像的不同尺寸,这对显示目的很有用。

如果当前注册的图像子尺寸中缺少任何一个,请创建它们并更新图像元数据。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_update_image_subsizes( $attachment_id ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
$image_file = wp_get_original_image_path( $attachment_id );
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
// Previously failed upload?
// If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
if ( ! empty( $image_file ) ) {
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
} else {
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
}
} else {
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
if ( empty( $missing_sizes ) ) {
return $image_meta;
}
// This also updates the image meta.
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
}
/** This filter is documented in wp-admin/includes/image.php */
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
// Save the updated metadata.
wp_update_attachment_metadata( $attachment_id, $image_meta );
return $image_meta;
}
function wp_update_image_subsizes( $attachment_id ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); $image_file = wp_get_original_image_path( $attachment_id ); if ( empty( $image_meta ) || ! is_array( $image_meta ) ) { // Previously failed upload? // If there is an uploaded file, make all sub-sizes and generate all of the attachment meta. if ( ! empty( $image_file ) ) { $image_meta = wp_create_image_subsizes( $image_file, $attachment_id ); } else { return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) ); } } else { $missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); if ( empty( $missing_sizes ) ) { return $image_meta; } // This also updates the image meta. $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id ); } /** This filter is documented in wp-admin/includes/image.php */ $image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' ); // Save the updated metadata. wp_update_attachment_metadata( $attachment_id, $image_meta ); return $image_meta; }
function wp_update_image_subsizes( $attachment_id ) {
	$image_meta = wp_get_attachment_metadata( $attachment_id );
	$image_file = wp_get_original_image_path( $attachment_id );

	if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
		// Previously failed upload?
		// If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
		if ( ! empty( $image_file ) ) {
			$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
		} else {
			return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
		}
	} else {
		$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );

		if ( empty( $missing_sizes ) ) {
			return $image_meta;
		}

		// This also updates the image meta.
		$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
	}

	/** This filter is documented in wp-admin/includes/image.php */
	$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );

	// Save the updated metadata.
	wp_update_attachment_metadata( $attachment_id, $image_meta );

	return $image_meta;
}

常见问题

FAQs
查看更多 >