使用 HTML + JavaScript 实现图片裁剪上传功能

html图片的上传 用于存储上传成功后的图片 URL。修改还剩一些,例如,进行完这些上传的图片不显示。我们需要给隐藏于设置值,这样修改的图片才能显示。以上便可以实现图片的上传和修改。将原先imgurl部分修改为。以上便可以实现图片的上传。在这段代码中,隐藏域( 阅读详情

本文将详细介绍一个基于 HTML 和 JavaScript 实现的图片裁剪上传功能。该功能支持文件选择、拖放上传、图片预览、区域选择、裁剪操作以及图片下载等功能,适用于需要进行图片处理的 Web 应用场景。

效果演示

image-20250602112435691

image-20250602112543017

项目概述

本项目主要包含以下核心功能:

  • 文件选择与拖放上传
  • 裁剪框拖动与调整大小
  • 图片裁剪
  • 图片上传(模拟)与下载

页面结构

上传区域

实现友好的文件选择体验,并支持拖放上传。

<div class="upload-section">
    <input type="file" id="fileInput" accept="image/*">
    <label for="fileInput" class="file-label">选择图片</label>
    <p>或拖放图片到此处</p>
</div>
预览区域

分为两个部分,左侧显示原始图片和裁剪框,右侧展示裁剪后的结果。

<div class="preview-section">
    <div class="image-container">
        <div>
            <h3>原始图片</h3>
            <img id="originalImage" style="max-width: 100%; display: none;">
            <div class="cropper-container" id="cropperContainer" style="display: none;">
                <canvas id="sourceCanvas"></canvas>
                <div class="selection-box" id="selectionBox">
                    <div class="resize-handle"></div>
                </div>
            </div>
            <p class="instruction">拖动选择框可移动位置,拖动右下角可调整大小</p>
        </div>

        <div>
            <h3>裁剪结果</h3>
            <canvas id="croppedCanvas" style="display: none;"></canvas>
            <p id="noCropMessage">请先选择图片并设置裁剪区域</p>
        </div>
    </div>
</div>
操作按钮

提供“裁剪”、“上传”、“下载”和“重置”按钮,方便用户进行各种操作。

<div class="controls">
    <button id="cropBtn" disabled>裁剪图片</button>
    <button id="uploadBtn" disabled>上传图片</button>
    <button id="downloadBtn" disabled>下载图片</button>
    <button id="resetBtn">重置</button>
</div>

核心功能实现

定义基础变量

获取DOM元素

const fileInput = document.getElementById('fileInput');
const originalImage = document.getElementById('originalImage');
const sourceCanvas = document.getElementById('sourceCanvas');
const croppedCanvas = document.getElementById('croppedCanvas');
const cropperContainer = document.getElementById('cropperContainer');
const selectionBox = document.getElementById('selectionBox');
const cropBtn = document.getElementById('cropBtn');
const uploadBtn = document.getElementById('uploadBtn');
const resetBtn = document.getElementById('resetBtn');
const noCropMessage = document.getElementById('noCropMessage');
const downloadBtn = document.getElementById('downloadBtn');

定义全局变量

let isDragging = false;
let isResizing = false;
let startX, startY;
let selection = {
    x: 0,
    y: 0,
    width: 0,
    height: 0,
    startX: 0,
    startY: 0,
    startWidth: 0,
    startHeight: 0
};
let imageRatio = 1;
文件选择

使用 FileReader API 将选中的图片读取为 Data URL 并显示在页面上。

function handleFileSelect(event) {
    const file = event.target.files[0];
    if (!file || !file.type.match('image.*')) {
        alert('请选择有效的图片文件');
        return;
    }

    const reader = new FileReader();
    reader.onload = function(e) {
        originalImage.src = e.target.result;
        originalImage.onload = function() {
            initCropper();
        };
    };
    reader.readAsDataURL(file);
}
拖放上传

通过监听 dragover、dragleave 和 drop 事件实现拖放上传功能。

const uploadSection = document.querySelector('.upload-section');
uploadSection.addEventListener('dragover', (e) => {
    e.preventDefault();
    uploadSection.style.borderColor = '#4CAF50';
});

uploadSection.addEventListener('dragleave', () => {
    uploadSection.style.borderColor = '#ccc';
});

uploadSection.addEventListener('drop', (e) => {
    e.preventDefault();
    uploadSection.style.borderColor = '#ccc';

    if (e.dataTransfer.files.length) {
        fileInput.files = e.dataTransfer.files;
        handleFileSelect({ target: fileInput });
    }
});
图片预览与裁剪框初始化

在图片加载完成后,绘制到 canvas 上,并根据图片尺寸调整画布大小。初始化一个固定比例的裁剪框,居中显示在画布上。

function initCropper() {
    // 显示原始图片和裁剪区域
    originalImage.style.display = 'none';
    cropperContainer.style.display = 'inline-block';

    // 设置canvas尺寸
    const maxWidth = 500;
    imageRatio = originalImage.naturalWidth / originalImage.naturalHeight;

    let canvasWidth, canvasHeight;
    if (originalImage.naturalWidth > maxWidth) {
        canvasWidth = maxWidth;
        canvasHeight = maxWidth / imageRatio;
    } else {
        canvasWidth = originalImage.naturalWidth;
        canvasHeight = originalImage.naturalHeight;
    }

    sourceCanvas.width = canvasWidth;
    sourceCanvas.height = canvasHeight;

    // 绘制图片到canvas
    const ctx = sourceCanvas.getContext('2d');
    ctx.drawImage(originalImage, 0, 0, canvasWidth, canvasHeight);

    // 初始化选择框 (1:1比例)
    const boxSize = Math.min(canvasWidth, canvasHeight) * 0.6;
    selection = {
        x: Math.max(0, Math.min((canvasWidth - boxSize) / 2, canvasWidth - boxSize)), // 确保初始位置在画布范围内
        y: Math.max(0, Math.min((canvasHeight - boxSize) / 2, canvasHeight - boxSize)), // 确保初始位置在画布范围内
        width: boxSize,
        height: boxSize,
        startX: 0,
        startY: 0,
        startWidth: 0,
        startHeight: 0
    };

    updateSelectionBox();
    cropBtn.disabled = false;
}
裁剪框拖动与调整大小

通过监听鼠标事件(mousedown、mousemove、mouseup)实现裁剪框的拖动和调整大小功能。确保裁剪框始终位于画布范围内,并保持指定的比例。

selectionBox.addEventListener('mousedown', startDrag);
document.addEventListener('mousemove', handleDrag);
document.addEventListener('mouseup', endDrag);
const resizeHandle = document.querySelector('.resize-handle');
resizeHandle.addEventListener('mousedown', (e) => {
    e.stopPropagation();
    startResize(e);
});
function startDrag(e) {
    if (e.target.classList.contains('resize-handle')) {
        return; // 忽略调整大小手柄的点击
    }
    isDragging = true;
    startX = e.clientX;
    startY = e.clientY;
    // 存储初始位置
    selection.startX = selection.x;
    selection.startY = selection.y;
    e.preventDefault();
}
function startResize(e) {
    isResizing = true;
    startX = e.clientX;
    startY = e.clientY;

    // 存储初始尺寸和位置
    selection.startX = selection.x;
    selection.startY = selection.y;
    selection.startWidth = selection.width;
    selection.startHeight = selection.height;

    e.preventDefault();
}
function handleDrag(e) {
    if (!isDragging && !isResizing) return;
    const dx = e.clientX - startX;
    const dy = e.clientY - startY;
    if (isDragging) {
        // 处理移动选择框
        let newX = selection.startX + dx;
        let newY = selection.startY + dy;
        // 限制在canvas范围内
        newX = Math.max(0, Math.min(newX, sourceCanvas.width - selection.width));
        newY = Math.max(0, Math.min(newY, sourceCanvas.height - selection.height));

        selection.x = newX;
        selection.y = newY;
    } else if (isResizing) {
        // 处理调整大小 (保持1:1比例)
        let newSize = Math.max(10, Math.min(
            selection.startWidth + (dx + dy) / 2, // 取dx和dy的平均值使调整更平滑
            Math.min(
                sourceCanvas.width - selection.startX,
                sourceCanvas.height - selection.startY
            )
        ));
        // 应用新尺寸 (保持正方形)
        selection.width = newSize;
        selection.height = newSize;
        // 确保裁剪框不会超出画布范围
        if (selection.x + selection.width > sourceCanvas.width) {
            selection.x = sourceCanvas.width - selection.width;
        }
        if (selection.y + selection.height > sourceCanvas.height) {
            selection.y = sourceCanvas.height - selection.height;
        }
    }
    updateSelectionBox();
}
function endDrag() {
    isDragging = false;
    isResizing = false;
}
图片裁剪与结果展示

使用 drawImage 方法从源画布中裁剪出指定区域,并将其绘制到目标画布上。

function cropImage() {
    const ctx = croppedCanvas.getContext('2d');
    // 设置裁剪后canvas的尺寸 (1:1)
    croppedCanvas.width = selection.width;
    croppedCanvas.height = selection.height;
    // 执行裁剪
    ctx.drawImage(
        sourceCanvas,
        selection.x, selection.y, selection.width, selection.height, // 源图像裁剪区域
        0, 0, selection.width, selection.height                     // 目标canvas绘制区域
    );
    // 显示裁剪结果
    croppedCanvas.style.display = 'block';
    noCropMessage.style.display = 'none';
    uploadBtn.disabled = false;
    downloadBtn.disabled = false;
}
图片上传与下载

提供模拟的上传功能,使用 toBlob 方法获取裁剪后的图片数据。支持将裁剪后的图片下载为 JPEG 格式的文件。

function uploadImage() {
    // 在实际应用中,这里应该将图片数据发送到服务器
    croppedCanvas.toBlob((blob) => {
        // 创建FormData对象并添加图片
        const formData = new FormData();
        formData.append('croppedImage', blob, 'cropped-image.jpg');

        // 模拟上传延迟
        setTimeout(() => {
            alert('图片上传成功!(模拟)');
            console.log('上传的图片数据:', blob);
            // 在实际应用中,你可能需要处理服务器响应
        }, 1000);
    }, 'image/jpeg', 0.9);
}
function downloadImage() {
    if (!croppedCanvas.width || !croppedCanvas.height) {
        alert('请先裁剪图片');
        return;
    }
    // 创建一个临时的a标签用于触发下载
    const link = document.createElement('a');
    link.href = croppedCanvas.toDataURL('image/jpeg', 0.9);
    link.download = 'cropped-image.jpg'; // 设置下载文件名
    link.click();
}

扩展建议

  • 支持多种裁剪比例:可以扩展代码以支持不同的裁剪比例(如 4:3、16:9),并通过 UI 控件让用户选择。
  • 图像缩放功能:添加对图片缩放的支持,允许用户放大或缩小图片以便更精确地选择裁剪区域。
  • 服务器端集成:实际应用中,应将裁剪后的图片发送到服务器进行存储和处理,可以通过请求实现。

完整代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片裁剪上传</title>
    <style>
        body {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        h1 {
            text-align: center;
        }
        .upload-section {
            padding: 20px;
            text-align: center;
            border-radius: 5px;
            background: #f8f9fa;
            border: 2px dashed #dee2e6;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        .upload-section:hover {
            border-color: #4CAF50;
            background: rgba(76, 175, 80, 0.05);
        }
        .file-label {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            border-radius: 25px;
            padding: 12px 24px;
            transition: transform 0.2s;
        }

        .file-label:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3);
        }

        .preview-section {
            display: flex;
            flex-direction: column;
            gap: 20px;
            background: #ffffff;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        canvas {
            max-width: 100%;
            border: 1px solid #eee;
            display: block;
        }
        .cropper-container {
            position: relative;
            display: inline-block;
        }
        .selection-box {
            position: absolute;
            border: 2px dashed #000;
            background: rgba(255, 255, 255, 0.3);
            cursor: move;
            box-sizing: border-box;
        }
        .resize-handle {
            position: absolute;
            width: 10px;
            height: 10px;
            background: #fff;
            border: 2px solid #000;
            border-radius: 50%;
            bottom: -5px;
            right: -5px;
            cursor: se-resize;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        button {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            border-radius: 25px;
            padding: 12px 24px;
            font-weight: 500;
            box-shadow: 0 4px 6px rgba(76, 175, 80, 0.1);
            transition: all 0.3s ease;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(76, 175, 80, 0.2);
        }

        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        input[type="file"] {
            display: none;
        }
        .file-label {
            display: inline-block;
            padding: 10px 15px;
            background-color: #f0f0f0;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 10px;
        }
        .controls {
            display: flex;
            gap: 10px;
            margin-top: 10px;
            justify-content: center;
        }
        .instruction {
            font-size: 14px;
            color: #666;
            margin-top: 10px;
        }
        .image-container {
            display: flex;
            gap: 20px;
        }
        .image-container > div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>图片裁剪上传</h1>

    <div class="upload-section">
        <input type="file" id="fileInput" accept="image/*">
        <label for="fileInput" class="file-label">选择图片</label>
        <p>或拖放图片到此处</p>
    </div>

    <div class="preview-section">
        <div class="image-container">
            <div>
                <h3>原始图片</h3>
                <img id="originalImage" style="max-width: 100%; display: none;">
                <div class="cropper-container" id="cropperContainer" style="display: none;">
                    <canvas id="sourceCanvas"></canvas>
                    <div class="selection-box" id="selectionBox">
                        <div class="resize-handle"></div>
                    </div>
                </div>
                <p class="instruction">拖动选择框可移动位置,拖动右下角可调整大小</p>
            </div>

            <div>
                <h3>裁剪结果</h3>
                <canvas id="croppedCanvas" style="display: none;"></canvas>
                <p id="noCropMessage">请先选择图片并设置裁剪区域</p>
            </div>
        </div>
    </div>

    <div class="controls">
        <button id="cropBtn" disabled>裁剪图片</button>
        <button id="uploadBtn" disabled>上传图片</button>
        <button id="downloadBtn" disabled>下载图片</button>
        <button id="resetBtn">重置</button>
    </div>
</div>

<script>
    // 获取DOM元素
    const fileInput = document.getElementById('fileInput');
    const originalImage = document.getElementById('originalImage');
    const sourceCanvas = document.getElementById('sourceCanvas');
    const croppedCanvas = document.getElementById('croppedCanvas');
    const cropperContainer = document.getElementById('cropperContainer');
    const selectionBox = document.getElementById('selectionBox');
    const cropBtn = document.getElementById('cropBtn');
    const uploadBtn = document.getElementById('uploadBtn');
    const resetBtn = document.getElementById('resetBtn');
    const noCropMessage = document.getElementById('noCropMessage');
    const downloadBtn = document.getElementById('downloadBtn');

    // 全局变量
    let isDragging = false;
    let isResizing = false;
    let startX, startY;
    let selection = {
        x: 0,
        y: 0,
        width: 0,
        height: 0,
        startX: 0,
        startY: 0,
        startWidth: 0,
        startHeight: 0
    };
    let imageRatio = 1;

    // 监听文件选择
    fileInput.addEventListener('change', handleFileSelect);

    // 拖放功能
    const uploadSection = document.querySelector('.upload-section');
    uploadSection.addEventListener('dragover', (e) => {
        e.preventDefault();
        uploadSection.style.borderColor = '#4CAF50';
    });

    uploadSection.addEventListener('dragleave', () => {
        uploadSection.style.borderColor = '#ccc';
    });

    uploadSection.addEventListener('drop', (e) => {
        e.preventDefault();
        uploadSection.style.borderColor = '#ccc';

        if (e.dataTransfer.files.length) {
            fileInput.files = e.dataTransfer.files;
            handleFileSelect({ target: fileInput });
        }
    });

    // 选择框鼠标事件
    selectionBox.addEventListener('mousedown', startDrag);
    document.addEventListener('mousemove', handleDrag);
    document.addEventListener('mouseup', endDrag);

    // 调整大小手柄事件
    const resizeHandle = document.querySelector('.resize-handle');
    resizeHandle.addEventListener('mousedown', (e) => {
        e.stopPropagation();
        startResize(e);
    });

    // 下载图片
    function downloadImage() {
        if (!croppedCanvas.width || !croppedCanvas.height) {
            alert('请先裁剪图片');
            return;
        }
        // 创建一个临时的a标签用于触发下载
        const link = document.createElement('a');
        link.href = croppedCanvas.toDataURL('image/jpeg', 0.9);
        link.download = 'cropped-image.jpg'; // 设置下载文件名
        link.click();
    }

    // 按钮事件
    cropBtn.addEventListener('click', cropImage);
    uploadBtn.addEventListener('click', uploadImage);
    resetBtn.addEventListener('click', resetAll);
    downloadBtn.addEventListener('click', downloadImage);

    // 处理文件选择
    function handleFileSelect(event) {
        const file = event.target.files[0];
        if (!file || !file.type.match('image.*')) {
            alert('请选择有效的图片文件');
            return;
        }

        const reader = new FileReader();
        reader.onload = function(e) {
            originalImage.src = e.target.result;
            originalImage.onload = function() {
                initCropper();
            };
        };
        reader.readAsDataURL(file);
    }

    // 初始化裁剪器
    function initCropper() {
        // 显示原始图片和裁剪区域
        originalImage.style.display = 'none';
        cropperContainer.style.display = 'inline-block';

        // 设置canvas尺寸
        const maxWidth = 500;
        imageRatio = originalImage.naturalWidth / originalImage.naturalHeight;

        let canvasWidth, canvasHeight;
        if (originalImage.naturalWidth > maxWidth) {
            canvasWidth = maxWidth;
            canvasHeight = maxWidth / imageRatio;
        } else {
            canvasWidth = originalImage.naturalWidth;
            canvasHeight = originalImage.naturalHeight;
        }

        sourceCanvas.width = canvasWidth;
        sourceCanvas.height = canvasHeight;

        // 绘制图片到canvas
        const ctx = sourceCanvas.getContext('2d');
        ctx.drawImage(originalImage, 0, 0, canvasWidth, canvasHeight);

        // 初始化选择框 (1:1比例)
        const boxSize = Math.min(canvasWidth, canvasHeight) * 0.6;
        selection = {
            x: Math.max(0, Math.min((canvasWidth - boxSize) / 2, canvasWidth - boxSize)), // 确保初始位置在画布范围内
            y: Math.max(0, Math.min((canvasHeight - boxSize) / 2, canvasHeight - boxSize)), // 确保初始位置在画布范围内
            width: boxSize,
            height: boxSize,
            startX: 0,
            startY: 0,
            startWidth: 0,
            startHeight: 0
        };

        updateSelectionBox();
        cropBtn.disabled = false;
    }

    // 更新选择框位置和尺寸
    function updateSelectionBox() {
        selectionBox.style.left = `${selection.x}px`;
        selectionBox.style.top = `${selection.y}px`;
        selectionBox.style.width = `${selection.width}px`;
        selectionBox.style.height = `${selection.height}px`;
    }

    // 开始拖动
    function startDrag(e) {
        if (e.target.classList.contains('resize-handle')) {
            return; // 忽略调整大小手柄的点击
        }

        isDragging = true;
        startX = e.clientX;
        startY = e.clientY;

        // 存储初始位置
        selection.startX = selection.x;
        selection.startY = selection.y;

        e.preventDefault();
    }

    // 处理拖动
    function handleDrag(e) {
        if (!isDragging && !isResizing) return;

        const dx = e.clientX - startX;
        const dy = e.clientY - startY;

        if (isDragging) {
            // 处理移动选择框
            let newX = selection.startX + dx;
            let newY = selection.startY + dy;

            // 限制在canvas范围内
            newX = Math.max(0, Math.min(newX, sourceCanvas.width - selection.width));
            newY = Math.max(0, Math.min(newY, sourceCanvas.height - selection.height));

            selection.x = newX;
            selection.y = newY;
        } else if (isResizing) {
            // 处理调整大小 (保持1:1比例)
            let newSize = Math.max(10, Math.min(
                selection.startWidth + (dx + dy) / 2, // 取dx和dy的平均值使调整更平滑
                Math.min(
                    sourceCanvas.width - selection.startX,
                    sourceCanvas.height - selection.startY
                )
            ));

            // 应用新尺寸 (保持正方形)
            selection.width = newSize;
            selection.height = newSize;

            // 确保裁剪框不会超出画布范围
            if (selection.x + selection.width > sourceCanvas.width) {
                selection.x = sourceCanvas.width - selection.width;
            }
            if (selection.y + selection.height > sourceCanvas.height) {
                selection.y = sourceCanvas.height - selection.height;
            }
        }

        updateSelectionBox();
    }

    // 结束拖动或调整大小
    function endDrag() {
        isDragging = false;
        isResizing = false;
    }

    // 开始调整大小
    function startResize(e) {
        isResizing = true;
        startX = e.clientX;
        startY = e.clientY;

        // 存储初始尺寸和位置
        selection.startX = selection.x;
        selection.startY = selection.y;
        selection.startWidth = selection.width;
        selection.startHeight = selection.height;

        e.preventDefault();
    }

    // 裁剪图片
    function cropImage() {
        const ctx = croppedCanvas.getContext('2d');
        // 设置裁剪后canvas的尺寸 (1:1)
        croppedCanvas.width = selection.width;
        croppedCanvas.height = selection.height;
        // 执行裁剪
        ctx.drawImage(
            sourceCanvas,
            selection.x, selection.y, selection.width, selection.height, // 源图像裁剪区域
            0, 0, selection.width, selection.height                     // 目标canvas绘制区域
        );
        // 显示裁剪结果
        croppedCanvas.style.display = 'block';
        noCropMessage.style.display = 'none';
        uploadBtn.disabled = false;
        downloadBtn.disabled = false;
    }

    // 上传图片
    function uploadImage() {
        // 在实际应用中,这里应该将图片数据发送到服务器
        croppedCanvas.toBlob((blob) => {
            // 创建FormData对象并添加图片
            const formData = new FormData();
            formData.append('croppedImage', blob, 'cropped-image.jpg');

            // 模拟上传延迟
            setTimeout(() => {
                alert('图片上传成功!(模拟)');
                console.log('上传的图片数据:', blob);
                // 在实际应用中,你可能需要处理服务器响应
            }, 1000);
        }, 'image/jpeg', 0.9);
    }

    // 重置所有
    function resetAll() {
        // 隐藏元素
        cropperContainer.style.display = 'none';
        croppedCanvas.style.display = 'none';
        noCropMessage.style.display = 'block';
        originalImage.style.display = 'none';

        // 重置按钮状态
        cropBtn.disabled = true;
        uploadBtn.disabled = true;
        downloadBtn.disabled = true;

        // 清除文件输入
        fileInput.value = '';

        // 清除画布
        const ctx = sourceCanvas.getContext('2d');
        ctx.clearRect(0, 0, sourceCanvas.width, sourceCanvas.height);

        const croppedCtx = croppedCanvas.getContext('2d');
        croppedCtx.clearRect(0, 0, croppedCanvas.width, croppedCanvas.height);
    }
</script>
</body>
</html>
HTML5 实现本地图片裁剪 HTML5 实现本地图片裁剪 文章目录HTML5 实现本地图片裁剪1.知识点1.HTML 结构与 CSS 样式2.初始化3 实现 handleFiles,获取文件,读取文件并生成 url4.实现 paintImage 方法5 实现 cutImage 方法6 编写 drag 方法7 保存截图8 完整 js 代码 这次小的案例是利用 HTML5 的 canvas 技术,结合 HTML5 的 File API 来实现图片的本地裁剪,从一个大图片中选取裁剪区域, 1.知识点 canvas JavaScript 阅读详情

相关推荐

使用WebUploader和Cropper.js进行图片裁剪和上传

WebUploader是一个基于HTML5的文件上传组件,它提供了丰富的文件上传功能,并且支持图片裁剪。综上所述,通过使用WebUploader和Cropper.js,我们可以方便地实现图片裁剪和上传的功能。这些工具提供了丰富的功能和灵活的配置选项,能够满足各种图片处理的需求。然后,我们需要编写JavaScript代码来初始化WebUploader和Cropper.js,并实现图片裁剪和上传的功能。需要注意的是,上述代码中的配置参数和上传逻辑需要根据实际情况进行相应的调整,以适应具体的项目需求。

SvqiAnaconda的博客 396

关于HTML中上传图片

关于HTML中上传图片 值得注意的是:当一个表单里面包含这个上传元素的时候,表单的enctype必须指定为multipart/form-data,method必须指定为post,浏览器才会认识并正确执行。但是还有一点,浏览器只允许用户点击&lt;input type="file"&gt;来选择本地文件,用JavaScript对&lt;input type="file"&gt;的value赋值是没...

喜欢做一些demo,和分享遇到的 BUG 4万+

前端开发:实现图片上传和裁剪功能

修改后的代码中,我们添加了Cropper.js库的引用,并在文件选择后创建了一个裁剪实例。我们设置了裁剪框的宽高比例为1:1,并将默认裁剪框的大小设为图像的一半。在裁剪完成后,可以在crop回调函数中获取裁剪框的位置和大小信息。同时,用户还可以使用裁剪框来选择感兴趣的部分进行裁剪,并在裁剪完成后获取裁剪框的位置和大小信息。以上代码创建了一个简单的HTML页面,其中包含一个文件上传输入框和一个用于显示预览图像的区域。在前端开发中,图片上传和裁剪是常见的需求之一。// 设置默认裁剪框的大小为图像的一半。

ScriptWELL的博客 328

js实现对上传图片进行压缩并且预览

js实现对上传图片的路径转成base64编码,并且对图片进行压缩,实现预览功能 需要先引入jquery:  1、html如下 2、css如下 #previewImg{ background-color: black; width: 400px;

感恩、奋进、自信 5万+

为什么PCB设计中的50Ω阻抗如此重要?从USB到以太网的实战解析

本文深入解析了PCB设计中50Ω特征阻抗的核心重要性及其工程权衡起源。文章从传输线理论出发,阐释了阻抗匹配对防止信号反射、确保信号完整性的关键作用,并对比了USB、以太网等不同应用场景的阻抗标准。同时,文章结合实际案例,剖析了在两层板等简单设计中实现精准阻抗控制的难点与实战解决方案。

weixin_29268385的博客 298

国产麒麟系统下Kettle安装避坑指南:解决aarch64架构不兼容问题

本文针对在国产麒麟系统上安装Kettle时遇到的aarch64架构不兼容问题,提供了详细的解决方案。通过剖析错误根源,指导用户获取并放置正确的SWT库文件,并修改启动脚本,成功解决“平台不支持”的报错,确保ETL工具在ARM架构的国产化环境中顺利运行。

weixin_29214691的博客 279

H5选择本地图片裁剪及上传功能

实现H5选择本地图片裁剪及上传功能。裁剪功能使用cropper.js。

蒙双眼看世界 1505

o型圈沟槽设计_O型密封圈——详细选型过程及步骤

一、密封圈常用材料二、密封圈常用密封形式密封圈分为活塞密封,活塞杆密封,端面密封,旋转密封。三、O型密封圈的选型规则O型密封圈的尺寸图径向安装O型圈选型(静态和动态运动)活塞杆密封选择适当规格的O形圈时,应该保证其外径(d1+2*d2)至少等于或者大于沟槽外径d6;当选择的O形圈直径d1大于250 mm时, O形圈外径将不能超过沟槽外径的3%;当选择的O形圈直径d1小于250mm时, O形圈外径将...

weixin_39856630的博客 2万+

简单Cropper图片裁剪的使用

官方地址 GitHub 简单示例 HTML <!-- 文件 --> <input type="file" id="imgFile" onchange="ImgFileChange()" /> <!-- 裁剪图片的弹出框 --> <div id="l-dialog"> <!-- 外层div的所用,是用于限制一个裁剪的可视范围 --...

荒唌Blog 455

App爬虫实战篇-以华为真机手机爬取集换社的app为例

在开始学习这篇文章之前,建议你先按照之前2篇文章(和),配置必要的环境,才可以继续完成本章节内容。

youbingchen的博客 781

高性能算力中心 — RDMA — Overview

目录 文章目录目录RDMA基于以太网的 RoCE/RDMA 计算优化NVMeNVMe Over Fabrics基于以太网的 NVMe 存储优化 RDMA RDMA(Remote DMA,远程直接内存访问)是一种新的内存访问技术,RDMA 让计算机可以直接存取其他计算机的内存,而不需要经过 CPU 耗时的处理。RDMA 将数据从一个系统快速移动到远程系统存储器中,而不对操作系统造成任何影响。RDMA 技术的原理及其与 TCP/IP 架构的对比如下图所示。 InfiniBand(IB):从一开始就支持 R

烟云的计算 2368

图片裁剪上传

样式基于bootstrap,前端代码:&lt;div class="panel panel-default panel-col lesson-manage-panel"&gt;        &lt;div class="panel-heading"&gt;            课程图片        &lt;/div&gt;        &lt;div class="panel-body&quot

qq_34713980的博客 171

Oracle EBS FA 如何打开关闭的资产会计期间?

用户“运行折旧”,误勾选为“关闭期间”,还有一部分资产还需要操作报废和调整,希望后台打开关闭的资产会计期。

陈显平的专栏-Oracle EBS 1033

图片裁剪功能模板简单实用 复制粘贴即可使用 11

在 head 中导入 cropper.css 样式表: <link rel="stylesheet" href="http://www.escook.cn:8086/assets/lib/cropper/cropper.css" /> 在 body 的结束标签之前,按顺序导入如下的 js 脚本 <script src="http://www.escook.cn:8086/assets/lib/jquery.js"></script> <script src="h.

cxy9999999的博客 614

使用 MATLAB/Simulink 构建一个 基于神经网络控制的单相并网逆变器系统

单相并网逆变器广泛应用于户用光伏小型储能和电动汽车V2G等场景。其核心控制目标是实现高质量的并网电流,即电流与电网电压同频同相(单位功率因数),且总谐波畸变率 (THD)极低。非线性因素:电力电子器件的开关死区非线性电感寄生参数。参数不确定性:滤波器元件(L, C)的老化温漂。电网扰动:电压谐波频率波动不平衡。模型依赖性:PI、PR等控制依赖于精确的系统模型,模型失配导致性能下降。动态响应:在光照突变(光伏)或负载突变(储能)下,需快速、无超调地响应。

xiaoheshang_123的博客 275

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

基于cropper图片裁剪,兼容PC端与移动端

所需的资源文件 <link rel="stylesheet" href="https://cdn.bootcss.com/cropper/4.0.0-beta/cropper.css"> <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> <script src=...

weixin_30505751的博客 251

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

直播源码开发,实现相册的上传和缩放裁剪

以上就是直播源码开发,实现相册的上传和缩放裁剪, 更多内容欢迎关注之后的文章。直播源码开发,实现相册的上传和缩放裁剪。通过ajax 即可上传给后台。

云豹网络科技 209

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

cropper.js图片裁剪 实战使用

https://www.cnblogs.com/GoTing/p/7536648.html https://www.cnblogs.com/GoTing/p/11363494.html 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ..

tongluren381的博客 494

H5图片裁剪-压缩-上传-神奇的Croppie.js

Croppie.js之图片裁剪压缩上传 h5图片裁剪, 压缩, 上传, 预览是常见功能, 幸运的是我们有cropp.js这款利器. 1. style <link rel="Stylesheet" href="https://cdn.bootcss.com/croppie/2.6.2/croppie.css" /> &l...

weixin_34194551的博客 574

html5,js插件实现手机端头像剪切上传

思路:先打开相册,选取图片,在剪切图片,转化为base64格式,然后上传到七牛存储,返回url,再传给后端,整个流程就是这样。用的是angular框架,图像插件用到imagecropper.js,废话不多说,直接上代码。 效果: 用到的插件 <!DOCTYPE html> <html lang="en" ng-app=""> <h...

weixin_30325071的博客 585

移动端照片上传、头像裁剪完整功能,兼容iphone,android (一)

第一步:照片上传 <input class="js_upFile" type="file" name="cover" accept="image/*" capture="camera" multiple/> // 获取浏览器的userAgent var agent=navigator.userAgent.toLowerCase(); var isIos=(ag...

weixin_30736301的博客 208

ajaxfileuploader php,thinkphp ajaxfileupload 异步上传图片

thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。引入核心文件下载https://github.com/carlcarl/A...HTML下面首先在html页面引入相关js资源图片上传接下来在body中创建相关div封面图片:解释一下:其中upd_file(this,'image_file')不可缺少其中隐藏的input 是用于上传成功后赋值图...

weixin_42510271的博客 152

编辑头像功能

html: <div class="modal fade" id="userImgModal" role="dialog" aria-labelledby="gridSystemModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-cont...

dexia7362的博客 184

html5点击修改头像上传图片裁剪代码

html5点击修改头像上传图片裁剪代码 特效描述:html5 点击修改 头像上传 图片裁剪代码,html5点击头像弹出修改头像窗口,上传图片后可设置放大缩小,拖动,翻转自定义修改头像,进行图片裁剪效果代码。 效果图如下 代码结构 1. 引入CSS <link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/font-awesome/4.6.0/css/font-awesome.min.css"> <link

郭靖宇的个人博客 3001

base64支持拖拽剪切上传

前 &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable

gaoduicai的博客 286
上一篇: 使用 HTML + JavaScript 实现一个日历任务管理系统
下一篇: 使用 HTML + JavaScript 实现文章逐句高亮朗读功能
技术小丁
博客等级 码龄2年 173粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值