将图像存储在变量中,稍后回显 [英] Store image in variable, echo later
问题描述
假设我有一个用户输入图像的URL。
Let's say I have a user enter the URL of an image.
经过URL验证后,我想获取图像并将其存储在PHP变量中。不是图像路径,而是实际图像本身。
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.
我在其他字符串之间添加了此图像保持变量,因此无法更改使用 header()
在回显图像之前。是否可以使用< img>
HTML标签?我真的不想将图像复制到我自己的服务器上...
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...
我该怎么做:
- 将图像存储在变量中,
- 从变量中回显图像而无需更改标题。
编辑:
我在上面说过,我将此图像放在另一个变量中,例如:
I said above that I am putting this image inside another variable, e.g.:
$str = "blah blah" . $var_holding_img . "more text";
是否可以在上面的字符串中插入将被图像替换的内容?以后可以解析变量 $ str
,用图像替换一些随机文本,例如 abg30j-as ...
Is it possible to insert something in the string above that will be replaced with the images? Can parse the variable $str
later to replace some random text like "abg30j-as" with the image...
推荐答案
我找到了自己的问题的答案:
I found an answer to my own question:
首先,我创建了另一个PHP文件,名为 img.php
:
First, I created another PHP file, called 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);
?>
然后,在原始PHP文件中,我使用以下PHP代码:
Then, in the original PHP file, I used this PHP code:
<?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;
?>
这样,PHP文件 img.php可以使用适当的标题,并且图像可以是作为HTML插入到其他任何PHP变量中。
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.
这篇关于将图像存储在变量中,稍后回显的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!