admin管理员组

文章数量:1427458

I am having issues sending an image as a string between Javascript and PHP. So far I have:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL

as the string which is being passed from the JS to PHP and this is verified as the image will load in chrome "as is".

I am using PHP to then

$im = imagecreatefromstring($data);

This however just errors, If I remove the data:image/png;base64 bit, it works but when it es to the larger files, this just does not work.

I'm just wondering what I could have missed here.

I am having issues sending an image as a string between Javascript and PHP. So far I have:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL

as the string which is being passed from the JS to PHP and this is verified as the image will load in chrome "as is".

I am using PHP to then

$im = imagecreatefromstring($data);

This however just errors, If I remove the data:image/png;base64 bit, it works but when it es to the larger files, this just does not work.

I'm just wondering what I could have missed here.

Share Improve this question asked Dec 28, 2011 at 1:12 Christopher BuckleyChristopher Buckley 1131 silver badge6 bronze badges 3
  • 2 Did you remember to base64_decode() the data before you pass it to imagecreatefromstring() – DaveRandom Commented Dec 28, 2011 at 1:15
  • I did try that but that didn't work at all. It works with just passing iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL – Christopher Buckley Commented Dec 28, 2011 at 1:17
  • @ChristopherBuckley: You're lying (or not pasting the correct data here). Just passing that string will get you: Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format in /code/Bce4M2 on line 6 An error occurred. - see codepad.viper-7./Bce4M2 – hakre Commented Dec 28, 2011 at 1:53
Add a ment  | 

2 Answers 2

Reset to default 1

You need to remove the header that is spat out from the canvas.toDataURL("image/png;base64");

you can either do it with javascript:

var imgData= canvas.toDataURL("image/png;base64");
var postData = {drawing:imgData.substr(22)};

or with php

substr($_POST['drawing'],22);

I got the same error, but if I try this :

echo '<img src="'.$data.'"/>';

Then the picture is displayed...

Into a shell this does not work :

echo 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cI' | base64 -d - >file.png

I got base64: invalid entry.

If I add a "=" :

echo 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL=' | base64 -d - >file.png

Then it is good. But alwas bad with imagecreatefromstring().

Could you tell us how did you get the base64 encoded content in javascript?

See this ment: http://www.php/manual/en/function.base64-decode.php#102113

Edit: This code works...

<?php
    $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL';
    $data = base64_decode($data);
    header('Content-Type: image/png');
    echo $data;
?>

本文标签: phpimagecreatefromstring issues with string passed from javascriptStack Overflow