將 PHP 頁面作為圖像返回 (Return a PHP page as an image)


問題描述

將 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 MichaelICEMartin Geislerban‑geoengineeringDrew LeSueurBerkBlaze612 YT)

參考文件

  1. Return a PHP page as an image (CC BY‑SA 3.0/4.0)

#image-processing #mime-types #PHP #http-headers






相關問題

在 matlab 中用 imread 讀取圖像文件會給出什麼樣的表示? (reading a image file with imread in matlab gives what kind of representation?)

使用 CRF 的圖像標記性能 (Image labeling performance using CRF)

Opencv:獲取圖像中的段大小並刪除小段 (Opencv: Get segments sizes in image and remove small segments)

將 PHP 頁面作為圖像返回 (Return a PHP page as an image)

我在哪裡可以找到有關雙三次插值和 Lanczos 重採樣的好讀物? (Where can I find a good read about bicubic interpolation and Lanczos resampling?)

從圖像中刪除白色背景 (Remove white backgrounds from images)

如何填充投影圖像的空白部分? (How to fill empty parts of a projected image?)

如何使圖像亮度均勻(使用 Python/PIL) (How to Make an Image Uniform Brightness (using Python/PIL))

圖像處理公式可生成類似通過 Mac 相機拍攝的照片的效果 (Image manipulation formula to generate effects like pictures taken via Mac's camera)

從照片生成漂亮的直方圖? (generating nice looking histogram from photo?)

使用 DjVu 工具進行背景/前景分離? (Using the DjVu tools to for background / foreground seperation?)

如何操縱跟踪器區域使其變成方形? (How can I manipulate the tracker area to make it into a square shape?)







留言討論