Home  >  Article  >  Backend Development  >  PHP implements code to extract an image file and display it on the browser_PHP tutorial

PHP implements code to extract an image file and display it on the browser_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:46741browse

I did a project last year where I wanted to make a text list of image files uploaded by users. When the user clicks on a file name, the image can be displayed.

Because we have to consider compatibility with various image formats, I The GD library is used to determine the specific image file (MINE), and then the corresponding image generation function imagecreatefromXXX() is called to generate an img, and then the img is output to the browser in jpeg format. Although it is done, but I always feel dissatisfied.

Today I had the opportunity to reconsider this function. I found a few lines of code in the php manual. It is concise and clear, and can fully realize the functions I want, and does not require the GD library

Copy code The code is as follows:

$size = getimagesize($filename); //Get mime information
$fp=fopen($filename, "rb"); //Open the file in binary mode
if ($size && $fp) {
header("Content-type: {$size['mime ']}");
fpassthru($fp); // Output to browser
exit;
} else {
// error
}
?>


The amount of code is less than 1/10 of my original size, and the speed is N times faster.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326070.htmlTechArticleLast year I did a project to make a text list of image files uploaded by users. When the user clicks on a file name After that, you can display this image. Because you have to consider compatibility with various image formats...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn