wordpress获取媒体附件图像元数据的函数wp_get_attachment_metadata()

WordPress教程 995

在wordpress中,使用wp_get_attachment_metadata()函数可以获取指定附件ID的元数据,如附件的宽度、高度、上传的文件路径,以及照片图像的光圈、IOS、快门等元数据,对于要做图片、摄影、图片素材类型的wordpress网站非常有用的一个wordpress函数。

函数代码

1
wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false )
参数
  • $attachment_id – (必须)附件ID,默认为全局$post
  • $unfiltered – (可选)是否运行筛选器,默认为false
返回值

返回值为包含附件元数据的数组,失败则返回false

  • ‘width’ – 附件的宽高
  • ‘height’ – 附件的高度
  • ‘file’ – 附件的相对路径
  • ‘sizes’ – 后台媒体设置中的缩略图大小、中等尺寸、大尺寸,键值是它们的别名,每一组包括文件名、宽度、高度以及mime类型。
  • ‘image_meta’ – 图像的元数据,如光圈、快门、ios等信息,电脑本地查看图像属性的详情信息里的东西

示例

1
2
3
4
<?php 
	$photo = wp_get_attachment_metadata(get_the_ID()); 
	print_r($photo);
?>
返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Array ( 
	[width] => 800 
	[height] => 600 
	[file] => 2017/09/f403241d7cc88ded9f17.jpg 
	[sizes] => Array ( 
		[medium] => Array ( 
			[file] => f403241d7cc88ded9f17-300x225.jpg 
			[width] => 300 
			[height] => 225 
			[mime-type] => image/jpeg 
		) 
		[thumbnail] => Array ( 
			[file] => f403241d7cc88ded9f17-150x150.jpg 
			[width] => 150 
			[height] => 150 
			[mime-type] => image/jpeg 
		) 
		[medium_large] => Array ( 
			[file] => f403241d7cc88ded9f17-768x576.jpg 
			[width] => 768 
			[height] => 576 
			[mime-type] => image/jpeg 
		) 
 
	) 
	[image_meta] => Array ( 
		[aperture] => 0 
		[credit] => 
		[camera] => 
		[caption] => 
		[created_timestamp] => 0 
		[copyright] => 
		[focal_length] => 0 
		[iso] => 0 
		[shutter_speed] => 0 
		[title] => 
		[orientation] => 0 
		[keywords] => Array ( ) 
	) 
)

函数位置:wp-includes/post.php

wordpress官方文档:

https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/

精品推荐: