在laravel5.2中實現點選使用者頭像更改頭像的方法
阿新 • • 發佈:2020-11-19
檢視層
!!!自己下載jquery檔案和ajaxfileUpload的外掛
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> </style> </head> <script type="text/javascript"> //下面用於圖片上傳預覽功能 function setImagePreview(avalue) { var docObj=document.getElementById("doc"); var imgObjPreview=document.getElementById("preview"); if(docObj.files &&docObj.files[0]) { //火狐下,直接設img屬性 imgObjPreview.style.display = 'block'; //imgObjPreview.src = docObj.files[0].getAsDataURL(); //火狐7以上版本不能用上面的getAsDataURL()方式獲取,需要一下方式 imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); } else { //IE下,使用濾鏡 docObj.select(); var imgSrc = document.selection.createRange().text; var localImagId = document.getElementById("localImag"); //必須設定初始大小 localImagId.style.width = "150px"; localImagId.style.height = "180px"; //圖片異常的捕捉,防止使用者修改後綴來偽造圖片 try{ localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; } catch(e) { alert("您上傳的圖片格式不正確,請重新選擇!"); return false; } imgObjPreview.style.display = 'none'; document.selection.empty(); } ajaxFileUpload(); //上傳圖片 return true; } </script> <body> <center> <label> @foreach ($arr as $key=>$val) {{-- <img src="{{$val->n_img}}" alt="" height="100" width="100" class="qq"> <input type="file" id="file1" style="VISIBILITY: hidden" >--}} <img id="preview" width="100" height="100" src="{{$val->n_img}}"> <input type="file" name="touxiang" id="doc" style="display:none" οnchange="javascript:setImagePreview();"> @endforeach </label> </center> </body> </html> <script src="./js.js"></script> <script src="./ajaxfileupload.js"></script> <script type="text/javascript"> function ajaxFileUpload() { $.ajaxFileUpload ( { url: "{{url('up_img')}}", //用於檔案上傳的伺服器端請求地址 secureuri: false, //是否需要安全協議,一般設定為false fileElementId: 'doc' } ); return false; } </script>
控制器層
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Illuminate\Support\Facades\Input; class ImgController extends Controller { public function Index(){ $arr= DB::select("SELECT * FROM r_nav limit 1"); return view('index',['arr'=>$arr]); } //修改圖片 public function up_img(Request $Request){ $n_file = Input::file('touxiang'); if($n_file->isValid()){ //獲取檔名稱 $clientName = $n_file -> getClientOriginalName(); $realPath = $n_file -> getRealPath(); //獲取圖片格式 $entension = $n_file -> getClientOriginalExtension(); //圖片儲存路徑 $mimeTye = $n_file -> getMimeType(); $path = $n_file -> move('IMG'); } $ress = DB::table('r_nav')->where('n_id',11)->update(['n_img'=>$path]); } }
以上這篇在laravel5.2中實現點選使用者頭像更改頭像的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援碼農教程。