getimagesize
(PHP 4, PHP 5, PHP 7, PHP 8)
getimagesize — 取得影象大小
說明
$filename
, array &$imageinfo
= ?): array
getimagesize() 函式將測定任何
GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM
或 WBMP 影象檔案的大小並返回影象的尺寸以及檔案型別和一個可以用於普通
HTML 檔案中 IMG
標記中的
height/width 文字字串。
如果不能訪問 filename
指定的影象或者其不是有效的影象,getimagesize()
將返回 false
併產生一條 E_WARNING 級的錯誤。
注意:
對 JPC,JP2,JPX,JB2,XBM 和 WBMP 的支援自 PHP 4.3.2 起可用。對 SWC 的支援自 PHP 4.3.0 起可用。對 TIFF 的支援是 PHP 4.2.0 新增的。
注意: JPEG 2000 支援是 PHP 4.3.2 新增的。注意 JPC 和 JP2 可以有不同的色彩深度的成分。此情況下,「bits」的值是碰到的最高的位深度。此外,JP2 檔案可能包含有多個 JPEG 2000 程式碼流,此情況下,getimagesize() 返回此檔案頂層中碰到的第一個程式碼流的值。
注意: 本函式不需要 GD 影象庫。
返回一個具有四個單元的陣列。索引 0 包含影象寬度的畫素值,索引 1 包含影象高度的畫素值。索引 2 是影象型別的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。這些標記與 PHP 4.3.0 新加的 IMAGETYPE 常量對應。索引 3 是文字字串,內容為「height="yyy" width="xxx"」,可直接用於 IMG 標記。
示例 #1 getimagesize(檔案)
<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr>";
?>
URL 支援是 PHP 4.0.5 新增的。
示例 #2 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");
?>
對於 JPG 影象,還會多返回兩個索引:channels
和 bits
。channels
對於 RGB 影象其值為 3,對於 CMYK 影象其值為 4。bits
是每種顏色的位數。
自 PHP 4.3.0 起,bits
和
channels
對於其它影象型別也存在。但是這些值可能會把人搞糊塗。例如,GIF
總是對每個畫素使用 3 個 channel,但是對於動畫 GIF
來說每個畫素的位數無法通過全域性顏色表計算出來。
某些格式可能不包含影象或者包含多個影象。此種情況下,getimagesize() 可能不能用來準確測定影象的大小。此時 getimagesize() 將返回零作為寬度和高度。
自 PHP 4.3.0 起,getimagesize() 還會返回額外的參數
mime
,符合該影象的 MIME 型別。此資訊可以用來在
HTTP Content-type 頭資訊中發送正確的資訊:
示例 #3 getimagesize() 和 MIME 型別
<?php
$size = getimagesize($filename);
$fp=fopen($filename, "rb");
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
} else {
// error
}
?>
可選的 imageinfo
參數允許從影象檔案中提取一些擴充套件資訊。目前,這將以一個關聯陣列返回不同的
JPG APP 標識。某些程式用這些 APP
標識來在影象中嵌入文字資訊。一個非常常見的是 APP13 標識中嵌入的
IPTC » http://www.iptc.org/
資訊。可以用 iptcparse() 函式來將二進制的
APP13 標識解析為可讀的資訊。
示例 #4 getimagesize() 返回 IPTC
<?php
$size = getimagesize("testimg.jpg", &$info);
if (isset($info["APP13"])) {
$iptc = iptcparse($info["APP13"]);
var_dump($iptc);
}
?>
參見 image_type_to_mime_type(),exif_imagetype(),exif_read_data() 和 exif_thumbnail()。
參數
-
filename
-
This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams.
-
imageinfo
-
This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. 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.
返回值
Returns an array with 7 elements.
Index 0 and 1 contains respectively the width and the height of the image.
注意:
Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases.
Index 2 is one of the IMAGETYPE_XXX
constants indicating
the type of the image.
Index 3 is a text string with the correct
height="yyy" width="xxx"
string that can be used
directly in an IMG tag.
mime
is the correspondant MIME type of the image.
This information can be used to deliver images with the correct HTTP
Content-type
header:
示例 #5 getimagesize() and MIME types
<?php
$size = getimagesize($filename);
$fp = fopen($filename, "rb");
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
} else {
// error
}
?>
channels
will be 3 for RGB pictures and 4 for CMYK
pictures.
bits
is the number of bits for each color.
For some image types, the presence of channels
and
bits
values can be a bit
confusing. 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.
On failure, false
is returned.
錯誤/異常
If accessing the filename
image is impossible, or
if it isn't a valid picture, getimagesize() will
generate an error of level E_WARNING
. On read error,
getimagesize() will generate an error of level
E_NOTICE
.
更新日誌
版本 | 說明 |
---|---|
5.3.0 | Added icon support. |
5.2.3 |
Read errors generated by this function downgraded to
E_NOTICE from E_WARNING .
|
4.3.2 | Support for JPC, JP2, JPX, JB2, XBM, and WBMP became available. |
4.3.2 |
JPEG 2000 support was added for the imageinfo
parameter.
|
4.3.0 |
bits and channels are present
for other image types, too.
|
4.3.0 |
mime was added.
|
4.3.0 | Support for SWC and IFF was added. |
4.2.0 | Support for TIFF was added. |
4.0.6 | Support for BMP and PSD was added. |
4.0.5 | URL support was added. |
範例
示例 #6 getimagesize() example
<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>
示例 #7 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");
?>
示例 #8 getimagesize() returning IPTC
<?php
$size = getimagesize("testimg.jpg", $info);
if (isset($info["APP13"])) {
$iptc = iptcparse($info["APP13"]);
var_dump($iptc);
}
?>
註釋
注意:
此函式不需要 GD 圖像庫。
參見
- image_type_to_mime_type() - 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的影象型別的 MIME 型別
- exif_imagetype() - 判斷一個影象的型別
- exif_read_data() - 從一個圖片檔案中讀取 EXIF 頭資訊
- exif_thumbnail() - 取得嵌入在 TIFF 或 JPEG 影象中的縮圖