這篇文章主要介紹了關於cropper+php+ajax實現上傳頭像,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8"><title>上傳頭像</title><link href="https://cdn.bootcss.com/cropper/3.1.3/cropper.min.css" rel="stylesheet"><link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><style type="text/css"> body{ text-align: center; } #user-photo { width:300px; height:300px; margin-top: 10px; } #photo { max-width:100%; max-height:350px; } .img-preview-box { text-align: center; } .img-preview-box > p { display: inline-block;; margin-right: 10px; } .img-preview { overflow: hidden; } .img-preview-box .img-preview-lg { width: 150px; height: 150px; } .img-preview-box .img-preview-md { width: 100px; height: 100px; } .img-preview-box .img-preview-sm { width: 50px; height: 50px; border-radius: 50%; }.cropper-view-box, .cropper-face { border-radius: 50%;}</style></head><body><button class="btn btn-primary" data-target="#changeModal" data-toggle="modal">開啟</button><br/><p class="user-photo-box"><img id="user-photo" src=""></p></p><p class="modal fade" id="changeModal" tabindex="-1" role="dialog" aria-hidden="true"><p class="modal-dialog"> <p class="modal-content"> <p class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title text-primary"> <i class="fa fa-pencil"></i> 更換頭像 </h4> </p> <p class="modal-body"> <p class="tip-info text-center"> 未選擇圖片 </p> <p class="img-container hidden"> <img src="" alt="" id="photo"> </p> <p class="img-preview-box hidden"> <hr> <span>150*150:</span> <p class="img-preview img-preview-lg"> </p> <span>100*100:</span> <p class="img-preview img-preview-md"> </p> <span>30*30:</span> <p class="img-preview img-preview-sm"> </p> </p> </p> <p class="modal-footer"> <label class="btn btn-danger pull-left" for="photoInput"> <input type="file" class="sr-only" id="photoInput" accept="image/*"> <span>開啟圖片</span> </label> <button class="btn btn-primary disabled" disabled="true" onclick="sendPhoto();">提交</button> <button class="btn btn-close" aria-hidden="true" data-dismiss="modal">取消</button> </p> </p></p></p><script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script><script src="https://cdn.bootcss.com/cropper/3.1.3/cropper.min.js"></script><script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><script type="text/javascript"> var initCropperInModal = function(img, input, modal){ var $image = img; var $inputImage = input; var $modal = modal; var options = { aspectRatio: 1, // 縱橫比 viewMode: 2, preview: '.img-preview' // 預覽圖的class名 }; // 模態框隱藏後需要儲存的資料對象 var saveData = {}; //var URL = window.URL || window.webkitURL; var blobURL; $modal.on('show.bs.modal',function () { // 如果開啟模態框時沒有選擇檔案就點擊“開啟圖片”按鈕 if(!$inputImage.val()){ $inputImage.click(); } }).on('shown.bs.modal', function () { // 重新建立 $image.cropper( $.extend(options, { ready: function () { // 當剪下介面就緒後,恢複資料 if(saveData.canvasData){ $image.cropper('setCanvasData', saveData.canvasData); $image.cropper('setCropBoxData', saveData.cropBoxData); } } })); }).on('hidden.bs.modal', function () { // 儲存相關資料 saveData.cropBoxData = $image.cropper('getCropBoxData'); saveData.canvasData = $image.cropper('getCanvasData'); // 銷毀並將圖片儲存在img標籤 $image.cropper('destroy').attr('src',blobURL); }); if (URL) { $inputImage.change(function() { var files = this.files; var file; if (!$image.data('cropper')) { return; } if (files && files.length) { file = files[0]; if (/^image\/\w+$/.test(file.type)) { if(blobURL) { URL.revokeObjectURL(blobURL); } blobURL = URL.createObjectURL(file); // 重設cropper,將映像替換 $image.cropper('reset').cropper('replace', blobURL); // 選擇檔案後,顯示和隱藏相關內容 $('.img-container').removeClass('hidden'); $('.img-preview-box').removeClass('hidden'); $('#changeModal .disabled').removeAttr('disabled').removeClass('disabled'); $('#changeModal .tip-info').addClass('hidden'); } else { window.alert('請選擇一個影像檔!'); } } }); } else { $inputImage.prop('disabled', true).addClass('disabled'); } } var sendPhoto = function(){ // 得到PNG格式的dataURLvar photo = $('#photo').cropper('getCroppedCanvas', {width: 300,height: 300}).toDataURL('image/png');$.ajax({url: 'http://localhost/test/upload.php', // 要上傳的地址type: 'post',data: {'imgData': photo},dataType: 'json',success: function (data) {if (data.status == 0) {// 將上傳的頭像的地址填入,為保證不載入緩衝加個隨機數$('.user-photo').attr('src', '頭像地址?t=' + Math.random());$('#changeModal').modal('hide');} else {alert(data.info);}}}); } $(function(){ initCropperInModal($('#photo'),$('#photoInput'),$('#changeModal')); });</script></body></html>
ini_set('date.timezone','Asia/Shanghai');/** * [將Base64圖片轉換為本地圖片並儲存] * @E-mial wuliqiang_aa@163.com * @TIME 2017-04-07 * @WEB http://blog.iinu.com.cn * @param [Base64] $base64_image_content [要儲存的Base64] * @param [目錄] $path [要儲存的路徑] */$base64_image_content = $_POST['imgData'];$path="./upload";echo base64_image_content($base64_image_content,$path);function base64_image_content($base64_image_content,$path){ //匹配出圖片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2]; $new_file = $path."/".date('Ymd',time())."/"; if(!file_exists($new_file)){ //檢查是否有該檔案夾,如果沒有就建立,並給予最高許可權 mkdir($new_file, 0700); } $new_file = $new_file.time().".{$type}"; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){ return '/'.$new_file; }else{ return false; } }else{ return false; }}
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!