getimagesize

(PHP 4, PHP 5, PHP 7, PHP 8)

getimagesize取得图像大小

说明

getimagesize(string $filename, array &$image_info = null): array|false

getimagesize() 函数将确定任何支持的指定图像文件的大小,并返回尺寸以及文件类型和 height/width 文本字符串,以在标准 HTML IMG 标签和对应的 HTTP 内容类型中使用。

getimagesize() 还可以在 image_info 参数中返回更多信息。

警告

此函数要求 filename 是有效的图像文件。如果提供的是非图像,可能会错误的识别为图像且函数会成功返回,但数组可能包含无意义的值。

不要使用 getimagesize() 识别指定的文件是否是有效的图像。请使用专业解决方案,比如 Fileinfo 扩展。

注意: 注意 JPC 和 JP2 是具有不同位深度的组件。此外,JP2 文件可能包含有多个 JPEG 2000 代码流,此情况下,getimagesize() 返回此文件顶层中碰到的第一个代码流的值。

注意: 从具有最高比特率的 icon 中检索有关 icon 的信息。

注意: GIF 图像由一个或多个帧组成,其中每个帧可能只占据图像的一部分。getimagesize() 报告的图像大小是整体大小(从逻辑屏幕描述符中读取)。

参数

filename

此参数指定希望检索其信息的文件。可以指向本地文件或(配置允许)使用某个支持的流的远程文件。

image_info

可选参数允许从图像文件中提取一些扩展信息。目前会将不同的 JPG APP 标记作为关联数组返回。一些程序使用这些 APP 标记在图像中嵌入文本信息。 A very common one is to embed » IPTC information in the APP13 marker. You can use the iptcparse() function to parse the binary APP13 marker into something readable.

注意:

image_info 仅支持 JFIF 文件。

返回值

返回信息最多包含 7 个元素。并非所有图像类型都包含 channelsbits 元素。

索引 0 和 1 分别包含图像的宽度和高度。

注意:

一些格式可能没有图像或者包含多个图像。在这种情况下,getimagesize() 可能无法正确确定图像大小。getimagesize() 此时会返回的宽高为 0。

索引 2 是表示图像类型的某个 IMAGETYPE_XXX 常量

Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.

mime 是图像对应的 MIME 类型。此信息可用于传递具有正确 HTTP Content-type 报头的图像:

示例 #1 getimagesize() 和 MIME 类型

<?php
$size
= getimagesize($filename);
$fp = fopen($filename, "rb");
if (
$size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
} else {
// error
}
?>

channels 对 RGB 图片为 3,对 CMYK 图像为 4。

bits 是每种颜色的位数。

对应一些图像类型,channelsbits 值的存在可能会让人困惑。 As an example, GIF always uses 3 channels per pixel, but the number of bits per pixel cannot be calculated for an animated GIF with a global color table.

失败时返回 false

错误/异常

如果无法访问 filename 图像,getimagesize() 将生成 E_WARNING 级别的错误。读取错误时,getimagesize() 将生成 E_NOTICE 级别的错误。

更新日志

版本 说明
8.2.0 现在将正确返回 AVIF 图像的实际尺寸、bits 和 channels;以前,尺寸报告为 0x0,并且不会报告 bits 和 channels。
7.1.0 新增 WebP 支持。

范例

示例 #2 getimagesize() 示例

<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo
"<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>

示例 #3 getimagesize (URL)

<?php
$size
= getimagesize("http://www.example.com/gifs/logo.gif");

// if the file name has space in it, encode it properly
$size = getimagesize("http://www.example.com/gifs/lo%20go.gif");

?>

示例 #4 getimagesize() 返回 IPTC

<?php
$size
= getimagesize("testimg.jpg", $info);
if (isset(
$info["APP13"])) {
$iptc = iptcparse($info["APP13"]);
var_dump($iptc);
}
?>

注释

注意:

此函数不需要 GD 图象库。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top