小程序的图片怎么转成base64

发布时间:2022-03-07 10:28:14 作者:iii
来源:亿速云 阅读:526

本文小编为大家详细介绍“小程序的图片怎么转成base64”,内容详细,步骤清晰,细节处理妥当,希望这篇“小程序的图片怎么转成base64”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

  目前,小程序图片或者本地文件转成base64是不能够实现的,以下是具体分析:

  微信小程序wx.uploadfile, 利用PHP接口把本地图片转化为base64流.

  网上到处都是粘贴复制的wx.uploadfile的解决方案, 但目前还没有具体的代码:

小程序的图片怎么转成base64

  特意呈现出来给需要的伙伴们:

  代码:

<pre name="code" class="php">case 'imgBufferToBase64':

<span style="white-space:pre"> </span>$data='';

<span style="white-space:pre"> </span>file_put_contents("1.txt",var_export($_FILES,true)."\r\n",FILE_APPEND);

<span style="white-space:pre"> </span>if(!empty($_FILES['upload']['tmp_name'])){

<span style="white-space:pre"> </span>if(empty($_FILES['upload']['type'])){

<span style="white-space:pre"> </span>IO::Debug('文件类型不合法');

<span style="white-space:pre"> </span>}

<span style="white-space:pre"> </span>if(!in_array($_FILES['upload']['type'],array(

<span style="white-space:pre"> </span>'image/gif',

<span style="white-space:pre"> </span>'image/pjpeg',

<span style="white-space:pre"> </span>'image/jpeg',

<span style="white-space:pre"> </span>'image/x-png',

<span style="white-space:pre"> </span>'image/png',

<span style="white-space:pre"> </span>'image/bmp'

<span style="white-space:pre"> </span>))){

<span style="white-space:pre"> </span>IO::Debug($_FILES['upload']['type'].'文件类型不合法');

<span style="white-space:pre"> </span>}

<span style="white-space:pre"> </span>$data=file_get_contents($_FILES['upload']['tmp_name']);

<span style="white-space:pre"> </span>file_put_contents("1.txt",var_export($data,true)."\r\n",FILE_APPEND);

<span style="white-space:pre"> </span>IO::Debug('解析成功',true,$data);

<span style="white-space:pre"> </span>}

<span style="white-space:pre"> </span>IO::Debug("解析失败");

break;

</pre>

<pre></pre>

<p></p>

<pre></pre>

<span style="font-size:14px">js代码:</span>

<p></p>

<pre name="code" class="javascript">wx.uploadFile({

  url: 'https://' + app.globalData.host + '/api/?sign=' + sign, 

  filePath: tempFilePaths[0],

  name: 'upload',

  header: { 

"content-type": "multipart/form-data",

"content-type": "application/x-www-form-urlencoded"

  },

  formData: formData,

  success: function (res) {</pre><pre name="code" class="javascript"><span style="white-space:pre"> </span>var $data = JSON.parse(res.data);</pre><pre name="code" class="javascript">     if (typeof ($data.data) != "undefined" && $data.code){

        var imgBase64 = "data:image/jpeg;base64," + $data.data;

     }

  }

})</pre><br data-filtered="filtered">

<p></p>

<p><br data-filtered="filtered">

</p>

<span style="font-size:14px"><br data-filtered="filtered">

<br data-filtered="filtered">

</span>

<p></p>

<pre></pre>

读到这里,这篇“小程序的图片怎么转成base64”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如何实现html5把上传的图片转成base64编码在显示
  2. php怎样把图片转成base64编码格式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

小程序 base64

上一篇:清除浮动的css写法是什么

下一篇:CSS的优点是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》