前言
幾乎每個程序都需要用到圖片。下面就來給大家介紹前端+php后端實現微信小程序實現圖片上傳功能小程序圖片裁剪上傳,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
方法如下:
一、wxml文件
上傳圖片
二、js文件
page({ /** * 頁面的初始數據 */ data: { //初始化為空 source:'' }, /** * 上傳圖片 */ uploadimg:function(){ var that = this;
wx.chooseimage({ //從本地相冊選擇圖片或使用相機拍照 count: 1, // 默認9 sizetype: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有 sourcetype: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 success:function(res){ //console.log(res) //前臺顯示 that.setdata({ source: res.tempfilepaths }) // 返回選定照片的本地文件路徑列表,tempfilepath可以作為img標簽的src屬性顯示圖片 var tempfilepaths = res.tempfilepaths
wx.uploadfile({ url: 'http://www.website.com/home/api/uploadimg', filepath: tempfilepaths[0], name: 'file', success:function(res){ //打印 console.log(res.data) } }) }
}) },)}
三、php后端代碼
// 上傳圖片 public function uploadimg() { $file = request()->file('file'); if ($file) { $info = $file->move('public/upload/weixin/'); if ($info) { $file = $info->getsavename();$res = ['errcode'=>0,'errmsg'=>'圖片上傳成功','file'=>$file]; return json($res); } } }
運行結果:
打印結果:
此時表示上傳成功!
總結
以上就是這篇文章的全部內容了小程序圖片裁剪上傳,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對七九推的支持。