PHP判断图片格式的七种方法小结_PHP
Jun 01, 2016 pm 12:07 PM
Image Format
使用php判断文件图片的格式
复制代码 代码如下:
$imgurl = "http://www.bitsCN.com/images/logo.gif";
//方法1
echo $ext = strrchr($imgurl,'.');
echo '
';
//方法2
echo $ext1 = substr($imgurl,strrpos($imgurl, '.'));
echo '
';
//方法3
echo(@end(explode(".",$imgurl)));
echo '
';
//方法4
echo pathinfo($imgurl,PATHINFO_EXTENSION);
print_r(pathinfo($imgurl));
echo '
';
//方法5
$imginfo= getimagesize($imgurl);
print_r($imginfo);
echo end($imginfo);
echo '
';
//方法6
$imghttp = get_headers($imgurl,true);
print_r($imghttp);
echo '
';
//方法7
echo exif_imagetype($imgurl);
测试结果:

方法分析:
第一种思想方法就是把图片路径当作字符串来处理,那么判断图片格式的也就是变成了查找.号的字符串。
方法1:使用strrchr
strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。
如果成失败,否则返回 false。备注:该函数是二进制安全的。
方法2:strrpos
strrpos() 函数查找字符串在另一个字符串中最后一次出现的位置。
如果成功,则返回位置,否则返回 false。
注释:该函数对大小写敏感。如需进行大小写不敏感的查找,请使用 strripos()。
先计算.号的位置,在使用substr截取字符串
方法3:explode把字符串分割成数组,取数组最后一个键值
———————————————————————–
第二种思想,把图片路径当作文件路径,那么这里就需要gd类库函数或者php文件函数
方法4:使用pathinfo()
pathinfo(path,options)函数以数组的形式返回文件路径的信息
包括以下的数组元素:
[dirname]
[basename]
[extension]
options参数:
可选。规定要返回的数组元素。默认是 all。
可能的值:
PATHINFO_DIRNAME – 只返回 dirname
PATHINFO_BASENAME – 只返回 basename
PATHINFO_EXTENSION – 只返回 extension
方式5:getimagesize获取图片大小格式的方式,这个不是gd类库函数
这个方法很经典,不仅可以用来判断图片格式还可以获得图片长宽信息
方法6:get_headers获取http报头信息
如果图片路径是一个标准的url路径,那么可以使用get_header函数,这个函数可以返回http报
头,另外这个方法也可以判断图片文件url是否是一个有效的url,使用方法参考:《PHP判断远程url是否有效的几种方法》
方法7:使用gd类库函数
exif_imagetype() 读取一个图像的第一个字节并检查其签名。
如果发现了恰当的签名则返回一个对应的常量,否则返回 FALSE。返回值和 getimagesize() 返回的数组中的索引 2 的值是一样的,但本函数快得多。
返回的常量值:对应的值,如例子返回1,对应的是gif格式图片。

综合评价
目前比较主流使用的方法是方法1,如果考虑安全问题的话可以使用方法5,因为方法5可以判断这个文件是否为图像文件(可以避免有人把非法程序伪造成图片文件).
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?
3 weeks ago
By 王林
Two Point Museum: All Exhibits And Where To Find Them
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?
3 weeks ago
By 王林
Two Point Museum: All Exhibits And Where To Find Them
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
