Problems with imagecreatefromstring with string passed from javascript

I'm having trouble sending an image as a string between Javascript and PHP. So far, I:

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

like a string that is passed from JS to PHP, and this is checked as the image is loaded in chrome "as is".

I use php and then

 $im = imagecreatefromstring($data); 

These, however, are simply mistakes. If I delete the data: image / png; base64 bit, it works, but when it comes to large files, it just doesn't work.

I'm just wondering what I could skip here.

+6
source share
2 answers

You need to remove the header that spits out of canvas.toDataURL ("image / png; base64");

you can do it with javascript:

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

or with php

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

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

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

Then the image is displayed ...

This does not work in the shell:

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

I got base64: invalid entry.

If I add "=":

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

Then it’s good. But also bad with imagecreatefromstring ().

Could you tell us how you got base64 encoded content in javascript?

See this comment: http://www.php.net/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; ?> 
0
source

Source: https://habr.com/ru/post/904568/


All Articles