Home > Backend Development > PHP Problem > What to do if php base64 encoding is garbled

What to do if php base64 encoding is garbled

PHPz
Release: 2023-04-24 15:37:32
Original
2380 people have browsed it

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解码
};
Copy after login

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:

  1. The base64 string contains prefixes such as data:image/jpeg;base64 and delimiters, which need to be removed first.
  2. The base64 string contains URL-encoded characters, such as , /, etc., which need to be decoded before decoding.
  3. The magic_quotes_gpc option is turned on in the php configuration, which causes characters such as single quotes and double quotes to be escaped, causing problems during decoding.
  4. During the transmission process, due to various reasons (such as Chinese encoding problems, etc.), part of the data is modified, which in turn affects base64 decoding.

The solution is as follows:

  1. Remove the prefix and delimiter

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);
Copy after login
  1. Decoding URL encoded characters

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);
Copy after login
  1. Disable magic_quotes_gpc option

Search for magic_quotes in php.ini, set it to Off, and then restart php to take effect.

magic_quotes_gpc = Off
Copy after login
  1. Data integrity check

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template