getimagesize

(PHP 4, PHP 5, PHP 7)

getimagesize取锟斤拷图锟斤拷锟叫�

说锟斤拷

getimagesize ( string $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 锟斤拷锟侥达拷锟斤拷

Note:

锟斤拷 JPC锟斤拷JP2锟斤拷JPX锟斤拷JB2锟斤拷XBM 锟斤拷 WBMP 锟斤拷支锟斤拷锟斤拷 PHP 4.3.2 锟斤拷锟斤拷谩锟斤拷锟� SWC 锟斤拷支锟斤拷锟斤拷 PHP 4.3.0 锟斤拷锟斤拷谩锟斤拷锟� TIFF 锟斤拷支锟斤拷锟斤拷 PHP 4.2.0 锟斤拷拥摹锟�

Note: JPEG 2000 支锟斤拷锟斤拷 PHP 4.3.2 锟斤拷拥摹锟阶拷锟� JPC 锟斤拷 JP2 锟斤拷锟斤拷锟叫诧拷同锟斤拷色锟斤拷锟斤拷鹊某煞帧锟斤拷锟斤拷锟斤拷锟铰o拷"bits"锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷叩锟轿伙拷锟饺★拷锟斤拷锟解,JP2 锟侥硷拷锟斤拷锟杰帮拷锟斤拷锟叫讹拷锟� JPEG 2000 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷拢锟�getimagesize() 锟斤拷锟截达拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷

Note: 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要 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 锟斤拷恰锟�

Example #1 getimagesize锟斤拷锟侥硷拷锟斤拷

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

URL 支锟斤拷锟斤拷 PHP 4.0.5 锟斤拷拥摹锟�

Example #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 头锟斤拷息锟叫凤拷锟斤拷锟斤拷确锟斤拷锟斤拷息锟斤拷

Example #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 锟斤拷识锟斤拷锟斤拷为锟缴讹拷锟斤拷锟斤拷息锟斤拷

Example #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.

Note:

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:

Example #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.

锟斤拷锟斤拷

Example #6 getimagesize() example

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

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");

?>

Example #8 getimagesize() returning IPTC

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

注锟斤拷

Note:

锟剿猴拷锟斤拷锟斤拷锟斤拷要 GD 图锟斤拷狻�

锟轿硷拷

  • image_type_to_mime_type() - 取锟斤拷 getimagesize锟斤拷exif_read_data锟斤拷exif_thumbnail锟斤拷exif_imagetype 锟斤拷锟斤拷锟截碉拷图锟斤拷锟斤拷锟酵碉拷 MIME 锟斤拷锟斤拷
  • exif_imagetype() - 锟叫讹拷一锟斤拷图锟斤拷锟斤拷锟斤拷锟�
  • exif_read_data() - 锟斤拷 JPEG 锟斤拷 TIFF 锟侥硷拷锟叫讹拷取 EXIF 头锟斤拷息
  • exif_thumbnail() - 取锟斤拷嵌锟斤拷锟斤拷 TIFF 锟斤拷 JPEG 图锟斤拷锟叫碉拷锟斤拷锟斤拷图