php 读取base64到网页显示并存到新文件
- <?php
-
- function bs64toimg($btxt,$path){
- //匹配出图片的格式
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $btxt, $result)){
- $type = $result[2];
- $new_file = $path;
- if(!file_exists($new_file)){
- mkdir($new_file, 0700);
- }
- $new_file .= date('Ymdhis').".{$type}";
- if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $btxt)))){
- return '/'.$new_file;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
-
- function imgtobs64($image_file) {
- $base64_image = '';
- $image_info = getimagesize($image_file);
- $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
- $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
- return $base64_image;
- }
-
- $img = 'imgstotxt.jpg';
- $base64_img = imgtobs64($img);//读取图片(imgfiles to base64)
- echo bs64toimg($base64_img,"uploads/");//写入图片(base64 to imgfiles)
-
- ?>
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。