0

Here my problem, I do a query to MySQL (PDO) for give me the last 5 URLs of a table nammed avatar who contains the ID and the URL :

$response = $dbh->query("SELECT url FROM avatar ORDER BY id_URL DESC LIMIT 0,5 ");

And I did :

  while ($donnees = $response->fetch())
{

$urlImage = $donnees['url']; //'url' contains the URL
$result = file_get_contents($urlImage);        
header('Content-Type: image/png');
echo $result;   
?>

But the header just return a small empty white square. However, the "$result = file_get_contents($urlImage);" takes properly the URL because when I do :

   $urlImage = $donnees['url']; //'url' contains the URL
$result = file_get_contents($urlImage);        
echo $result;   
?>

It just shows the "encodage of the image" (a ton of special characters) but not displays the image.

I also try with "imagick" but it says to me that the class doesn't exist and I don't think that the imagecreatefrompng can be use with URL.

Thanks !

6
  • @Hexadect Where's the decimal? Commented Feb 14, 2017 at 19:00
  • @BenM Can you set a LIMIT of 0,5? Commented Feb 14, 2017 at 19:01
  • @Hexadect Yes. The first integer is simply the offset. So LIMIT 10,5 would fetch 5 records, with an offset of 10. LIMIT 5 OFFSET 0 == LIMIT 0,5 == LIMIT 5. Commented Feb 14, 2017 at 19:04
  • @Hexadect Yes, the "LIMIT 0, 5" is here to indicate that the limit is between 0 and 5 but I think it's need to be reversed with the comma. There is an example in W3Schools. Commented Feb 14, 2017 at 19:12
  • @Mofallo999 Yes I know. I'm saying that LIMIT 0,5 is the same as LIMIT 5. Commented Feb 14, 2017 at 19:14

2 Answers 2

3

Can you try this and see if it works?

 $image = file_get_contents($donnees['url']);
 $finfo = new finfo(FILEINFO_MIME_TYPE);
 header('content-type: ' . $finfo->buffer($image));
 echo $image;

This is suppose to handle one Image. One php script can return one image. If you want to combine the images and render a big long image then probably you should look at http://image.intervention.io/

EDIT

What I understood after trying out the above code is that if you put file_get_contents before header then the raw characters are shown. However you put it after header then everything seems to be working

$image="http://www.hillspet.com/HillsPetUS/v1/portal/en/us/cat-care/images/HP_PCC_md_0130_cat53.jpg";
$filename = basename($image);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpeg"; break;
    default:
}

header('Content-type: ' . $ctype);
$image = file_get_contents($image);
echo $image;

Working fiddle

Sign up to request clarification or add additional context in comments.

6 Comments

No, I want to return one image, this is a loop so the variable will always render one image ? But that's the same that's my code, it's return meta data of the image.
You have written the header and echo inside the loop. That means it will echo it 5 times. So even though the client side thinks it is an image but it cannot render it as it is some combination for 5 image urls. Try doing limit 0,1
Ah, yes I understand what you say, but I make the Limit 0,1 and remove the loop, that's give the same result.
Hi, I just copy paste your code but this just returns a small empty white square.
@Mofallo999 please try out the demo.. the picture is coming fine. Also note that your url cannot have https, so replace that with http (if working with http) using $image = str_replace("https", "http", $image); after the first line
|
1

If you're trying to use a dynamic image source where your url is the image source, and it isn't working, then the problem might be that there's a space or extra character somewhere on the page, which will make the browser treat it like a document instead of an image in some cases.

Your problem is that the browser isn't understanding that it's supposed to be an image.

You could always do:

<img src="<?=$urlImage?>">

4 Comments

1. <img /> is a self-closing tag. 2. That's the purpose of header('Content-Type: image/png');.
@BenM. I come on and help someone where you couldn't and you give me a downvote?
@AuntJamaima I downvoted because this doesn't actually solve the problem. Hotlinking to an image inside of <img /> is not the same objective as the OP posted. They wish to output the image directly from a PHP script; those are two distinct problem sets.
@BenM. Where does he say he wants to output directly from the script? He's running a while loop and said he tried imagick and couldn't make it display. This is a case of server client confusion. Case in point he already said that this works in his comment above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.