A summary of seven methods for PHP to determine the image format. The code for determining the format of a file image using php is as follows :? Php?imgurlwww.jb51.netimageslogo.gif; Method 1 echo $ extstrrchr ($ imgurl,.); echohr; method php is used to determine the file Image format
The code is as follows:
$ 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 ).
The pipeline code is as follows :? Php $ imgurl = "http://www.jb51.net/images/logo.gif"; // method 1 echo $ ext = strrchr ($ imgurl, '.'); echo 'HR'; // method...