PHP code for extracting an image file and displaying it on a browser

Source: Internet
Author: User
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. 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. I don't need the GD library to work on a project last year, to list the text of the image file uploaded by the user, you can click a file name to 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:
$ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.