問題描述
將 PHP 頁面作為圖像返回 (Return a PHP page as an image)
I am trying to read a image file (.jpeg to be exact), and 'echo' it back to the page output, but have is display an image...
my index.php has an image link like this:
<img src='test.php?image=1234.jpeg' />
and my php script does basically this:
1) read 1234.jpeg 2) echo file contents... 3) I have a feeling I need to return the output back with a mime‑type, but this is where I get lost
Once I figure this out, I will be removing the file name input all together and replace it with an image id.
If I am unclear, or you need more information, please reply.
‑‑‑‑‑
參考解法
方法 1:
The PHP Manual has this example:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content‑Type: image/png");
header("Content‑Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
The important points is that you must send a Content‑Type header. Also, you must be careful not include any extra white space (like newlines) in your file before or after the <?php ... ?>
tags.
As suggested in the comments, you can avoid the danger of extra white space at the end of your script by omitting the ?>
tag:
<?php
$name = './img/ok.png';
$fp = fopen($name, 'rb');
header("Content‑Type: image/png");
header("Content‑Length: " . filesize($name));
fpassthru($fp);
You still need to carefully avoid white space at the top of the script. One particularly tricky form of white space is a UTF‑8 BOM. To avoid that, make sure to save your script as "ANSI" (Notepad) or "ASCII" or "UTF‑8 without signature" (Emacs) or similar.
方法 2:
I feel like we can make this code a little bit easier by just getting the mime type from $image_info:
$file_out = "myDirectory/myImage.gif"; // The image to return
if (file_exists($file_out)) {
$image_info = getimagesize($file_out);
//Set the content‑type header as appropriate
header('Content‑Type: ' . $image_info['mime']);
//Set the content‑length header
header('Content‑Length: ' . filesize($file_out));
//Write the image bytes to the client
readfile($file_out);
}
else { // Image file not found
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
}
With this solution any type of image can be processed but it is just another option. Thanks ban‑geoengineering for your contribution.
方法 3:
This should work. It may be slower.
$img = imagecreatefromjpeg($filename);
header("Content‑Type: image/jpg");
imagejpeg($img);
imagedestroy($img);
方法 4:
I worked without Content‑Length . maybe reason work for remote image files
// open the file in a binary mode
$name = 'https://www.example.com/image_file.jpg';
$fp = fopen($name, 'rb');
// send the right headers
header('Cache‑Control: no‑cache, no‑store, max‑age=0, must‑revalidate');
header('Expires: January 01, 2013'); // Date in the past
header('Pragma: no‑cache');
header("Content‑Type: image/jpg");
/* header("Content‑Length: " . filesize($name)); */
// dump the picture and stop the script
fpassthru($fp);
exit;
方法 5:
Very, very easy.
<?php
//could be image/jpeg or image/gif or whatever
header('Content‑Type: image/png')
readfile('image.png')
?>
(by MichaelICE、Martin Geisler、ban‑geoengineering、Drew LeSueur、Berk、Blaze612 YT)