Heim  >  Artikel  >  Backend-Entwicklung  >  php判断图片格式的简单方法

php判断图片格式的简单方法

WBOY
WBOYOriginal
2016-07-25 08:58:051476Durchsuche
本文介绍下,在php编程中用于判断图片格式的方法,这也是最基本的一种方法,通过判断文件的扩展名来实现的。有需要的朋友参考下吧。

php判断图片格式,下面是最简单的方法:

<?php
/**
* 判断图片的格式
* edit by bbs.it-home.org
*/
$file= "test.jpg";
$filetype= strtolower(strrchr($file,"."));
$arrtype = array(".jpeg",".bmp",".gif",".jpg");
if(!in_array($filetype,$arrtype))
{
echo '不符合格式';
exit;
}
?>

另外,在php中判断图片格式,多是在上传图片的过程中。

此时,可以用:$_FILES['userfile']['type']在文件上传之前判断。

另外,php中还有一个getimagesize函数,用于取得图像大小。 getimagesize 返回和参数如下。 返回一个具有四个单元的数组。 索引 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 标记。

就介绍这些吧,希望对大家有所帮助。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn