search
HomeBackend DevelopmentPHP TutorialProblem with php saving Base64 image base64_decode

PHP has very good support for Base64, with built-in base64_encode and base64_decode responsible for Base64 encoding and decoding of images.

In terms of encoding, just read the image stream and then use base64_encode to encode it.

The decoding is a little more troublesome. The reason is that after encoding the image into a base64 string, these characters data:image/png;base64 will be added to the encoding, which is originally used for base64 recognition. However, if you put it directly into PHP and use the base64_decode function to decode it, the final saved image file format will be damaged. The solution is to remove this string of characters first:


$base64_string= explode(',', $base64_string); //截取data:image/png;base64, 这个逗号后的字符
$data= base64_decode($base64_string[1]);  //对截取后的字符使用base64_decode进行解码
file_put_contents($url, $data); //写入文件并保存

The above is the detailed content of Problem with php saving Base64 image base64_decode. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何在 Windows 11 上安全禁用 Windows Modules Installer Worker如何在 Windows 11 上安全禁用 Windows Modules Installer WorkerApr 13, 2023 pm 03:43 PM

无论您使用的是旧计算机还是需要您的 PC 同时运行许多任务,您都可能希望禁用 Windows 模块安装程序工作程序。原因是 Windows 模块安装程序工作人员对您的磁盘、CPU 和内存施加了很高的负载。您可能会使用最好的软件来修复高 CPU 使用率,但一些报告显示它甚至可能会占用 100% 的 CPU 使用率。虽然它可以帮助您维护一个更安全和可靠的系统,但它会付出代价。因此,您可以决定保留或禁用它以避免性能问题。在本文中,我们将详细探讨什么是 Windows 模块安装程序工作人员以及如何启用或

Oracle DECODE函数的高级用法及技巧分享Oracle DECODE函数的高级用法及技巧分享Mar 08, 2024 am 10:30 AM

Oracle数据库中的DECODE函数是一个非常常用的函数,它可以根据一个表达式的结果值在一组值中进行选择。DECODE函数的语法如下:DECODE(expression,search_value1,result1,search_value2,result2,...,default_result)其中,expression是要进行比较的表达式,s

Oracle DECODE函数详解及用法示例Oracle DECODE函数详解及用法示例Mar 08, 2024 pm 03:51 PM

Oracle中的DECODE函数是一种条件表达式,常用于在查询语句中根据不同的条件返回不同的结果。本文将详细介绍DECODE函数的语法、用法和示例代码。一、DECODE函数语法DECODE(expr,search1,result1[,search2,result2,...,default])expr:要进行比较的表达式或字段。search1,

linux base64加密解密怎么实现linux base64加密解密怎么实现May 14, 2023 am 11:58 AM

1、给文件file进行base64编码,并打印到标准输出[root@pps~]#base64filec25haWx3YXJyaW9yCg==也可以这样:[root@pps~]#catfile|base64c25haWx3YXJyaW9yCg==2、从标准输入读取文件内容,base64编码并打印到标准输出[root@pps~]#base64snailwarriorc25haWx3YXJyaW9yCg==3、对字符串"snailwarrior"编码,并打印到标准输出[root@p

Oracle中DECODE函数的基本用法解析Oracle中DECODE函数的基本用法解析Mar 07, 2024 pm 04:09 PM

Oracle中DECODE函数的基本用法解析在Oracle数据库中,DECODE函数是一种非常常用的函数,用于实现类似于多层if-else语句的逻辑判断和数值替换。DECODE函数的基本语法如下:DECODE(expr,search1,result1,search2,result2,...,default_result)其中参数含义如下:expr

php怎么实现Base64的编码和解码php怎么实现Base64的编码和解码Mar 24, 2023 pm 05:11 PM

Base64算法是一种将二进制数据转换为ASCII字符的编码方式,使得数据可以在传输过程中不被修改或损坏,同时也可以隐藏数据的真实内容。在PHP中,可以利用内置函数或手动编写代码实现Base64的编码和解码。

Java中的base64编码器怎么实现Java中的base64编码器怎么实现Apr 28, 2023 pm 04:04 PM

简介什么是Base64编码呢?在回答这个问题之前,我们需要了解一下计算机中文件的分类,对于计算机来说文件可以分为两类,一类是文本文件,一类是二进制文件。对于二进制文件来说,其内容是用二进制来表示的,对于人类是不可立马理解的。如果你尝试用文本编辑器打开二进制文件,可能会看到乱码。这是因为二进制文件的编码方式和文本文件的编码方式是不一样的,所以当文本编辑器尝试将二进制文件翻译成为文本内容的时候,就会出现乱码。对于文本文件来说,也有很多种编码方式,比如最早的ASCII编码和目前常用的UTF-8和UTF

如何使用base64命令加密linux命令并执行如何使用base64命令加密linux命令并执行May 17, 2023 pm 08:37 PM

1.加密命令echo"ls-l"|2.解密命令并执行echobHMgLWwK|base64-d|这时只看得到命令的回显,看不到命令本身。Linux有哪些版本Linux的版本有:Deepin、UbuntuKylin、Manjaro、LinuxMint、Ubuntu等版本。其中Deepin是国内发展最好的Linux发行版之一;UbuntuKylin是基于Ubuntu的衍生发行版;Manjaro是基于Arch的Linux发行版;LinuxMint默认的Cinnamon桌面类似Win

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools