Base64 imagecreatefromstring and black squares?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
6 views
Hello. Need to make a saving intermediate results from the sketched canvas on the server.

One "but" on the saved image should be put, watermark.

Do the following:
var canvas = document.getElementById('canvas'), dataURL = canvas.toDataURL("image/jpeg"); //send a POST request with the dataURL


On the server side:
$dataURL = base64_decode(str_replace('data:image/jpeg;base64,', ", $_POST['dataURL'])); $image = imagecreatefromstring($dataURL); ob_start(); //somewhere, then put watermark imagejpeg($image); $filename = 'test.jpeg'; $binaryImage = ob_get_clean(); $file = fopen ($filename, 'w'); fwrite($file, $binaryImage);


And get a black square. However, if I do something like
$dataURL = file_get_contents('1.jpeg'); $image = imagecreatefromstring($dataURL); ob_start(); //somewhere, then put watermark imagejpeg($image); $filename = 'test.jpeg'; $binaryImage = ob_get_clean(); $file = fopen ($filename, 'w'); fwrite($file, $binaryImage);


It's all right. Which leads to doubt as if to speak more correctly, the correct base64 string on the client. The feeling that the canvas gives out something incomprehensible for php-shnoy imagecreatefromstring();

Faced one?

Thank you.
by | 6 views

4 Answers

0 like 0 dislike
There is an assumption that in POST['dataURL'] hits the entire row.
\r
print the base64 string, which is obtained in JS.
print the base64 string, which is obtained in POST['dataURL']
\r
compare them.
by
0 like 0 dislike
Checked your script. Exactly with the image/jpeg is not working. If javascriptом to encode to png, then all is well.
\r
From this conclusion that the problem is not with base64_decode, the problem is likely in the coding browser jpeg.
If you need jpeg as the output format, it may be better to convert on the server using the same GD?
by
0 like 0 dislike
What browser?
Right now I checked in Chrome. It turns out that if you call the method toDataURL('image/jpeg');
It displays image/png!
\r
In Firefox no such problem.
by
0 like 0 dislike
Checked right now.
\r
In Chrome 8, you can write what you want the parameter to toDataURL, even rubbish, it will always return image/png
\r
Firefox 3.6.13 on options looking: if toDataURL will send a parameter that Firefox does not know, he will not print anything.
While revealed that Firefox knows 2 parameter are image/png and image/jpg (or image/gif or image/bmp doesn't know)
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Aug 12, 2019 by bogdan_uman
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
27,943 users