cropper.js和exif.js實(shí)現(xiàn)頭像上傳縮放裁剪旋轉(zhuǎn)
本文實(shí)例為大家分享了cropper.js和exif.js實(shí)現(xiàn)頭像上傳縮放裁剪旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
做了一個頭像上傳的小功能,同時處理了ios豎著拍照圖片旋轉(zhuǎn)的問題。cropper.js(注意:cropper壓縮版的js在手機(jī)版版縮放圖片會有黑屏和圖片飛了的bug,所以建議引入開發(fā)版本的cropper)依賴jquery,exif是用來獲取拍照信息的,用于修復(fù)ios豎著拍照旋轉(zhuǎn)的問題,我用的是amazeui框架,我在文件中也引入。
這是html文件
<!doctype html> <html lang="zh"> <head> ?? ?<meta charset="UTF-8"> ?? ?<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> ?? ?<meta name="viewport" content="width=device-width, initial-scale=1.0"> ?? ?<title>cropper圖片裁剪縮放</title> ?? ?<link rel="stylesheet" href="css/amazeui.min.css"> ?? ?<link rel="stylesheet" href="css/cropper.css"> ?? ?<link rel="stylesheet" href="css/amazeui.cropper.css"> </head> <body> ?? ?<div class="am-form-group"> ?? ??? ?<label>logo</label> ?? ??? ?<div class="am-cf"> ?? ??? ??? ?<a href="javascript:;" id="up-img-touch" data-am-modal="{target: '#doc-modal-1'}"> ?? ??? ??? ??? ?<img src="picture-2.jpg" id="pic_img" style="width: 100px;"> ?? ??? ??? ??? ?<input type="hidden" name="new_pic" id="new_pic" value=""> ?? ??? ??? ?</a> ?? ??? ?</div> ?? ?</div> ?? ?<!--圖片上傳框--> ?? ?<div class="am-popup up-frame-bj " id="doc-modal-1"> ?? ??? ?<div class="am-modal-dialog up-frame-parent up-frame-radius"> ?? ??? ??? ?<div class="header_check header_setting"> ?? ??? ??? ??? ?<a href="javascript:;" class="iconfont" data-am-modal-close>關(guān)閉</a> ?? ??? ??? ??? ?<h1>上傳</h1> ?? ??? ??? ?</div> ?? ??? ??? ?<div class="up-frame-body"> ?? ??? ??? ??? ?<div class="up-pre-main am-cf" > ?? ??? ??? ??? ??? ?<div class="up-pre-before up-frame-radius"> ?? ??? ??? ??? ??? ??? ?<img alt="" src="" id="image"> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ?</div> ?? ??? ??? ??? ?<div class="upload_btn am-cf"> ?? ??? ??? ??? ??? ?<div class="am-fl am-form-file"> ?? ??? ??? ??? ??? ??? ?<button type="button" class="am-btn">上傳圖片</button> ?? ??? ??? ??? ??? ??? ?<input type="file" id="inputImage"> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?<div class="rotateimg"> ?? ??? ??? ??? ??? ??? ?<span οnclick="rotateimgleft()">左</span> ?? ??? ??? ??? ??? ??? ?<span οnclick="rotateimgright()">右</span> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?<div class="am-fr"> ?? ??? ??? ??? ??? ??? ?<button type="button" class="am-btn" ?id="up-btn-ok" url="admin/user/upload.action">確定</button> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ?</div> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </body> <script src="js/jquery.min.js" charset="utf-8"></script> <script src="js/amazeui.min.js" charset="utf-8"></script> <script src="js/cropper.js" charset="utf-8"></script> <script src="js/exif.js" charset="utf-8"></script> <script src="js/custom_up_img.js" charset="utf-8"></script> </html>
這是js文件
$(function() { ? ? 'use strict'; ? ? // 初始化 ? ? var $image = $('#image'); ? ? $image.cropper({ ? ? ? ? aspectRatio:1/1, ? ? ? ? viewMode : 1, ? ? ? ? autoCropArea:0.8, ? ? ? ? dragMode:'move', ? ? ? ? cropBoxMovable:false, ? ? ? ? cropBoxResizable:false, ? ? ? ? zoomOnTouch:true, ? ? ? ? zoomable:true, ? ? ? ? movable:true, ? ? }); ? ? var $inputImage = $('#inputImage'); ? ? var URL = window.URL || window.webkitURL; ? ? var Orientation ? ? var rotate_num ? ? var blobURL; ? ? if (URL) { ? ? ? ? $inputImage.change(function () { ? ? ? ? ? ? var files = this.files; ? ? ? ? ? ? var file; ? ? ? ? ? ? if (files && files.length) { ? ? ? ? ? ? ? ? file = files[0]; ? ? ? ? ? ? ? ? if (/^image\/\w+$/.test(file.type)) { ? ? ? ? ? ? ? ? ? ? blobURL = URL.createObjectURL(file); ? ? ? ? ? ? ? ? ? ? $image.attr("src",blobURL) ? ? ? ? ? ? ? ? ? ? EXIF.getData(file, function() { ? ? ? ? ? ? ? ? ? ? ? ? EXIF.getAllTags(this); ? ? ? ? ? ? ? ? ? ? ? ? Orientation = EXIF.getTag(this, 'Orientation'); ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? ? ? $image.one('built.cropper', function () { ? ? ? ? ? ? ? ? ? ? ? ? // Revoke when load complete ? ? ? ? ? ? ? ? ? ? ? ? URL.revokeObjectURL(blobURL); ? ? ? ? ? ? ? ? ? ? }).cropper('reset', true).cropper('replace', blobURL); ? ? ? ? ? ? ? ? ? ? $inputImage.val(''); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? window.alert('Please choose an image file.'); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } else { ? ? ? ? $inputImage.prop('disabled', true).parent().addClass('disabled'); ? ? } ? ? $('#up-btn-ok').on('click',function(){ ? ? ? ? var img_src=$image.attr("src"); ? ? ? ? if(img_src==""){ ? ? ? ? ? ? $('#my-alert').modal('open'); ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ? var url=$(this).attr("url"); ? ? ? ? var canvas=$("#image").cropper('getCroppedCanvas'); ? ? ? ? var cv_img = document.createElement("canvas"); ? ? ? ? var ctx = cv_img.getContext("2d"); ? ? ? ? var x = canvas.width/2; ? ? ? ? var y = canvas.height/2; ? ? ? ? cv_img.width =canvas.width; ? ? ? ? cv_img.height =canvas.width; ? ? ? ? ctx.clearRect(0,0, canvas.width, canvas.height);//先清掉畫布上的內(nèi)容 ? ? ? ? ctx.translate(x,y);//將繪圖原點(diǎn)移到畫布中點(diǎn) ? ? ? ? if(Orientation == 6) { ? ? ? ? ? ? ctx.rotate(Math.PI/2); ? ? ? ? } else if(Orientation == 3) { ? ? ? ? ? ? ctx.rotate(-Math.PI/2); ? ? ? ? } else if(Orientation == 8) { ? ? ? ? ? ? ctx.rotate(Math.PI/1); ? ? ? ? } ? ? ? ? ctx.translate(-x,-y);//將畫布原點(diǎn)移動 ? ? ? ? ctx.drawImage(canvas,0,0); ? ? ? ? var data=cv_img.toDataURL("image/jpeg"); ? ? ? ? $("#pic_img").attr("src",data) ? ? ? ? $('#new_pic').val($("#pic_img").attr("src")); ? ? ? ? $('#doc-modal-1').modal('close'); ? ? }); }); function rotateimgright() { ? ? $("#image").cropper('rotate', 90); } function rotateimgleft() { ? ? $("#image").cropper('rotate', -90); } function set_alert_info(content){ ? ? $("#alert_content").html(content); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)Ш綑诟S滑動效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了小程序?qū)Ш綑诟S滑動效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05JavaScript 事件流、事件處理程序及事件對象總結(jié)
JS與HTML之間的交互通過事件實(shí)現(xiàn),事件就是文檔或?yàn)g覽器窗口中發(fā)生的一些特定的交互瞬間,可以使用(或處理程序)來預(yù)定事件,以便事件發(fā)生時執(zhí)行相應(yīng)的代碼,本文將介紹JS事件相關(guān)的基礎(chǔ)知識,2017-04-04詳解小程序如何動態(tài)綁定點(diǎn)擊的執(zhí)行方法
這篇文章主要介紹了詳解小程序如何動態(tài)綁定點(diǎn)擊的執(zhí)行方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11js解決彈窗問題實(shí)現(xiàn)班級跳轉(zhuǎn)DIV示例
本文為大家介紹下js如何解決彈窗問題實(shí)現(xiàn)班級跳轉(zhuǎn)DIV,具體示例如下,感興趣的朋友可以參考下2014-01-01