Home >Backend Development >PHP Tutorial >Let's talk about the mutual conversion methods of Base64, Blob and File in PHP
This article brings you relevant knowledge about php. It mainly talks about how Base64, Blob and File are converted to each other? Friends who are interested can take a look below. I hope it will be helpful to everyone.
Preface
When obtaining pictures, I encountered a situation where I needed to convert the format, so I recorded it and shared it.
Text
1. Basic introduction to the format
Base64
Base64 is one of the most common encoding methods for transmitting 8Bit bytecode on the Internet. Base64 is a method of representing binary data based on 64 printable characters Base64 Document Entry
For example
object represents an immutable, raw data file-like object. Its data can be read in text or binary format, or converted into ReadableStream
for data operations. Blob document entry
##File
##File (
) interface provides information about a file and allows JavaScript in web pages to access its contents. File document entryFor example
2. How to judge these three formats
1. Determine whether it is a Base64 string
// 判断是否为base64格式字符串 function isBase64(str) { //正则表达式判断 var reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i; return reg.test(str) //返回 true or false }
console.log(data instanceof Blob) //ture or false
3 .Determine whether it is a File object
console.log(data instanceof File && !data instanceof Blob) //ture or falsePS:Both Blob and File use instanceof to determine whether it is the corresponding type of data
One thing to note is that the File object is also a Blob object , because File inherits from Blob, the judgment logic can be defined by yourself
3. Conversion between formats
1. Convert Base64 to File
function dataURLtoFile(dataurl, filename) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], filename, { type: mime }); }
You need to pass two parameters, the first is data, the second is a custom file name string
function dataURLtoBlob(dataurl, filename) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); }
is basically the same as transferFile
, except for the last sentence
3.Blob to Filefunction blobToFile(blob) { return new File([blob], 'screenshot.png', { type: 'image/jpeg' }) }
Here and
Base64 to File method. , the second parameter above is passed in. It is fixed here. This parameter is not very important. You can modify the function by yourself. The methods have been provided, so you can use it directly. Recommended learning: "
The above is the detailed content of Let's talk about the mutual conversion methods of Base64, Blob and File in PHP. For more information, please follow other related articles on the PHP Chinese website!