Image upload and crop

Image upload and crop

There are several features about
1: achieve picture upload
2: Implement image preview
3: implement image cropping

Use the knowledge
file objects, <input type='file'/>, cropper.js,

Reference: https://fengyuanchen.github.io/cropperjs/

code show as below:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Cropper.js</title>
    <link rel="stylesheet" href="../css/cropper.css">
    <style>
        .container {
            margin: 20px auto;
            max-width: 640px;
        }
        
        img {
            max-width: 100%;
        }
        
        .cropper-view-box,
        .cropper-face {
            border-radius: 50%;
        }
        
        #image {
            width: auto;
            height: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <label>
            上传图片:<input type='file' id="upImg" />
        </label>

        <h1>裁剪图片</h1>
        <div id='chooseImg'>
        </div>
        <h3>裁剪的结果</h3>
        <p>
            <button type="button" id="button">Crop</button>
        </p>
        <div id="result"></div>
    </div>
    <script src="../js/cropper.js"></script>
    <script>
        var upImg = document.getElementById('upImg');
        //上传选择的文件
        upImg.addEventListener('change', function() {

            var objImage = '';
            objImage = upImg.files[0];
            var reader = new FileReader();
            reader.addEventListener('load', function() {
                var imgUrl = reader.result;
                var imgBox = document.getElementById('chooseImg');

                // 重新选择图片进行裁剪,如果裁剪的模块有内容,进行情况
                if (document.getElementById('image')) {
                    imgBox.innerHTML = '';
                }
                //实现图片预览
                imageObj = document.createElement('img');
                imageObj.setAttribute('id', 'image');
                imageObj.src = imgUrl;
                imgBox.appendChild(imageObj);

                //选择后的图片进行裁剪
                cropperEvent(imageObj);
            });
            if (objImage) {
                reader.readAsDataURL(objImage);
            }
        }, false);

        function getRoundedCanvas(sourceCanvas) {
            var canvas = document.createElement('canvas');
            var context = canvas.getContext('2d');
            var width = sourceCanvas.width;
            var height = sourceCanvas.height;

            canvas.width = width;
            canvas.height = height;
            context.imageSmoothingEnabled = true;
            context.drawImage(sourceCanvas, 0, 0, width, height);
            context.globalCompositeOperation = 'destination-in';
            context.beginPath();
            context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
            context.fill();
            return canvas;
        }

        function cropperEvent(image) {
            var button = document.getElementById('button');
            var result = document.getElementById('result');
            var croppable = false;
            var cropper = new Cropper(image, {
                aspectRatio: 1,
                viewMode: 1,
                ready: function() {
                    croppable = true;
                },
            });
            button.onclick = function() {
                var croppedCanvas;
                var roundedCanvas;
                var roundedImage;

                if (!croppable) {
                    return;
                }

                // Crop
                croppedCanvas = cropper.getCroppedCanvas();

                // Round
                roundedCanvas = getRoundedCanvas(croppedCanvas);

                // Show
                roundedImage = document.createElement('img');
                roundedImage.src = roundedCanvas.toDataURL();
                result.innerHTML = '';
                result.appendChild(roundedImage);
            };
        }
    </script>
</body>

</html>


</html>

Achieve results:
https://avatar.csdn.net/7/7/B/1_ralf_hx163com.jpg#pic_center

Published 11 original articles · won praise 1 · views 1164

Guess you like

Origin blog.csdn.net/u014403513/article/details/88426924