在線剪切

圖片裁剪

<%--
  Created by IntelliJ IDEA.
  User: MSI-PC
  Date: 2019/4/19
  Time: 14:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基於cropper.js的圖片裁剪</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/css/cropper.min.css">
    <link rel="stylesheet" href="/css/ImgCropping.css">

</head>
<body style="background: #eee">

<button id="replaceImg" class="l-btn">更換圖片</button>
<div style="width: 320px;height: 320px;border: solid 1px #555;padding: 5px;margin-top: 10px">
    <img id="finalImg" src="" width="100%">
</div>
<!--圖片裁剪框 start-->
<div style="display: none" class="tailoring-container">
    <div class="black-cloth" onclick="closeTailor(this)"></div>
    <div class="tailoring-content">
        <div class="tailoring-content-one">
            <label title="上傳圖片" for="chooseImg" class="l-btn choose-btn">
                <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)">
                選擇圖片
            </label>
            <div class="close-tailoring"  onclick="closeTailor(this)">×</div>
        </div>
        <div class="tailoring-content-two">
            <div class="tailoring-box-parcel">
                <img id="tailoringImg">
            </div>
            <div class="preview-box-parcel">
                <p>圖片預覽:</p>
                <div class="square previewImg"></div>
                <div class="circular previewImg"></div>
            </div>
        </div>
        <div class="tailoring-content-three">
            <button class="l-btn cropper-reset-btn">復位</button>
            <button class="l-btn cropper-rotate-btn">旋轉</button>
            <button class="l-btn cropper-scaleX-btn">換向</button>
            <button class="l-btn sureCut" id="sureCut">肯定</button>
        </div>
    </div>
</div>
<!--圖片裁剪框 end-->
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="/js/cropper.min.js"></script>
<script>

    //彈出框水平垂直居中
    (window.onresize = function () {
        var win_height = $(window).height();
        var win_width = $(window).width();
        if (win_width <= 768){
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": 0
            });
        }else{
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": (win_width - $(".tailoring-content").outerWidth())/2
            });
        }
    })();

    //彈出圖片裁剪框
    $("#replaceImg").on("click",function () {
        $(".tailoring-container").toggle();
    });
    //圖像上傳
    function selectImg(file) {
        if (!file.files || !file.files[0]){
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            var replaceSrc = evt.target.result;
            //更換cropper的圖片
            $('#tailoringImg').cropper('replace', replaceSrc,false);//默認false,適應高度,不失真
        }
        reader.readAsDataURL(file.files[0]);
    }
    //cropper圖片裁剪
    $('#tailoringImg').cropper({
        aspectRatio: 1/1,//默認比例
        preview: '.previewImg',//預覽視圖
        guides: false,  //裁剪框的虛線(九宮格)
        autoCropArea: 0.5,  //0-1之間的數值,定義自動剪裁區域的大小,默認0.8
        movable: false, //是否容許移動圖片
        dragCrop: true,  //是否容許移除當前的剪裁框,並經過拖動來新建一個剪裁框區域
        movable: true,  //是否容許移動剪裁框
        resizable: true,  //是否容許改變裁剪框的大小
        zoomable: false,  //是否容許縮放圖片大小
        mouseWheelZoom: false,  //是否容許經過鼠標滾輪來縮放圖片
        touchDragZoom: true,  //是否容許經過觸摸移動來縮放圖片
        rotatable: true,  //是否容許旋轉圖片
        crop: function(e) {
            // 輸出結果數據裁剪圖像。
        }
    });
    //旋轉
    $(".cropper-rotate-btn").on("click",function () {
        $('#tailoringImg').cropper("rotate", 45);
    });
    //復位
    $(".cropper-reset-btn").on("click",function () {
        $('#tailoringImg').cropper("reset");
    });
    //換向
    var flagX = true;
    $(".cropper-scaleX-btn").on("click",function () {
        if(flagX){
            $('#tailoringImg').cropper("scaleX", -1);
            flagX = false;
        }else{
            $('#tailoringImg').cropper("scaleX", 1);
            flagX = true;
        }
        flagX != flagX;
    });
    //裁剪後的處理
    $("#sureCut").on("click",function () {
        if ($("#tailoringImg").attr("src") == null ){
            return false;
        }else{
            var cas = $('#tailoringImg').cropper('getCroppedCanvas');//獲取被裁剪後的canvas
            var base64url = cas.toDataURL('image/png'); //轉換爲base64地址形式
            $("#finalImg").prop("src",base64url);//顯示爲圖片的形式

            //關閉裁剪框
            closeTailor();
        }
    });
    //關閉裁剪框
    function closeTailor() {
        $(".tailoring-container").toggle();
    }
</script>

</body>
</html>
相關文章
相關標籤/搜索