Use PHP to determine the format of a file picture
Copy CodeThe code is as follows:
$imgurl = "Http://www.jb51.net/images/logo.gif";
Method 1
echo $ext = STRRCHR ($imgurl, '. ');
Echo ';
Method 2
echo $ext 1 = substr ($imgurl, Strrpos ($imgurl, '. '));
Echo ';
Method 3
Echo (@end (Explode (".", $imgurl)));
Echo ';
Method 4
Echo PathInfo ($imgurl, pathinfo_extension);
Print_r (PathInfo ($imgurl));
Echo ';
Method 5
$imginfo = getimagesize ($imgurl);
Print_r ($imginfo);
Echo End ($imginfo);
Echo ';
Method 6
$imghttp = Get_headers ($imgurl, true);
Print_r ($imghttp);
Echo ';
Method 7
echo Exif_imagetype ($imgurl);
Test results:
Method Analysis:
The first way of thinking is to treat the image path as a string, then the image format is changed to look for. Number of strings.
Method 1: Use STRRCHR
The STRRCHR () function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string.
If it fails, it returns false. Note: This function is binary safe.
Method 2:strrpos
The Strrpos () function finds the position of the last occurrence of a string in another string.
If successful, returns the position, otherwise false.
Note: The function is case-sensitive. For case-insensitive lookups, use Strripos ().
The position of the. Number is first calculated by using substr to intercept the string
method 3:explode to divide the string into an array, taking the last key value
———————————————————————–
The second idea, the image path as a file path, then this requires the GD class library function or PHP file functions
Method 4: Use PathInfo ()
The PathInfo (path,options) function returns the file path information as an array
Include the following array elements:
[DirName]
[BaseName]
[Extension]
Options parameters:
Optional. Specifies the array element to return. The default is all.
Possible values:
pathinfo_dirname– only returns DIRNAME
pathinfo_basename– only returns BASENAME
pathinfo_extension– only returns EXTENSION
Way 5:getimagesize Get the picture size format, this is not GD class library function
This method is very classic, not only can be used to determine the image format can also get picture length and width information
method 6:get_headers Get HTTP header information
If the picture path is a standard URL path, you can use the Get_header function, which can return an HTTP report
Header, this method can also determine whether the image file URL is a valid URL, using the method reference: "PHP to determine whether the remote URL is valid several methods"
Method 7: Use the GD class library function
Exif_imagetype () reads the first byte of an image and checks its signature.
Returns a corresponding constant if the appropriate signature is found, otherwise FALSE. The return value and the value of index 2 in the array returned by getimagesize () are the same, but this function is much faster.
Return constant value: the corresponding value, as the example returns 1, corresponding to the GIF format picture.
Comprehensive Evaluation
At present, the main use of the method is method 1, if you consider security issues can be used in method 5, because method 5 can determine whether the file is an image file (you can avoid the illegal process of forgery into a picture file).
http://www.bkjia.com/PHPjc/327399.html www.bkjia.com true http://www.bkjia.com/PHPjc/327399.html techarticle use PHP to determine the format of the file image copy code as follows:? php $imgurl = "http://www.jb51.net/images/logo.gif";//Method 1 echo $ext = STRRCHR ($imgurl, '. ') ); echo ' HR '; Method ...