[英]display a <img> from php( problem)
我想在里面返回一个链接到<img>
。 我不知道是什么问题。
类别.php
<HTML>....
<img src="categoriaMAIN.php?type=celular">
</HTML>
类别MAIN.php
<?php
$varcate= $_GET['type'];
if ($varcate == "celular")
echo "http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg";
?>
类别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;
//...
}
其他人似乎都提供了 readfile/grab 并转发了 content 方法。 我以为我会让 HTTP 协议为我们完成工作。
嗯,img 标签指向的是文本,而不是图像。
尝试:
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");
}
一点警告:如果你没有 output 在这里的图像,那么浏览器可能会抱怨它试图加载的图像。 您应该添加一个默认值。
Kristian 指出这对服务器来说是很多工作,他是对的。 如果你能设法使 img 标签的 src 直接更改,那就更好了。 然而,上面的内容会让你到达你要求 go 的地方,尽管它可能不是最好的选择。
img
标签必须指向实际图像——而不是包含 URL 的网页。
<img src="categoriaMAIN.php?type=celular">
必须得到评估
<img src="http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg">
现在不是这样。 如何做到这一点,很大程度上取决于您的源代码的 rest 以及您实际要完成的工作。
这行不通! <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";
?>
你给img
一个 PHP 页面作为它的src
。 所以浏览器期望 PHP 页面返回图像。 相反,您让该页面简单地将 URL 返回到图像。 你必须改变它,这样你才能真正echo
图像数据。 我对这一切有点生疏,但我认为你会做以下事情:
<?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');
}
?>
如果你真的想这样做,你的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");
}
?>
这将获取实际的图像数据并将其作为图像返回给浏览器,这正是浏览器所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.