JS裁剪图片底部的水印

效果

 

源码 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Popup</title>
    <style>
        #popup {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.8);
        }
        #popup img {
            margin: auto;
            display: block;
            max-width: 90%;
            max-height: 90%;
        }
        #popup:target {
            display: block;
        }
        #popupClose {
            position: absolute;
            top: 20px;
            right: 35px;
            color: #fff;
            font-size: 40px;
            font-weight: bold;
            text-decoration: none;
        }
        #popupClose:hover,
        #popupClose:focus {
            color: #bbb;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <img id="originalImage" src="" alt="Original Image" style="display:none;">
    <img id="croppedImage" src="" alt="Cropped Image" onclick="showPopup(this.src);">

    <div id="popup">
        <a href="#" id="popupClose">&times;</a>
        <img id="popupImg" src="" alt="Popup Image">
    </div>

    <script>
        function cropImage(url, cropHeight, callback) {
            const img = new Image();
            img.crossOrigin = 'Anonymous'; // Enable CORS if the image is hosted on a different domain
            img.onload = function() {
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');

                const cropWidth = img.width;
                const effectiveCropHeight = img.height - cropHeight; // Fixed variable usage

                canvas.width = cropWidth;
                canvas.height = effectiveCropHeight;

                ctx.drawImage(img, 0, 0, cropWidth, effectiveCropHeight, 0, 0, cropWidth, effectiveCropHeight);

                const croppedImageDataUrl = canvas.toDataURL();
                callback(croppedImageDataUrl);
            };
            img.onerror = function() {
                console.error('Failed to load image.');
            };
            img.src = url;
        }

        function showPopup(src) {
            const popupImg = document.getElementById('popupImg');
            popupImg.src = src;
            window.location.hash = 'popup';
        }

        // Example usage
        const imageUrl = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage109.360doc.com%2FDownloadImg%2F2021%2F08%2F0420%2F227651986_4_20210804085322222&refer=http%3A%2F%2Fimage109.360doc.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1719538441&t=a5b93bb2e329ccf76156ac82bd67e1ec';
        const watermarkHeight = 70; // Adjust this height to match the watermark height

        cropImage(imageUrl, watermarkHeight, function(croppedImageUrl) {
            document.getElementById('originalImage').src = imageUrl;
            document.getElementById('originalImage').style.display = 'block';
            document.getElementById('croppedImage').src = croppedImageUrl;
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值