让 PHP 页面输出静态图像

发布于 2024-09-25 02:29:12 字数 167 浏览 4 评论 0原文

我希望 PHP 能够发送 3 个图像中的 1 个,具体取决于 $_GET[] 参数。我现在有三个独立的 PNG 图像,并且希望 PHP 脚本将这些图像嵌入其中,然后返回指定的图像。所以,我想要一个 PHP 脚本而不是 3 个图像。这可能吗?我不需要即时创建特殊图像,只需打印其中一张即可。谢谢!

I want a PHP to be able to send 1 of 3 images, depending on a $_GET[] parameter. I have the images as three separate PNGs right now, and would like the PHP script to have those embedded in it, then return the specified image. So, I want one PHP script instead of 3 images. Is this possible? I don't need to create special images on the fly, just print out one of those. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伏妖词 2024-10-02 02:29:12

如果您的图像位于文件中,请使用 PHP 的 readfile() 函数,并发送输出之前的内容类型标头:

<?php
$imagePaths = array(
    '1' => 'file1.png',
    '2' => 'file2.png',
    '3' => 'file3.png',
);

$type = $_GET['img'];

if ( isset($imagePaths[$type]) ) {
    $imagePath = $imagePaths[$type];
    header('Content-Type: image/png');
    readfile($imagePath);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

编辑:
您还可以通过将图像编码为 Base64 来将图像嵌入脚本中,然后将它们嵌入为字符串在 PHP 中,然后使用 base64_decode 对其进行解码以传递它们:

<?php
$imageData = array(
    '1' => '...', // Base64-encoded data as string
    ...
);

$type = $_GET['img'];

if ( isset($imageData[$type]) ) {
    header('Content-Type: image/png');
    echo base64_decode($imageData[$type]);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

您可以还可以使用 PHP 在命令行上对图像进行编码。只需在命令行中执行此 PHP 脚本 (php script.php image1.png image2.png image3.png > output.php) 并保存其输出,并将其合并到您的脚本中:

<?php
$imageData = array();

foreach ($argv as $index => $imagePath)
    $imageData[(string)($index + 1)] = base64_encode(file_get_contents($imagePath));

echo '$imageData = '.var_export($imageData, true).';';
?>

If your images are in files, use PHP's readfile() function, and send a content-type header before outputting it:

<?php
$imagePaths = array(
    '1' => 'file1.png',
    '2' => 'file2.png',
    '3' => 'file3.png',
);

$type = $_GET['img'];

if ( isset($imagePaths[$type]) ) {
    $imagePath = $imagePaths[$type];
    header('Content-Type: image/png');
    readfile($imagePath);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

EDIT:
You could also embed your images in the script by encoding them e.g. as Base64, then embed them as strings in PHP, then decode it there with base64_decode to deliver them:

<?php
$imageData = array(
    '1' => '...', // Base64-encoded data as string
    ...
);

$type = $_GET['img'];

if ( isset($imageData[$type]) ) {
    header('Content-Type: image/png');
    echo base64_decode($imageData[$type]);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

You could also use PHP to encode the image on the command line. Just execute this PHP script in the command line (php script.php image1.png image2.png image3.png > output.php) and save its output, and incorporate it into your script:

<?php
$imageData = array();

foreach ($argv as $index => $imagePath)
    $imageData[(string)($index + 1)] = base64_encode(file_get_contents($imagePath));

echo '$imageData = '.var_export($imageData, true).';';
?>
萌能量女王 2024-10-02 02:29:12

我没有测试以下内容,但它应该有效。使用此脚本获取将包含图像数据的 PHP 代码。

要获取图像:

$image = base64_encode(file_get_contents("image.png"));
// The string $image now contains your image data.

在代码中为每个图像获取这个(可能很大)字符串。打印并复制然后粘贴。通过网络将其导入为文本文件。这取决于你。

然后,要打印图像(仅打印图像,就好像 PHP 脚本是图像一样),请执行以下操作:

header("content-type: image/png");
print base64_decode($image);

当然,您可以将每个图像数据放入数组或类似的内容中。

让我知道它是否有效。

I have not tested the following, but it should work. Use this script to get PHP code that will contain the image data.

To get the image:

$image = base64_encode(file_get_contents("image.png"));
// The string $image now contains your image data.

Get this (potentially big) string in your code where you want the image, for each image. Print it and copy it then paste it. Import it as a text file over the web. That's up to you.

Then, to print the image (only the image as if the PHP script were the image), do:

header("content-type: image/png");
print base64_decode($image);

Of course, you would put each image data in an array or something like that.

Let me know if it works.

揪着可爱 2024-10-02 02:29:12

是的,这是可能的。

执行此操作:

  1. 编写一个 php 脚本来决定输出哪个图像。
  2. 使用 header() 函数设置适当的标头(即内容类型)
  3. 打开文件,读取它并将其发送到输出流。

请记住,使用这种方法您可能会失去浏览器缓存带来的任何好处......

Yes, it's possible.

Do this:

  1. Write a php script deciding which image to output.
  2. Set the appropriate headers with header() function (I.e. Content-type)
  3. Open the file, read it and send it to the output stream.

Remember that you will probably lose any benefits from browser's cache with this approach...

泪意 2024-10-02 02:29:12

一种快速但不真正安全也不优雅的方法...

<?php
switch($_GET['type']){
 case "1":
    $image = "image1.png";
    break;
 case "2":
    $image = "image2.png";
    break;
 case "3":
    $image = "image3.png";
    break;
}
header('Content-Type: image/png');
readfile($image);
?>

故事的寓意:使用 header() 和 readfile() =D

A fast but not really secure nor elegant way to do it...

<?php
switch($_GET['type']){
 case "1":
    $image = "image1.png";
    break;
 case "2":
    $image = "image2.png";
    break;
 case "3":
    $image = "image3.png";
    break;
}
header('Content-Type: image/png');
readfile($image);
?>

the moral of the story: use header() and readfile() =D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文