php base64转图片保存本地

调用函数

php 复制代码
   public function base64(){
       $img =  $this->request->param('img');
       $img =  'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCADUANQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDcooor1jjCiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiilSOWedLe3jMk0hwqjvUzmoK7KjFydkMZ1TqasxafqM67odPuWU9G8s4NdronhW201VnuQlxeddxGVQ/wCyP6/yroe1cM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ZFerV5tdWqM66XwISiiisjQKoaho1hqY/0q2R3A4ccMPxH8qv0UAYNroN1p0qiz1J2tgQDBcJv49jkY/AVsCLbU1FKw+ZlW4uILSMPcTJEhO0M7YGamRldAyMGU9CDkU25tYLyEw3MSSxnqrDNYL+EIYSz6bf3dlIf7jkr+XBP50ahc6Siua+x+K7Y/udRtbmNegmXBP5D+tINR8VRE+do0Eg9Y5QP/AGY0XCx0mKjesRNT8QysANCVOeS9wMD/AD+Nbuwlfm4J645xQBWcZquwqvNpmtliYtaTaTkBrVeKgOia5KMS66qj/pnbL/8AWqWjSM0i03AyeKzbrW7eBxBbq13ctwsUPzHPuRVkeEYJgpvtQvbkjqrSYU/h2/Otiy0yy05CtpbJFnqQMk/Unk0uVjdXsZOlaLPJdjUtW2tcD/VQDlYv8TXQ0UVaVjFu5R1fU4tJ02S6kwSOETP3mPQf57ZrzGWWW8uZLu5ffNIck+lbHivUv7Q1f7PGcwWpK/V+5/p+HvWNmuvDU7vnZhVnb3UGaKKK7TmCiiigAooooAltbkWd7b3LKGEcqsQfQGvWe1eOz/6luK9bs5fOsbeUnJeNW/MVwYn+IddH4Saiiiuc1CiiigAooooAKKKKACiiigAooooAKKKKACiiigArA8T66+k24hijbzrhCIpM8KQQDx7A5rfrkfHcObexuP7spT8xn+lHURxyjaPc8k+tOoor1kklZHC227sKKKKYgooooAKKKKAGS/6pvpXqeinOhWB9baP/ANBFeWyf6pvpXpnh1w/h6x4xiIL+XFcGK+Neh1UfhNOiiiuc2CiiigAooooAKKKKACiiigAooooAKKKKACiiigArl/HX/IHtf+vpf/QWrqK53xrHv8PM+P8AVyo364/rQwRwlFFFeujgCiiigQUUUUAFFFFADZP9U30r03w6APD1iB/zyoorgxXxr0Oqj8JpUUUVzmwUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFYni4Z8MXef9j/0NaKKUtgR54v3R9KWiivXWxwPcKKKKYgooooA//9k=';
       return  $this->base64_image($img,'storage','222');
    }

转图片并保持到本地

php 复制代码
public function base64_image($base64_image_content,$path,$imgname=''){
    /**
     * [将Base64图片转换为本地图片并保存]
     * @E-mial 332410549@qq.com
     * @TIME   2023-08-07
     * @param  [Base64] $base64_image_content [要保存的Base64]
     * @param  [目录] $path [要保存的路径]
    */
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            $new_file = $path."/".date('Ymd',time())."/";
            if(!file_exists($new_file)){
                //检查是否有该文件夹,如果没有就创建,并给予最高权限
                mkdir($new_file, 0700);
            }
            $img = '';
            $img = empty($imgname) ?  time() : $imgname;
            $new_file = $new_file.$img.".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                return '/'.$new_file;
            }else{
                return false;
            }
        }else{
            return false;
        }


}

#结果

相关推荐
问道飞鱼几秒前
【前端知识】强大的js动画组件anime.js
开发语言·前端·javascript·anime.js
拓端研究室几秒前
R基于贝叶斯加法回归树BART、MCMC的DLNM分布滞后非线性模型分析母婴PM2.5暴露与出生体重数据及GAM模型对比、关键窗口识别
android·开发语言·kotlin
Code成立2 分钟前
《Java核心技术I》Swing的网格包布局
java·开发语言·swing
Auc246 分钟前
使用scrapy框架爬取微博热搜榜
开发语言·python
QQ同步助手13 分钟前
C++ 指针进阶:动态内存与复杂应用
开发语言·c++
凯子坚持 c19 分钟前
仓颉编程语言深入教程:基础概念和数据类型
开发语言·华为
小爬虫程序猿21 分钟前
利用Java爬虫速卖通按关键字搜索AliExpress商品
java·开发语言·爬虫
程序猿-瑞瑞23 分钟前
24 go语言(golang) - gorm框架安装及使用案例详解
开发语言·后端·golang·gorm
qq_4335545424 分钟前
C++ 面向对象编程:递增重载
开发语言·c++·算法
易码智能32 分钟前
【EtherCATBasics】- KRTS C++示例精讲(2)
开发语言·c++·kithara·windows 实时套件·krts