(PHP 4, PHP 5, PHP 7)
getimagesize — ȡ��ͼ���С
$filename
[, array &$imageinfo
] ) : array
getimagesize() �������ⶨ�κ�
GIF��JPG��PNG��SWF��SWC��PSD��TIFF��BMP��IFF��JP2��JPX��JB2��JPC��XBM
�� WBMP ͼ���ļ��Ĵ�С������ͼ��ijߴ��Լ��ļ����ͺ�һ������������ͨ
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 �����в�ͬ��ɫ����ȵijɷ֡�������£�"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 ��˵ÿ�����ص�λ����ͨ��ȫ����ɫ����������
ijЩ��ʽ���ܲ�����ͼ����߰������ͼ��������£�getimagesize() ���ܲ�������ȷ�ⶨͼ��Ĵ�С����ʱ getimagesize() ����������Ϊ��Ⱥ߶ȡ�
�� PHP 4.3.0 ��getimagesize() ���᷵�ض���IJ��� 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 ��ʶ��ijЩ��������Щ APP
��ʶ����ͼ����Ƕ���ı���Ϣ��һ���dz��������� 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 ͼ��⡣