php 读取base64到网页显示并存到新文件

本文阅读 1 分钟
首页 PHP笔记 正文
  1. <?php
  2. function bs64toimg($btxt,$path){
  3. //匹配出图片的格式
  4. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $btxt, $result)){
  5. $type = $result[2];
  6. $new_file = $path;
  7. if(!file_exists($new_file)){
  8. mkdir($new_file, 0700);
  9. }
  10. $new_file .= date('Ymdhis').".{$type}";
  11. if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $btxt)))){
  12. return '/'.$new_file;
  13. }else{
  14. return false;
  15. }
  16. }else{
  17. return false;
  18. }
  19. }
  20. function imgtobs64($image_file) {
  21. $base64_image = '';
  22. $image_info = getimagesize($image_file);
  23. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  24. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  25. return $base64_image;
  26. }
  27. $img = 'imgstotxt.jpg';
  28. $base64_img = imgtobs64($img);//读取图片(imgfiles to base64)
  29. echo bs64toimg($base64_img,"uploads/");//写入图片(base64 to imgfiles)
  30. ?>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP获取数组最后一个元素的键和值
« 上一篇 03-29
PHP原生socket与Websocket握手、解码、加码
下一篇 » 04-01

发表评论

惪特博客
  • 文章总数:
    18496 篇
  • 评论总数:
    53284 条
  • 标签总数:
    8869 个
  • 总浏览量:
    22082563 次
  • 最后更新:
    一天前

最多点赞

随便看看

标签TAG