php 将图像存储在变量中,稍后回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12396693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:28:58  来源:igfitidea点击:

Store image in variable, echo later

phpvariablesechostore

提问by apparatix

Let's say I have a user enter the URL of an image.

假设我让用户输入图像的 URL。

After URL validation, etc. I want to get the image and store it in a PHP variable. Not the image path, but the actual image itself.

在 URL 验证等之后,我想获取图像并将其存储在 PHP 变量中。不是图像路径,而是实际图像本身。

I am adding this image-holding variable in between other strings, so I cannot change use header()before echoing the image. Is it possible to use <img>HTML tags? I really don't want to copy the images to my own server...

我在其他字符串之间添加了这个图像保持变量,所以header()在回显图像之前我不能改变使用。是否可以使用<img>HTML 标签?我真的不想将图像复制到我自己的服务器...

How do I:

我如何能:

  1. Store the image in a variable such that,
  2. Echo the image from the variable without changing headers.
  1. 将图像存储在一个变量中,使得,
  2. 从变量中回显图像而不更改标题。

Edit:

编辑:

I said above that I am putting this image inside another variable, e.g.:

我在上面说过我将此图像放入另一个变量中,例如:

$str = "blah blah" . $var_holding_img . "more text";

Is it possible to insert something in the string above that will be replaced with the images? Can parse the variable $strlater to replace some random text like "abg30j-as" with the image...

是否可以在上面的字符串中插入一些将被图像替换的内容?可以$str稍后解析变量以用图像替换一些随机文本,如“abg30j-as”...

回答by apparatix

I found an answer to my own question:

我找到了我自己问题的答案:

First, I created another PHP file, called img.php:

首先,我创建了另一个 PHP 文件,名为img.php

<?php
$url = $_GET['imgurl'];
/*
Conduct image verification, etc.
*/
$img_ext = get_ext($url); //Create function "get_ext()" that gets file extension
header('Content-type: image/' . $img_ext);
echo file_get_contents($url);
?>

Then, in the original PHP file, I used this PHP code:

然后,在原来的 PHP 文件中,我使用了这个 PHP 代码:

<?php
$var_holding_img = '<img src="img.php?imgurl=http://example.com/image.png"/>';
$string = "This is an image:<br \>" . $var_holding_img . "<br \>displayed dynamically with PHP.";
echo $string;
?>

This way, the PHP file "img.php" can use the proper headers and the image can be inserted as HTML into any other PHP variable.

这样,PHP 文件“img.php”可以使用正确的标题,图像可以作为 HTML 插入到任何其他 PHP 变量中。

回答by LSerni

How do I:

Store the image in a variable such that, Echo the image from the variable without changing headers.

我如何能:

将图像存储在一个变量中,以便在不更改标题的情况下从变量中回显图像。

You can do this in two ways.

您可以通过两种方式执行此操作。

In way one, you serialize the image in a string, and save the string in the session. Which is exactly the same as saving it server side, except that now the session GC should take care of clearing it for you. Then the IMG SRC you use will redirect to a script that takes the image and outputs it as image with proper MIME type.

在方法一中,您将图像序列化为一个字符串,并将该字符串保存在会话中。这与在服务器端保存它完全相同,除了现在会话 GC 应该负责为您清除它。然后您使用的 IMG SRC 将重定向到一个脚本,该脚本获取图像并将其输出为具有正确 MIME 类型的图像。

In way two, if the image is small enough, you can encode it as BASE64 and output it into a specially crafted IMG tag:

方式二,如果图片足够小,可以将其编码为BASE64,输出到特制的IMG标签中:

http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html

http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html

This saves you some time in connection, also. Of course the image must be reasonablysmall.

这也为您节省了一些连接时间。当然,图像必须相当小。

回答by Chris

  1. You can't save the actual image in a variable. Either you save the URL or copy the image (what you obvious don't want) to your server and save the path to the image
  2. See answer 1, you can't echo the image itself, only link it
  1. 您不能将实际图像保存在变量中。您可以保存 URL 或将图像(您显然不想要的)复制到您的服务器并保存图像的路径
  2. 看答案1,图片本身不能回显,只能链接

Edit: Okay obviously you can save images directly to a variable, but I don't recommend you to do this.

编辑:好的,显然您可以将图像直接保存到变量中,但我不建议您这样做。

回答by Tyler Eaves

No, that isn't possible. If you want to serve something, it has to exist on the server.

不,那是不可能的。如果你想提供一些东西,它必须存在于服务器上。