getimagesize

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

getimagesize取得圖像大小

說(shuō)明

getimagesize(string $filename, array &$imageinfo = ?): array

getimagesize() 函數(shù)將測(cè)定任何 GIF,JPG,PNG,SWFSWC,PSD,TIFF,BMPIFF,JP2JPX,JB2,JPC,XBMWBMP 圖像文件的大小并返回圖像的尺寸以及文件類型和一個(gè)可以用于普通 HTML 文件中 IMG 標(biāo)記中的 height/width 文本字符串。

如果不能訪問(wèn) filename 指定的圖像或者其不是有效的圖像,getimagesize() 將返回 false 并產(chǎn)生一條 E_WARNING 級(jí)的錯(cuò)誤。

注意:

對(duì) JPC,JP2,JPX,JB2,XBMWBMP 的支持自 PHP 4.3.2 起可用。對(duì) SWC 的支持自 PHP 4.3.0 起可用。對(duì) TIFF 的支持是 PHP 4.2.0 添加的。

注意: JPEG 2000 支持是 PHP 4.3.2 添加的。注意 JPC 和 JP2 可以有不同的色彩深度的成分。此情況下,“bits”的值是碰到的最高的位深度。此外,JP2 文件可能包含有多個(gè) JPEG 2000 代碼流,此情況下,getimagesize() 返回此文件頂層中碰到的第一個(gè)代碼流的值。

注意: 本函數(shù)不需要 GD 圖像庫(kù)。

返回一個(gè)具有四個(gè)單元的數(shù)組。索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標(biāo)記: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。這些標(biāo)記與 PHP 4.3.0 新加的 IMAGETYPE 常量對(duì)應(yīng)。索引 3 是文本字符串,內(nèi)容為“height="yyy" width="xxx"”,可直接用于 IMG 標(biāo)記。

示例 #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");

?>

對(duì)于 JPG 圖像,還會(huì)多返回兩個(gè)索引:channelsbits。channels 對(duì)于 RGB 圖像其值為 3,對(duì)于 CMYK 圖像其值為 4。bits 是每種顏色的位數(shù)。

自 PHP 4.3.0 起,bitschannels 對(duì)于其它圖像類型也存在。但是這些值可能會(huì)把人搞糊涂。例如,GIF 總是對(duì)每個(gè)像素使用 3 個(gè) channel,但是對(duì)于動(dòng)畫 GIF 來(lái)說(shuō)每個(gè)像素的位數(shù)無(wú)法通過(guò)全局顏色表計(jì)算出來(lái)。

某些格式可能不包含圖像或者包含多個(gè)圖像。此種情況下,getimagesize() 可能不能用來(lái)準(zhǔn)確測(cè)定圖像的大小。此時(shí) getimagesize() 將返回零作為寬度和高度。

自 PHP 4.3.0 起,getimagesize() 還會(huì)返回額外的參數(shù) mime,符合該圖像的 MIME 類型。此信息可以用來(lái)在 HTTP Content-type 頭信息中發(fā)送正確的信息:

示例 #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 參數(shù)允許從圖像文件中提取一些擴(kuò)展信息。目前,這將以一個(gè)關(guān)聯(lián)數(shù)組返回不同的 JPG APP 標(biāo)識(shí)。某些程序用這些 APP 標(biāo)識(shí)來(lái)在圖像中嵌入文本信息。一個(gè)非常常見的是 APP13 標(biāo)識(shí)中嵌入的 IPTC ? http://www.iptc.org/ 信息。可以用 iptcparse() 函數(shù)來(lái)將二進(jìn)制的 APP13 標(biāo)識(shí)解析為可讀的信息。

示例 #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()。

參數(shù)

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.

錯(cuò)誤/異常

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.

更新日志

版本 說(shuō)明
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);
}
?>

注釋

注意:

此函數(shù)不需要 GD 圖象庫(kù)。

參見