A project was created last year to list the text list of the image files uploaded by the user. After clicking a file name, the user can display the image.
Considering compatibility with various image formats, I used the GD library to determine which image file is the image file (MINE) and then call the corresponding image generation function imagecreatefromXXX (), generate an img, and then output the img to the browser in jpeg format. Although this is made, I always feel unsatisfied.
Today, I have the opportunity to reconsider this function. I found several lines of code in the php manual, which is concise and clear, and can fully implement the functions I want without the GD library.
Copy codeThe Code is as follows: <? Php
$ Size = getimagesize ($ filename); // obtain mime Information
$ Fp = fopen ($ filename, "rb"); // open a file in binary mode
If ($ size & $ fp ){
Header ("Content-type: {$ size ['mime ']}");
Fpassthru ($ fp); // output to the browser
Exit;
} Else {
// Error
}
?>
The Code volume is less than 1/10 of my original code, which is N times faster.