Ghazanfar Idrees
Ghazanfar Idrees

Reputation: 31

base64 image not decoding php

I have an image in HTML5 canvas which is taken from webcam's live stream. When I get a frame as snapshot and save it in canvas I do this

var myImage = canvas.toDataURL("image/png");
dom.write('<form method="post" action="upload.php"><input type="myImage" value="'+myImage+'" /><input type="submit" value="Submit" /></form>')

and in upload.php I do this

header("Content-type: image/png");
$img= $_GET["myImage"];
echo '<img src="data:image/gif;base64,'.$img.'" />';

doing this I get a popup window when page is loaded and when the form is submitted, it give me

data:image/png;base64,"VERY HUGE STRING"

I want to get it as an image. Please Help

Upvotes: 1

Views: 442

Answers (2)

jabbink
jabbink

Reputation: 1291

canvas.toDataURL() already includes the data:image/***;base64, part, so remove it from your output.

Upvotes: 2

Lucas Jones
Lucas Jones

Reputation: 20193

Your call to header suggests you want to output the actual PNG image as the response to the request. However, you then write out an HTML tag, which is not valid in a PNG file.

If you want to write out the PNG image itself, presuming $img contains a valid PNG-format image, you can decode the base64 with the base64_decode function:

echo base64_decode($img);

If you want to show the image on an HTML page, change your header call to read Content-Type: text/html.

Upvotes: 2

Related Questions