Recently, when developing a web application based on front-end and back-end separation, I encountered a rather difficult problem: after the front-end uses JavaScript to base64 encode the image, it is transmitted to the back-end PHP program for decoding, and the result is garbled code.
After many attempts and research, I have summarized some solutions and share them for your reference.
First, let’s understand the basic principles of base64 encoding and decoding. Base64 is a method of using 64 characters to represent binary data. The data is classified into groups of 6 bits. The corresponding characters are found in the predefined character set based on the 6-bit value to replace the bytecode in the original binary data. In this way, any data can be converted into printable ASCII characters for easy transmission and storage.
In JavaScript, you can generate a base64-encoded string containing image data by calling the toDataURL() function of the canvas object:
var canvas = document.createElement('canvas'); canvas.width = 400; canvas.height = 300; var ctx = canvas.getContext('2d'); ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, 400, 300); var img = new Image(); img.src = './test.jpg'; img.onload = function() { ctx.drawImage(img, 0, 0, 400, 300); var dataURL = canvas.toDataURL('image/jpeg', 0.8); // 传输dataURL给后端进行base64解码 };
In PHP, you can use the base64_decode() function to convert base64 to The encoded string is decoded into raw binary data for further manipulation.
However, during actual operation, we found that sometimes garbled characters appear. Why is this?
The reasons for garbled characters are as follows:
The solution is as follows:
You can replace data:image/jpeg with string replacement ;base64, remove prefixes and delimiters, and directly use base64 strings for decoding.
$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/…'; // base64编码的图片数据 $data = str_replace('data:image/jpeg;base64,', '', $data); $data = str_replace(' ', '+', $data); // 如果出现URL编码中的+,需要替换成空格。 $data = base64_decode($data);
Use the urldecode() function to decode URL-encoded characters, and then use base64_decode() for base64 decoding.
$data = 'data:image/jpeg;base64,%2F9j%2F4AAQSk...'; // base64编码的图片数据 $data = urldecode($data); $data = base64_decode($data);
Search for magic_quotes in php.ini, set it to Off, and then restart php to take effect.
magic_quotes_gpc = Off
When transmitting data at the front and back ends, the data can be integrity checked to ensure that the data has not been modified. For example, you can use md5() to digest the original data and transmit it to the backend. The backend then digests the received binary data and compares the two digest values. If they are the same, the data has not been modified and the decoding operation can be performed.
Summary:
The above are some solutions I summarized when solving the problem of garbled characters in php base64 decoding. You can choose and try according to your actual situation. When doing web development, don't be afraid when you encounter problems. Try more and consult more information. I believe you will always find a solution.
The above is the detailed content of What to do if php base64 encoding is garbled. For more information, please follow other related articles on the PHP Chinese website!