简体   繁体   English

显示一个<img>来自 php(问题)

[英]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.您应该添加一个默认值。

EDIT编辑

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=""搜索图像文件,而不是它的位置:试试这个:

categoria.php类别.php

<?php $varcate='celular'; ?>
<html>....
<img src="<?php include('categoriaMAIN.php'); ?>">
</html>

categoriaMAIN.php类别MAIN.php

<?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.这将获取实际的图像数据并将其作为图像返回给浏览器,这正是浏览器所需要的。

尝试显示来自 PHP 脚本的回显图像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目标:</p><p> 1)我在 web 根目录之外有一个图像文件夹,其中放置了来自 web 应用程序的所有图像(每个图像路径都保存在数据库表中);</p><p> 2) 我想根据用户的请求获取该图像。</p><p> 很简单,对吧?</p><p> 我做了:</p><p> a) 创建一个 class 来读取图像(具有我记得的所有安全功能和保护措施)。 它(成功地)返回使用相应的 mime 类型编码(base64)的图像。</p><p> b) 出于演示目的,我创建了一个 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在这里唯一的目标是从我的 web 应用程序中的任何其他地方调用一个脚本,该脚本输出具有相应 mime 类型的 base64 编码图像。</p><p> c) 创建了一个脚本“agivenscript.php”,它通过 GET 接收图像名称,实例化我的 class 并在一切顺利时获取 base64 编码图像。 然后它与图像相呼应。</p><p> 怎么了:</p><ul><li> 当我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一个魅力;</li><li> 此外,如果我将 $output64encodedwithmimetype 内容直接放在 index.php 的 src 标记中,它也可以工作。</li><li> 但是,当我尝试&lt;img src="agivenscript.php?img=name.jpeg"&gt;时它不起作用。</li></ul><p> 由于 base64 编码的图像显然很好(并且具有 mime 类型),我错过了什么? 任何想法?</p><p> 预先感谢。</p></div> - Trying to display an echoed image from a PHP script in and `<img src=“…”` tag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用php无法显示数据库中的img - Display img from database with php not working 在PHP和MySQL中显示URL中的img - Display img from URL in PHP & MySQL php img悬停并显示到其他img标签中 - php img hover and display into a different img tag 来自 getImage.php 的图像不会显示在 img src 中 - Image from getImage.php won't display in img src 尝试显示来自 PHP 脚本的回显图像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目标:</p><p> 1)我在 web 根目录之外有一个图像文件夹,其中放置了来自 web 应用程序的所有图像(每个图像路径都保存在数据库表中);</p><p> 2) 我想根据用户的请求获取该图像。</p><p> 很简单,对吧?</p><p> 我做了:</p><p> a) 创建一个 class 来读取图像(具有我记得的所有安全功能和保护措施)。 它(成功地)返回使用相应的 mime 类型编码(base64)的图像。</p><p> b) 出于演示目的,我创建了一个 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在这里唯一的目标是从我的 web 应用程序中的任何其他地方调用一个脚本,该脚本输出具有相应 mime 类型的 base64 编码图像。</p><p> c) 创建了一个脚本“agivenscript.php”,它通过 GET 接收图像名称,实例化我的 class 并在一切顺利时获取 base64 编码图像。 然后它与图像相呼应。</p><p> 怎么了:</p><ul><li> 当我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一个魅力;</li><li> 此外,如果我将 $output64encodedwithmimetype 内容直接放在 index.php 的 src 标记中,它也可以工作。</li><li> 但是,当我尝试&lt;img src="agivenscript.php?img=name.jpeg"&gt;时它不起作用。</li></ul><p> 由于 base64 编码的图像显然很好(并且具有 mime 类型),我错过了什么? 任何想法?</p><p> 预先感谢。</p></div> - Trying to display an echoed image from a PHP script in and `<img src=“…”` tag 使用php通过传递给html来显示图像img标签包含从php传递的变量 - Using php to display an image by passing to html img tag contains variables passed from php 从PHP和andriod studio编码时的Json对象img url问题 - Json object img url problem when encode from PHP and andriod studio PHP函数显示问题 - PHP function display problem PHP和MySQL显示问题 - PHP & MySQL display problem MySQL和PHP显示问题? - MySQL and PHP display problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM