Gregoire
Gregoire

Reputation: 198

obtain an Image with Jquery and Php

I want to obtain images from the server.

The html code :

<div id='container'></div>

Here is my code in jquery :

$.post('php/Image.php', function(data) {
        $('#container').html(data);
    });

And the php code :

header('Content-Type: image/png');
$img = imagecreatefrompng("image.png");
imagepng($img);   

On my web page I can see the "binary" of the image and that's not what I expect. Somebody can tell me what I'm doing wrong ? Or if there is a better solution to do that.

Thanks in advance for your help.

Upvotes: 0

Views: 95

Answers (2)

Bojangles
Bojangles

Reputation: 101473

Assuming that your PHP isn't stripped down (only reads an image and sends it), you can simply request the image from the server by changing the src attribute of your img tag, like so.

All you need to add now is an <img> tag to your HTML:

$('#container').html('<img src="php/Image.php">');

Upvotes: 0

zerkms
zerkms

Reputation: 254896

You don't need to request the image body itself, but insert an img tag with link to it:

$('#container').html('<img src="php/Image.php" />');

Upvotes: 1

Related Questions