Use php to determine the file image format
Copy codeThe Code is as follows:
<? Php
$ Imgurl = "http://www.jb51.net/images/logo.gif ";
// Method 1
Echo $ ext = strrchr ($ imgurl ,'.');
Echo '// Method 2
Echo $ ext1 = 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 method is to treat the image path as a string, so the format of the image is converted into a string for searching for a. Number.
Method 1: Use strrchr
The strrchr () function finds the position of the last occurrence of a string in another string, and returns all characters from this position to the end of the string.
Otherwise, false is returned. Note: This function is binary secure.
Method 2: strrpos
The strrpos () function is used to locate the last occurrence of a string in another string.
If the operation is successful, the location is returned. Otherwise, false is returned.
Note: This function is case sensitive. Use strripos () to perform case-insensitive searches ().
Calculate the position of the. number first, and use substr to intercept the string
Method 3: explode splits the string into an array and obtains the last key value of the array.
------------------------
The second idea is to regard the image path as a file path. Here, the gd class library function or PHP file function is required.
Method 4: Use pathinfo ()
The pathinfo (path, options) function returns the file path information in an array.
Includes the following array elements:
[Dirname]
[Basename]
[Extension]
Options parameters:
Optional. Specifies the array element to be returned. The default value is all.
Possible values:
PATHINFO_DIRNAME-returns only dirname
PATHINFO_BASENAME-only return basename
PATHINFO_EXTENSION-returns only extension
Method 5: getimagesize is used to obtain the image size format. This is not a gd class library function.
This method is classic and can be used to determine the image format and obtain the image length and width information.
Method 6: get_headers
If the image path is a standard url path, you can use the get_header function. This function can return an http message.
In addition, this method can also determine whether the Image File url is a valid url. For more information, see: several methods for PHP to determine whether remote URLs are valid.
Method 7: Use the gd Class Library Function
Exif_imagetype () reads the first byte of an image and checks its signature.
If an appropriate signature is found, a corresponding constant is returned; otherwise, FALSE is returned. The returned value is the same as the value of index 2 in the array returned by getimagesize (), but this function is much faster.
Return constant value: the corresponding value. For example, 1 is returned, which corresponds to an image in gif format.
Comprehensive Evaluation
Currently, the most popular method is method 1. If you consider security issues, you can use Method 5, because Method 5 can determine whether the file is an image file (this can prevent someone from forging an illegal program into an image file ).