函数:array getimagesize(string filename, array [imageinfo]);
返回值 : 数组
函数种类 : 图形处理
内容说明:
PHP函数getimagesize可用来取得 gif、jpeg 及 png 三种 www 上图片的高与宽,不需要安装 gd library 就可以使用本函数。返回的数组有四个元素。
返回数组的第一个元素 (索引值 0) 是图片的高度,单位是像素 (pixel)。
第二个元素 (索引值 1) 是图片的宽度。
第三个元素 (索引值 2) 是图片的文件格式,其值 1 为 gif 格式、 2 为 jpeg/jpg 格式、3 为 png 格式。
第四个元素 (索引值 3) 为图片的高与宽字符串,height=xxx width=yyy。
可省略的参数 imageinfo 用来取得一些图片的相关信息,例如 iptc (http://www.xe.net/iptc) 的 app13 标记,就可以加在图片中,可利用 iptcparse() 来解析。
下面的例子是smarty中用于图片上传后的图片类型验证和长宽判断:
function load($filename) {
$image_info = @getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = @imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = @imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = @imagecreatefrompng($filename);
}else{
return false;
}
if(function_exists("imagecopyresampled") && function_exists("imagecreatetruecolor") && $this->image_type != IMAGETYPE_GIF){
$this->true_color = true;
}
if($this->image){
return true;
}
return false;
}