[英]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.