display a <img> from php( problem)
I wanna return a link inside to <img>
. I dont kn开发者_JS百科ow what is the problem.
categoria.php
<HTML>....
<img src="categoriaMAIN.php?type=celular">
</HTML>
categoriaMAIN.php
<?php
$varcate= $_GET['type'];
if ($varcate == "celular")
echo "http://ecx.images-amazon.com/images/I/41l1yyZuyXL._AA160_.jpg";
?>
categoriaMAIN.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. I thought I'd let the HTTP protocol do the work for us.
Well, the img tag is pointing to text, not an image.
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. You should add a default.
EDIT
Kristian made the point that this is a lot of work for the server and he is right. It would be much better if you could manage to make it so that the src of the img tag changed directly. The above, however, will get you where you are asking to go, though it may not be the best option.
The img
tag has to point at the actual image - not a webpage containing the 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.
This won't work! <img src=""
searches for the image file, not its location! Try this:
categoria.php
<?php $varcate='celular'; ?>
<html>....
<img src="<?php include('categoriaMAIN.php'); ?>">
</html>
categoriaMAIN.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
. So the browser is expecting that PHP page to return an image. Instead, you're having that page simply return a URL to the image. You'll have to change that so that you're actually echo
ing the image data. 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:
<?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.
精彩评论