[英]display a <img> from php( problem)
I wanna return a link inside to <img>
.我想在里面返回一个链接到
<img>
。 I dont know what is the problem.我不知道是什么问题。
categoria.php类别.php
<HTML>....
<img src="categoriaMAIN.php?type=celular">
</HTML>
categoriaMAIN.php类别MAIN.php
<?php
$varcate= $_GET['type'];
if ($varcate == "celular")
echo "http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg";
?>
categoriaMAIN.php类别MAIN.php
<?php
switch ($_GET['type'])
{
case 'celular':
header('Location: http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg');
break;
case '...':
header('Location: http://somesite.com/some/img/path/image.jpg');
break;
//...
}
Everyone else seemed to offer the readfile/grab and forward the content method.其他人似乎都提供了 readfile/grab 并转发了 content 方法。 I thought I'd let the HTTP protocol do the work for us.
我以为我会让 HTTP 协议为我们完成工作。
Well, the img tag is pointing to text, not an image.嗯,img 标签指向的是文本,而不是图像。
Try:尝试:
header("Content-Type: image/jpg"); //tell the browser that this is an image.
$varcate= $_GET['type']; // you know this part
if ($varcate == "celular")
{
// readfile will grab the file and then output its contents without
// procressing it.
readfile("http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg");
}
Bit of a warning: if you don't output an image here, then the browser will probably complain about the image it is trying to load.一点警告:如果你没有 output 在这里的图像,那么浏览器可能会抱怨它试图加载的图像。 You should add a default.
您应该添加一个默认值。
Kristian made the point that this is a lot of work for the server and he is right. Kristian 指出这对服务器来说是很多工作,他是对的。 It would be much better if you could manage to make it so that the src of the img tag changed directly.
如果你能设法使 img 标签的 src 直接更改,那就更好了。 The above, however, will get you where you are asking to go, though it may not be the best option.
然而,上面的内容会让你到达你要求 go 的地方,尽管它可能不是最好的选择。
The img
tag has to point at the actual image - not a webpage containing the URL. img
标签必须指向实际图像——而不是包含 URL 的网页。
<img src="categoriaMAIN.php?type=celular">
has to get evalulated to必须得到评估
<img src="http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg">
It isn't being right now.现在不是这样。 How to accomplish this, greatly depends on the rest of your source code and what you actually are trying to accomplish.
如何做到这一点,很大程度上取决于您的源代码的 rest 以及您实际要完成的工作。
This won't work!这行不通!
<img src=""
searches for the image file, not its location: Try this: <img src=""
搜索图像文件,而不是它的位置:试试这个:
<?php $varcate='celular'; ?>
<html>....
<img src="<?php include('categoriaMAIN.php'); ?>">
</html>
<?php
if ($varcate == "celular")
echo "http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg";
?>
You give the img
a PHP page as its src
.你给
img
一个 PHP 页面作为它的src
。 So the browser is expecting that PHP page to return an image.所以浏览器期望 PHP 页面返回图像。 Instead, you're having that page simply return a URL to the image.
相反,您让该页面简单地将 URL 返回到图像。 You'll have to change that so that you're actually
echo
ing the image data.你必须改变它,这样你才能真正
echo
图像数据。 I'm a little rusty will all this, but I think you'd do something like the following:我对这一切有点生疏,但我认为你会做以下事情:
<?php
$varcate = $_GET['type'];
if ($varcate == 'celular') {
header('Content-type: image/jpg');
echo file_get_contents('http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg');
}
?>
If you really want to do it this way, your categoriaMAIN.php
would need to look more like:如果你真的想这样做,你的
categoriaMAIN.php
需要看起来更像:
<?php
$varcate = $_GET['type'];
if ($varcate == "celular") {
header("Content-Type: image/jpeg");
echo file_get_contents("http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg");
}
?>
This will get the actual image data and return it to the browser as an image, which is what the browser needs.这将获取实际的图像数据并将其作为图像返回给浏览器,这正是浏览器所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.