0

I've seen a few cases of this online but every solution I try yields the same result. I have tried various versions of this same bit of code but I still am unable to display an image, either from the internet or from a folder.

My code looks like this:

    <?php
    $img = 'anything.jpg';
    $getInfo = getimagesize($img);
    header('Content-type: ' . $getInfo['mime']);
    readfile($img);
    ?>

And then this in the appropriate spot in the html document:

<?php
echo "<img src ='$img'>";
?>

My output looks like this: (only it is about 100 lines long, covering most of the page)

„•J€m’¥Id„©,ƒð‹"È!dY*   
d¨E„Œœ6TêÞ BTç–dáfδ§Ýfΰɾ,ÙÔÌxà5IõcÁ=£TM´® ‡iNt4ÖMYí;ÅU©ª’§ª7’Ê:¡ú§šß”,¾5™ˆ   

The image displays after all this. I've been working at this quite a while now, any help would make a man's day.

2 Answers 2

3

The content-type of the image is in the array under mime, not img.

header('Content-type: ' . $getInfo['mime']);

More info is available in the PHP docs.

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

2 Comments

Thank you very much for your response. I fixed that. I was hopeful but unfortunately it didn't solve my error.
Your code to output the image is all that's in that file, right? And the HTML document is separate? Where in that page is $img defined? Is it just the URL of the first script?
0

I ended up solving my problem using the following code:

<?php
$img = 'anything.png';
header("Content-type:image/png");
imagepng($img);
?>

<?php
echo "<img src ='$img'>";
?>

Comments

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.