当前位置:首页 > JavaScript

js实现缩略图

2026-01-30 12:16:11JavaScript

使用 Canvas 实现缩略图

通过 Canvas 的 drawImage 方法缩放图片并生成缩略图。将原始图片绘制到缩小后的 Canvas 上,再转换为 Base64 或 Blob 数据。

function createThumbnail(file, maxWidth, maxHeight) {
  return new Promise((resolve) => {
    const img = new Image();
    img.onload = () => {
      const canvas = document.createElement('canvas');
      let width = img.width;
      let height = img.height;

      // 计算缩放比例
      if (width > maxWidth) {
        height = Math.round((height * maxWidth) / width);
        width = maxWidth;
      }
      if (height > maxHeight) {
        width = Math.round((width * maxHeight) / height);
        height = maxHeight;
      }

      // 绘制缩略图
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext('2d');
      ctx.drawImage(img, 0, 0, width, height);

      // 转换为 Blob
      canvas.toBlob(resolve, 'image/jpeg', 0.7);
    };
    img.src = URL.createObjectURL(file);
  });
}

使用第三方库(如 sharp.js)

在 Node.js 环境中,可使用 sharp 库高效处理图片缩略图。需先安装库:npm install sharp

const sharp = require('sharp');

async function generateThumbnail(inputPath, outputPath, width, height) {
  await sharp(inputPath)
    .resize(width, height, { fit: 'inside' })
    .toFile(outputPath);
}

纯前端缩略图预览

通过 URL.createObjectURL 实现上传前的本地预览,适合不要求高质量缩略图的场景。

function previewThumbnail(file, targetElementId) {
  const reader = new FileReader();
  reader.onload = (e) => {
    const img = document.createElement('img');
    img.src = e.target.result;
    img.style.maxWidth = '200px';
    document.getElementById(targetElementId).appendChild(img);
  };
  reader.readAsDataURL(file);
}

响应式缩略图(CSS 方案)

利用 CSS 的 object-fit 属性实现容器内自适应缩略图,不改变原始文件。

<div class="thumbnail-container">
  <img src="original.jpg" class="thumbnail" />
</div>

<style>
  .thumbnail-container {
    width: 200px;
    height: 150px;
    overflow: hidden;
  }
  .thumbnail {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
</style>

服务端生成缩略图(Node.js + Jimp)

使用 Jimp 库处理图片,适合需要后端处理的场景。

const Jimp = require('jimp');

async function createThumbnail(inputPath, outputPath) {
  const image = await Jimp.read(inputPath);
  await image
    .resize(200, Jimp.AUTO)
    .quality(80)
    .writeAsync(outputPath);
}

js实现缩略图

标签: 缩略图js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 //…

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与响应式…

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的H…

js实现复制

js实现复制

使用document.execCommand方法 在较旧的浏览器中,可以使用document.execCommand('copy')实现复制功能。创建一个临时的textarea或input元素,将需要…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…