a3rxander
a3rxander

Reputation: 906

display a <img> from php( problem)

I wanna return a link inside to <img>. I dont know 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";
?>

Upvotes: 1

Views: 335

Answers (6)

cwallenpoole
cwallenpoole

Reputation: 82078

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.

Upvotes: 3

ETWW-Dave
ETWW-Dave

Reputation: 732

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.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

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.

Upvotes: 5

sdleihssirhc
sdleihssirhc

Reputation: 42496

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');
}
?>

Upvotes: 0

kongr45gpen
kongr45gpen

Reputation: 453

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";
?>

Upvotes: 0

kba
kba

Reputation: 19476

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.

Upvotes: 0

Related Questions