1273 lines
40 KiB
HTML
1273 lines
40 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>图片压缩 · 功能工具箱</title>
|
||
<script>
|
||
// 嵌入主界面时,手机端由主界面顶部栏提供标题,避免重复
|
||
if (window.self !== window.top) {
|
||
document.documentElement.classList.add("embedded");
|
||
}
|
||
</script>
|
||
<link rel="icon" href="assets/favicon.svg" type="image/svg+xml" />
|
||
<link rel="stylesheet" href="assets/styles.css" />
|
||
<script src="assets/common.js"></script>
|
||
<style>
|
||
.compressor-page {
|
||
max-width: 1220px;
|
||
--compress-success-strong: #065f46;
|
||
--compress-success-mid: #047857;
|
||
--compress-danger-strong: #b91c1c;
|
||
}
|
||
|
||
[data-theme="dark"] .compressor-page {
|
||
--compress-success-strong: var(--success);
|
||
--compress-success-mid: #6ee7b7;
|
||
--compress-danger-strong: var(--danger);
|
||
}
|
||
|
||
.compressor-layout {
|
||
display: grid;
|
||
grid-template-columns: minmax(0, 1fr) 344px;
|
||
gap: 16px;
|
||
align-items: start;
|
||
}
|
||
|
||
.compressor-main {
|
||
min-width: 0;
|
||
}
|
||
|
||
.compressor-side {
|
||
position: sticky;
|
||
top: 16px;
|
||
min-width: 0;
|
||
}
|
||
|
||
/* 视口很矮时取消吸附,避免设置卡片底部无法滚动到 */
|
||
@media (min-width: 901px) and (max-height: 640px) {
|
||
.compressor-side {
|
||
position: static;
|
||
}
|
||
}
|
||
|
||
.media-head {
|
||
flex-wrap: wrap;
|
||
row-gap: 8px;
|
||
}
|
||
|
||
.media-head .btn {
|
||
flex: none;
|
||
}
|
||
|
||
.media-card .image-grid {
|
||
margin-top: 16px;
|
||
}
|
||
|
||
/* 压缩比例悬浮在缩略图右下角 */
|
||
.badge-ratio.ratio-float {
|
||
position: absolute;
|
||
right: 6px;
|
||
bottom: 6px;
|
||
padding: 2px 7px;
|
||
background: #047857;
|
||
color: #fff;
|
||
font-size: 0.64rem;
|
||
backdrop-filter: blur(4px);
|
||
}
|
||
|
||
.media-card .image-card .detail .sizes b {
|
||
color: var(--compress-success-mid);
|
||
}
|
||
|
||
.badge-ratio.ratio-float.pending {
|
||
background: rgba(5, 7, 15, 0.62);
|
||
color: #fff;
|
||
}
|
||
|
||
.badge-ratio.ratio-float.increase {
|
||
background: #dc2626;
|
||
color: #fff;
|
||
}
|
||
|
||
.quality-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
margin-bottom: 7px;
|
||
}
|
||
|
||
.quality-head .field-label {
|
||
font-size: 0.82rem;
|
||
font-weight: 600;
|
||
color: var(--text-secondary);
|
||
}
|
||
|
||
.zoom-display {
|
||
min-width: 48px;
|
||
padding: 5px 10px;
|
||
border: 1px solid var(--border);
|
||
border-radius: var(--radius-sm);
|
||
background: var(--surface-2);
|
||
text-align: center;
|
||
font-size: 0.78rem;
|
||
font-weight: 600;
|
||
color: var(--text-secondary);
|
||
font-variant-numeric: tabular-nums;
|
||
}
|
||
|
||
/* 打开预览时隐藏通知,避免遮挡缩放按钮 */
|
||
.overlay.show ~ .toast-wrap {
|
||
display: none;
|
||
}
|
||
|
||
.quality-badge {
|
||
padding: 2px 10px;
|
||
border-radius: 999px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-size: 0.8rem;
|
||
font-weight: 700;
|
||
font-variant-numeric: tabular-nums;
|
||
}
|
||
|
||
.range-labels {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
font-size: 0.7rem;
|
||
color: var(--text-muted);
|
||
margin-top: -2px;
|
||
}
|
||
|
||
.settings-fields {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 13px;
|
||
margin-top: 16px;
|
||
}
|
||
|
||
.summary-box {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 2px;
|
||
margin-top: 16px;
|
||
padding: 12px 14px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 12px;
|
||
background: var(--surface-2);
|
||
}
|
||
|
||
.summary-box .summary-title {
|
||
font-size: 0.85rem;
|
||
font-weight: 650;
|
||
letter-spacing: -0.01em;
|
||
}
|
||
|
||
.summary-box .summary-detail {
|
||
font-size: 0.74rem;
|
||
line-height: 1.5;
|
||
color: var(--text-muted);
|
||
}
|
||
|
||
.summary-box.is-success {
|
||
background: var(--success-soft);
|
||
border-color: transparent;
|
||
}
|
||
|
||
.summary-box.is-success .summary-title {
|
||
color: var(--compress-success-strong);
|
||
}
|
||
|
||
.summary-box.is-success .summary-detail {
|
||
color: var(--compress-success-mid);
|
||
}
|
||
|
||
.summary-box.is-warning {
|
||
background: var(--danger-soft);
|
||
border-color: transparent;
|
||
}
|
||
|
||
.summary-box.is-warning .summary-title {
|
||
color: var(--compress-danger-strong);
|
||
}
|
||
|
||
.summary-box.is-warning .summary-detail {
|
||
color: var(--compress-danger-strong);
|
||
opacity: 0.9;
|
||
}
|
||
|
||
.settings-cta {
|
||
width: 100%;
|
||
height: 44px;
|
||
margin-top: 14px;
|
||
}
|
||
|
||
.settings-card .progress-wrap {
|
||
margin-top: 12px;
|
||
}
|
||
|
||
.note {
|
||
margin-top: 13px;
|
||
font-size: 0.74rem;
|
||
color: var(--text-muted);
|
||
line-height: 1.55;
|
||
}
|
||
|
||
/* ================= 手机端固定操作栏 ================= */
|
||
|
||
.mobile-bar {
|
||
display: none;
|
||
}
|
||
|
||
@media (max-width: 900px) {
|
||
/* 主界面已有工具标题,嵌入时不再重复显示 */
|
||
html.embedded .tool-header {
|
||
display: none;
|
||
}
|
||
|
||
.compressor-page {
|
||
padding: 16px 14px calc(96px + env(safe-area-inset-bottom));
|
||
}
|
||
|
||
.compressor-layout {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 14px;
|
||
}
|
||
|
||
.compressor-main,
|
||
.compressor-side {
|
||
width: 100%;
|
||
}
|
||
|
||
.compressor-side {
|
||
position: static;
|
||
}
|
||
|
||
/* 手机端用底部固定操作栏替代卡片内按钮 */
|
||
.settings-cta {
|
||
display: none;
|
||
}
|
||
|
||
.settings-card .progress-wrap {
|
||
display: none !important;
|
||
}
|
||
|
||
.mobile-bar {
|
||
position: fixed;
|
||
left: 0;
|
||
right: 0;
|
||
bottom: 0;
|
||
z-index: 60;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 9px;
|
||
padding: 9px 14px calc(9px + env(safe-area-inset-bottom));
|
||
background: var(--surface);
|
||
border-top: 1px solid var(--border);
|
||
box-shadow: 0 -12px 30px -22px rgba(0, 0, 0, 0.45);
|
||
}
|
||
|
||
.mobile-bar[hidden] {
|
||
display: none !important;
|
||
}
|
||
|
||
.mobile-bar-progress {
|
||
position: absolute;
|
||
top: -1px;
|
||
left: 0;
|
||
right: 0;
|
||
height: 3px;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.mobile-bar-progress span {
|
||
display: block;
|
||
width: 0;
|
||
height: 100%;
|
||
background: linear-gradient(90deg, var(--accent), #a78bfa);
|
||
transition: width 0.2s ease;
|
||
}
|
||
|
||
.mobile-bar-info {
|
||
flex: 1;
|
||
min-width: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
line-height: 1.3;
|
||
}
|
||
|
||
.mobile-bar-info strong {
|
||
font-size: 0.82rem;
|
||
font-weight: 650;
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
|
||
.mobile-bar-info span {
|
||
font-size: 0.7rem;
|
||
color: var(--text-muted);
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
|
||
.mobile-bar .btn--soft {
|
||
height: 38px;
|
||
padding: 0 12px;
|
||
flex: none;
|
||
font-size: 0.82rem;
|
||
}
|
||
|
||
.mobile-bar .btn--primary {
|
||
height: 40px;
|
||
padding: 0 16px;
|
||
flex: none;
|
||
font-size: 0.85rem;
|
||
}
|
||
|
||
.toast-wrap {
|
||
bottom: calc(94px + env(safe-area-inset-bottom));
|
||
}
|
||
|
||
/* 手机上尽量保持两列,缩略图更紧凑 */
|
||
.media-card .image-grid {
|
||
grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
|
||
gap: 10px;
|
||
}
|
||
|
||
.media-card .image-card .thumb {
|
||
height: 104px;
|
||
}
|
||
|
||
/* 触屏上放大缩略图操作按钮 */
|
||
.icon-bubble {
|
||
width: 30px;
|
||
height: 30px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.image-card .card-actions {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
@media (max-width: 420px) {
|
||
.media-head .btn {
|
||
padding: 6px 9px;
|
||
}
|
||
|
||
.mobile-bar {
|
||
gap: 7px;
|
||
padding-left: 11px;
|
||
padding-right: 11px;
|
||
}
|
||
|
||
.mobile-bar .btn--primary {
|
||
padding: 0 13px;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="tool-page compressor-page">
|
||
<header class="tool-header">
|
||
<div class="tool-icon">
|
||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||
<path d="M9 3v4a2 2 0 0 1-2 2H3M15 3v4a2 2 0 0 0 2 2h4" />
|
||
<path d="M9 21v-4a2 2 0 0 0-2-2H3M15 21v-4a2 2 0 0 1 2-2h4" />
|
||
</svg>
|
||
</div>
|
||
<div>
|
||
<h1>图片压缩</h1>
|
||
<p>批量压缩图片体积,可调节质量、输出格式与最大尺寸。</p>
|
||
</div>
|
||
<div class="header-actions">
|
||
<button
|
||
class="btn btn--icon btn--ghost"
|
||
data-theme-toggle
|
||
title="切换主题"
|
||
aria-label="切换主题"
|
||
>
|
||
<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true">
|
||
<path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" />
|
||
</svg>
|
||
<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true">
|
||
<circle cx="12" cy="12" r="4" />
|
||
<path
|
||
d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"
|
||
/>
|
||
</svg>
|
||
</button>
|
||
</div>
|
||
</header>
|
||
|
||
<div class="compressor-layout">
|
||
<!-- 左侧主区域:上传 + 结果 -->
|
||
<main class="compressor-main">
|
||
<section class="card media-card" id="mediaCard">
|
||
<div class="card-head media-head">
|
||
<h2>图片</h2>
|
||
<span class="badge" id="imageCountBadge">0 张</span>
|
||
<span class="spacer"></span>
|
||
<button class="btn btn--soft btn--sm" id="downloadAllBtn" hidden>
|
||
下载全部
|
||
</button>
|
||
<button class="btn btn--ghost btn--sm" id="clearAllBtn" disabled>
|
||
清空
|
||
</button>
|
||
</div>
|
||
|
||
<div class="dropzone" id="uploadArea">
|
||
<div class="dropzone-icon">
|
||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||
<path d="M12 16V4M7 9l5-5 5 5" />
|
||
<path d="M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" />
|
||
</svg>
|
||
</div>
|
||
<strong id="dropTitle">点击或拖拽图片到这里</strong>
|
||
<span>支持 JPG、PNG、WEBP 等常见格式,可一次选择多张</span>
|
||
<button class="btn btn--soft btn--sm" id="chooseBtn" type="button">
|
||
选择图片
|
||
</button>
|
||
<input type="file" id="fileInput" accept="image/*" multiple />
|
||
</div>
|
||
|
||
<div class="image-grid" id="imageGrid" hidden></div>
|
||
</section>
|
||
</main>
|
||
|
||
<!-- 右侧设置区 -->
|
||
<aside class="compressor-side" id="settingsCard">
|
||
<section class="card settings-card">
|
||
<div class="card-head">
|
||
<h2>压缩设置</h2>
|
||
<span class="spacer"></span>
|
||
<span class="badge">本地处理</span>
|
||
</div>
|
||
|
||
<div class="quality-head">
|
||
<span class="field-label">压缩质量</span>
|
||
<span class="quality-badge" id="qualityBadge">80%</span>
|
||
</div>
|
||
<input
|
||
type="range"
|
||
id="quality"
|
||
min="10"
|
||
max="100"
|
||
step="5"
|
||
value="80"
|
||
aria-label="压缩质量"
|
||
/>
|
||
<div class="range-labels">
|
||
<span>更小体积</span>
|
||
<span>更高画质</span>
|
||
</div>
|
||
|
||
<div class="settings-fields">
|
||
<div class="field">
|
||
<label for="format">输出格式</label>
|
||
<select id="format">
|
||
<option value="original" selected>保持原格式</option>
|
||
<option value="jpeg">JPEG</option>
|
||
<option value="png">PNG</option>
|
||
<option value="webp">WEBP</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="field">
|
||
<label for="maxWidth">最大宽度</label>
|
||
<select id="maxWidth">
|
||
<option value="0" selected>保持原始尺寸</option>
|
||
<option value="1920">1920 px</option>
|
||
<option value="1280">1280 px</option>
|
||
<option value="800">800 px</option>
|
||
<option value="640">640 px</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="summary-box" id="summaryBox" aria-live="polite">
|
||
<span class="summary-title" id="summaryTitle">等待添加图片</span>
|
||
<span class="summary-detail" id="summaryDetail">
|
||
添加图片后可开始压缩
|
||
</span>
|
||
</div>
|
||
|
||
<button class="btn btn--primary settings-cta" id="compressAllBtn" disabled>
|
||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||
<path d="M9 3v4a2 2 0 0 1-2 2H3M15 3v4a2 2 0 0 0 2 2h4" />
|
||
<path d="M9 21v-4a2 2 0 0 0-2-2H3M15 21v-4a2 2 0 0 1 2-2h4" />
|
||
</svg>
|
||
<span id="compressBtnLabel">开始压缩</span>
|
||
</button>
|
||
|
||
<div class="progress-wrap" id="progressWrap">
|
||
<div class="progress-track">
|
||
<div class="progress-bar" id="progressBar"></div>
|
||
</div>
|
||
<div class="progress-text" id="progressText">处理中…</div>
|
||
</div>
|
||
|
||
<p class="note">
|
||
PNG 为无损格式,质量参数对 PNG 不生效;转换为 JPEG
|
||
时透明区域会填充为白色。若重新编码后体积反而变大,会自动保留原图。修改设置后需要重新压缩。
|
||
</p>
|
||
</section>
|
||
</aside>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 手机端固定操作栏 -->
|
||
<div class="mobile-bar" id="mobileBar" hidden>
|
||
<div class="mobile-bar-progress">
|
||
<span id="mobileProgressBar"></span>
|
||
</div>
|
||
<div class="mobile-bar-info">
|
||
<strong id="mobileBarTitle">0 张图片</strong>
|
||
<span id="mobileBarMeta">质量 80%</span>
|
||
</div>
|
||
<button
|
||
class="btn btn--soft"
|
||
id="mobileSettingsBtn"
|
||
title="跳转到压缩设置"
|
||
aria-label="跳转到压缩设置"
|
||
>
|
||
设置
|
||
</button>
|
||
<button class="btn btn--primary" id="mobileCompressBtn" disabled>
|
||
<span id="mobileCompressLabel">开始压缩</span>
|
||
</button>
|
||
</div>
|
||
|
||
<!-- 大图预览 -->
|
||
<div
|
||
class="overlay"
|
||
id="previewOverlay"
|
||
role="dialog"
|
||
aria-modal="true"
|
||
aria-labelledby="previewTitle"
|
||
>
|
||
<div class="modal">
|
||
<div class="modal-head">
|
||
<span class="modal-title" id="previewTitle">图片预览</span>
|
||
<button
|
||
class="btn btn--icon btn--sm btn--ghost"
|
||
id="previewClose"
|
||
title="关闭"
|
||
aria-label="关闭"
|
||
>
|
||
×
|
||
</button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<img id="previewImage" alt="" />
|
||
</div>
|
||
<div class="modal-foot">
|
||
<button class="btn btn--sm" data-zoom="0.5">50%</button>
|
||
<button class="btn btn--sm" data-zoom="1">100%</button>
|
||
<button class="btn btn--sm" data-zoom="2">200%</button>
|
||
<span class="zoom-display" id="previewZoom">100%</span>
|
||
<span class="hint">Esc 关闭 · +/- 缩放 · 1 重置</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
(function () {
|
||
"use strict";
|
||
|
||
var images = [];
|
||
var previewIndex = -1;
|
||
var previewZoom = 1;
|
||
var isCompressing = false;
|
||
var progressTimer = null;
|
||
|
||
var uploadArea = document.getElementById("uploadArea");
|
||
var fileInput = document.getElementById("fileInput");
|
||
var chooseBtn = document.getElementById("chooseBtn");
|
||
var dropTitle = document.getElementById("dropTitle");
|
||
var imageGrid = document.getElementById("imageGrid");
|
||
var imageCountBadge = document.getElementById("imageCountBadge");
|
||
var mediaCard = document.getElementById("mediaCard");
|
||
var settingsCard = document.getElementById("settingsCard");
|
||
var clearAllBtn = document.getElementById("clearAllBtn");
|
||
var downloadAllBtn = document.getElementById("downloadAllBtn");
|
||
|
||
var compressAllBtn = document.getElementById("compressAllBtn");
|
||
var compressBtnLabel = document.getElementById("compressBtnLabel");
|
||
var progressWrap = document.getElementById("progressWrap");
|
||
var progressBar = document.getElementById("progressBar");
|
||
var progressText = document.getElementById("progressText");
|
||
|
||
var summaryBox = document.getElementById("summaryBox");
|
||
var summaryTitle = document.getElementById("summaryTitle");
|
||
var summaryDetail = document.getElementById("summaryDetail");
|
||
|
||
var qualityInput = document.getElementById("quality");
|
||
var qualityBadge = document.getElementById("qualityBadge");
|
||
var formatSelect = document.getElementById("format");
|
||
var maxWidthSelect = document.getElementById("maxWidth");
|
||
|
||
var mobileBar = document.getElementById("mobileBar");
|
||
var mobileBarTitle = document.getElementById("mobileBarTitle");
|
||
var mobileBarMeta = document.getElementById("mobileBarMeta");
|
||
var mobileCompressBtn = document.getElementById("mobileCompressBtn");
|
||
var mobileCompressLabel = document.getElementById("mobileCompressLabel");
|
||
var mobileSettingsBtn = document.getElementById("mobileSettingsBtn");
|
||
var mobileProgressBar = document.getElementById("mobileProgressBar");
|
||
|
||
var previewOverlay = document.getElementById("previewOverlay");
|
||
var previewTitle = document.getElementById("previewTitle");
|
||
var previewImage = document.getElementById("previewImage");
|
||
var previewZoomLabel = document.getElementById("previewZoom");
|
||
|
||
/* ================= 上传 ================= */
|
||
|
||
function readFile(file) {
|
||
return new Promise(function (resolve, reject) {
|
||
if (!file.type.startsWith("image/")) {
|
||
reject(new Error(file.name + " 不是图片文件"));
|
||
return;
|
||
}
|
||
var reader = new FileReader();
|
||
reader.onload = function (event) {
|
||
resolve({
|
||
id: Date.now() + "-" + Math.random(),
|
||
name: file.name,
|
||
type: file.type,
|
||
originalSize: file.size,
|
||
dataUrl: event.target.result,
|
||
compressed: false,
|
||
unchanged: false,
|
||
compressedDataUrl: null,
|
||
compressedSize: 0,
|
||
compressedFormat: "",
|
||
compressionRatio: 0,
|
||
});
|
||
};
|
||
reader.onerror = function () {
|
||
reject(new Error("读取 " + file.name + " 失败"));
|
||
};
|
||
reader.readAsDataURL(file);
|
||
});
|
||
}
|
||
|
||
function addFiles(fileList) {
|
||
if (isCompressing) {
|
||
Toolbox.toast("正在压缩,请稍候", "info");
|
||
return;
|
||
}
|
||
|
||
var files = Array.prototype.slice.call(fileList || []);
|
||
if (!files.length) return;
|
||
|
||
Promise.all(files.map(readFile))
|
||
.then(function (loaded) {
|
||
images = images.concat(loaded);
|
||
updateUI();
|
||
Toolbox.toast("已添加 " + loaded.length + " 张图片", "success");
|
||
})
|
||
.catch(function (error) {
|
||
Toolbox.toast(error.message, "error");
|
||
});
|
||
}
|
||
|
||
uploadArea.addEventListener("click", function (event) {
|
||
if (event.target === fileInput) return;
|
||
fileInput.click();
|
||
});
|
||
|
||
chooseBtn.addEventListener("click", function (event) {
|
||
event.stopPropagation();
|
||
fileInput.click();
|
||
});
|
||
|
||
fileInput.addEventListener("change", function (event) {
|
||
addFiles(event.target.files);
|
||
event.target.value = "";
|
||
});
|
||
|
||
["dragover", "dragenter"].forEach(function (type) {
|
||
uploadArea.addEventListener(type, function (event) {
|
||
event.preventDefault();
|
||
uploadArea.classList.add("drag-over");
|
||
});
|
||
});
|
||
|
||
["dragleave", "dragend"].forEach(function (type) {
|
||
uploadArea.addEventListener(type, function () {
|
||
uploadArea.classList.remove("drag-over");
|
||
});
|
||
});
|
||
|
||
uploadArea.addEventListener("drop", function (event) {
|
||
event.preventDefault();
|
||
uploadArea.classList.remove("drag-over");
|
||
addFiles(event.dataTransfer.files);
|
||
});
|
||
|
||
/* ================= 界面状态 ================= */
|
||
|
||
function hasCompressed() {
|
||
return images.some(function (image) {
|
||
return image.compressed;
|
||
});
|
||
}
|
||
|
||
function updateUI() {
|
||
var hasImages = images.length > 0;
|
||
|
||
uploadArea.classList.toggle("dropzone--compact", hasImages);
|
||
dropTitle.textContent = hasImages
|
||
? "继续添加图片"
|
||
: "点击或拖拽图片到这里";
|
||
chooseBtn.textContent = hasImages ? "添加" : "选择图片";
|
||
|
||
imageGrid.hidden = !hasImages;
|
||
imageCountBadge.textContent = images.length + " 张";
|
||
downloadAllBtn.hidden = !hasCompressed();
|
||
mobileBar.hidden = !hasImages;
|
||
|
||
renderGrid();
|
||
updateActionState();
|
||
updateSummary();
|
||
}
|
||
|
||
function updateActionState() {
|
||
var hasImages = images.length > 0;
|
||
var label = isCompressing
|
||
? "压缩中…"
|
||
: hasCompressed()
|
||
? "重新压缩"
|
||
: "开始压缩";
|
||
|
||
compressBtnLabel.textContent = label;
|
||
mobileCompressLabel.textContent = label;
|
||
|
||
compressAllBtn.disabled = !hasImages || isCompressing;
|
||
mobileCompressBtn.disabled = !hasImages || isCompressing;
|
||
clearAllBtn.disabled = !hasImages || isCompressing;
|
||
downloadAllBtn.disabled = isCompressing;
|
||
qualityInput.disabled = isCompressing;
|
||
formatSelect.disabled = isCompressing;
|
||
maxWidthSelect.disabled = isCompressing;
|
||
}
|
||
|
||
function updateSummary() {
|
||
var quality = qualityInput.value;
|
||
var compressed = images.filter(function (image) {
|
||
return image.compressed;
|
||
});
|
||
var originalTotal = 0;
|
||
|
||
images.forEach(function (image) {
|
||
originalTotal += image.originalSize;
|
||
});
|
||
|
||
summaryBox.classList.remove("is-success", "is-warning");
|
||
|
||
if (!images.length) {
|
||
summaryTitle.textContent = "等待添加图片";
|
||
summaryDetail.textContent = "添加图片后可开始压缩";
|
||
mobileBarTitle.textContent = "0 张图片";
|
||
mobileBarMeta.textContent = "质量 " + quality + "%";
|
||
return;
|
||
}
|
||
|
||
if (!compressed.length) {
|
||
summaryTitle.textContent = "已选择 " + images.length + " 张图片";
|
||
summaryDetail.textContent =
|
||
"原始大小 " +
|
||
Toolbox.formatBytes(originalTotal) +
|
||
" · 当前质量 " +
|
||
quality +
|
||
"%";
|
||
mobileBarTitle.textContent = images.length + " 张图片";
|
||
mobileBarMeta.textContent =
|
||
"质量 " +
|
||
quality +
|
||
"% · " +
|
||
Toolbox.formatBytes(originalTotal);
|
||
return;
|
||
}
|
||
|
||
var compressedOriginal = 0;
|
||
var compressedTotal = 0;
|
||
|
||
compressed.forEach(function (image) {
|
||
compressedOriginal += image.originalSize;
|
||
compressedTotal += image.compressedSize;
|
||
});
|
||
|
||
var saved = compressedOriginal - compressedTotal;
|
||
var percent = compressedOriginal
|
||
? Math.round((saved / compressedOriginal) * 100)
|
||
: 0;
|
||
var prefix =
|
||
compressed.length < images.length
|
||
? "已压缩 " +
|
||
compressed.length +
|
||
" / " +
|
||
images.length +
|
||
" 张 · "
|
||
: "";
|
||
var detail =
|
||
prefix +
|
||
"原始 " +
|
||
Toolbox.formatBytes(compressedOriginal) +
|
||
" → 现在 " +
|
||
Toolbox.formatBytes(compressedTotal);
|
||
|
||
mobileBarTitle.textContent = images.length + " 张图片";
|
||
|
||
if (saved > 0) {
|
||
summaryBox.classList.add("is-success");
|
||
summaryTitle.textContent =
|
||
"已节省 " + Toolbox.formatBytes(saved) + "(" + percent + "%)";
|
||
mobileBarMeta.textContent =
|
||
"已节省 " + Toolbox.formatBytes(saved) + " · " + quality + "%";
|
||
} else if (saved === 0) {
|
||
summaryTitle.textContent = "体积无变化";
|
||
mobileBarMeta.textContent = "体积无变化 · " + quality + "%";
|
||
} else {
|
||
summaryBox.classList.add("is-warning");
|
||
summaryTitle.textContent =
|
||
"体积增加 " + Toolbox.formatBytes(-saved);
|
||
mobileBarMeta.textContent =
|
||
"体积增加 " + Toolbox.formatBytes(-saved) + " · " + quality + "%";
|
||
}
|
||
|
||
summaryDetail.textContent = detail;
|
||
}
|
||
|
||
/* ================= 结果列表 ================= */
|
||
|
||
function renderGrid() {
|
||
imageGrid.innerHTML = "";
|
||
|
||
images.forEach(function (image, index) {
|
||
var card = document.createElement("div");
|
||
card.className = "image-card";
|
||
|
||
var displayUrl = image.compressed
|
||
? image.compressedDataUrl
|
||
: image.dataUrl;
|
||
var ratio = image.compressionRatio;
|
||
var ratioClass = " pending";
|
||
var ratioText = "待压缩";
|
||
|
||
if (image.compressed) {
|
||
if (image.unchanged) {
|
||
ratioText = "无变化";
|
||
} else if (ratio >= 0) {
|
||
ratioClass = "";
|
||
ratioText = "省 " + Math.abs(ratio) + "%";
|
||
} else {
|
||
ratioClass = " increase";
|
||
ratioText = "增 " + Math.abs(ratio) + "%";
|
||
}
|
||
}
|
||
|
||
card.innerHTML =
|
||
'<div class="thumb">' +
|
||
'<img alt="" draggable="false" />' +
|
||
'<span class="index-badge">' + (index + 1) + "</span>" +
|
||
'<span class="thumb-hint">点击预览</span>' +
|
||
'<span class="badge-ratio ratio-float' +
|
||
ratioClass +
|
||
'">' +
|
||
ratioText +
|
||
"</span>" +
|
||
'<div class="card-actions">' +
|
||
(image.compressed
|
||
? '<button class="icon-bubble" data-action="download" title="下载" aria-label="下载">↓</button>'
|
||
: "") +
|
||
'<button class="icon-bubble danger" data-action="remove" title="删除" aria-label="删除">×</button>' +
|
||
"</div></div>" +
|
||
'<div class="meta"><div class="name"></div>' +
|
||
'<div class="detail"><span class="sizes"></span></div></div>';
|
||
|
||
var img = card.querySelector("img");
|
||
img.src = displayUrl;
|
||
img.alt = image.name;
|
||
card.querySelector(".name").textContent = image.name;
|
||
|
||
var sizes = card.querySelector(".sizes");
|
||
if (image.compressed) {
|
||
sizes.innerHTML = "<s></s> → <b></b>";
|
||
sizes.querySelector("s").textContent = Toolbox.formatBytes(
|
||
image.originalSize,
|
||
);
|
||
sizes.querySelector("b").textContent = Toolbox.formatBytes(
|
||
image.compressedSize,
|
||
);
|
||
} else {
|
||
sizes.textContent = Toolbox.formatBytes(image.originalSize);
|
||
}
|
||
|
||
card.querySelector(".thumb").addEventListener("click", function () {
|
||
openPreview(index);
|
||
});
|
||
|
||
card.querySelectorAll("[data-action]").forEach(function (button) {
|
||
button.addEventListener("click", function (event) {
|
||
event.stopPropagation();
|
||
if (button.dataset.action === "download") {
|
||
downloadImage(index);
|
||
} else {
|
||
removeImage(index);
|
||
}
|
||
});
|
||
});
|
||
|
||
imageGrid.appendChild(card);
|
||
});
|
||
}
|
||
|
||
function removeImage(index) {
|
||
if (isCompressing) {
|
||
Toolbox.toast("正在压缩,请稍候", "info");
|
||
return;
|
||
}
|
||
images.splice(index, 1);
|
||
if (previewIndex === index) closePreview();
|
||
updateUI();
|
||
}
|
||
|
||
clearAllBtn.addEventListener("click", function () {
|
||
if (isCompressing) return;
|
||
images = [];
|
||
fileInput.value = "";
|
||
updateUI();
|
||
Toolbox.toast("图片已清空", "success");
|
||
});
|
||
|
||
/* ================= 压缩 ================= */
|
||
|
||
function loadImage(src) {
|
||
return new Promise(function (resolve, reject) {
|
||
var img = new Image();
|
||
img.onload = function () {
|
||
resolve(img);
|
||
};
|
||
img.onerror = function () {
|
||
reject(new Error("图片加载失败"));
|
||
};
|
||
img.src = src;
|
||
});
|
||
}
|
||
|
||
function sourceFormat(image) {
|
||
if (image.type.indexOf("png") >= 0) return "png";
|
||
if (image.type.indexOf("webp") >= 0) return "webp";
|
||
if (image.type.indexOf("jpeg") >= 0 || image.type.indexOf("jpg") >= 0)
|
||
return "jpeg";
|
||
return "other";
|
||
}
|
||
|
||
function outputFormat(image, formatValue) {
|
||
if (formatValue !== "original") return formatValue;
|
||
if (image.type.indexOf("png") >= 0) return "png";
|
||
if (image.type.indexOf("webp") >= 0) return "webp";
|
||
return "jpeg";
|
||
}
|
||
|
||
function compressImage(image, options) {
|
||
return new Promise(function (resolve, reject) {
|
||
loadImage(image.dataUrl).then(function (img) {
|
||
var scale =
|
||
options.maxWidth && img.width > options.maxWidth
|
||
? options.maxWidth / img.width
|
||
: 1;
|
||
var width = Math.max(1, Math.round(img.width * scale));
|
||
var height = Math.max(1, Math.round(img.height * scale));
|
||
var format = outputFormat(image, options.format);
|
||
|
||
var canvas = document.createElement("canvas");
|
||
canvas.width = width;
|
||
canvas.height = height;
|
||
var ctx = canvas.getContext("2d");
|
||
|
||
// JPEG 不支持透明,先铺白底
|
||
if (format === "jpeg") {
|
||
ctx.fillStyle = "#ffffff";
|
||
ctx.fillRect(0, 0, width, height);
|
||
}
|
||
|
||
ctx.drawImage(img, 0, 0, width, height);
|
||
|
||
canvas.toBlob(
|
||
function (blob) {
|
||
if (!blob) {
|
||
reject(new Error("压缩失败"));
|
||
return;
|
||
}
|
||
var reader = new FileReader();
|
||
reader.onload = function (event) {
|
||
resolve({
|
||
dataUrl: event.target.result,
|
||
size: blob.size,
|
||
format: format,
|
||
scaled: scale !== 1,
|
||
});
|
||
};
|
||
reader.onerror = function () {
|
||
reject(new Error("读取压缩结果失败"));
|
||
};
|
||
reader.readAsDataURL(blob);
|
||
},
|
||
"image/" + format,
|
||
options.quality,
|
||
);
|
||
}, reject);
|
||
});
|
||
}
|
||
|
||
function scrollToElement(element) {
|
||
var reduceMotion =
|
||
window.matchMedia &&
|
||
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
||
element.scrollIntoView({
|
||
behavior: reduceMotion ? "auto" : "smooth",
|
||
block: "start",
|
||
});
|
||
}
|
||
|
||
function setProgress(percent, text) {
|
||
progressBar.style.width = percent + "%";
|
||
progressText.textContent = text;
|
||
mobileProgressBar.style.width = percent + "%";
|
||
}
|
||
|
||
function showProgress(show) {
|
||
progressWrap.classList.toggle("show", show);
|
||
if (!show) {
|
||
setProgress(0, "处理中…");
|
||
mobileProgressBar.style.width = "0%";
|
||
}
|
||
}
|
||
|
||
async function compressAll() {
|
||
if (!images.length || isCompressing) return;
|
||
|
||
isCompressing = true;
|
||
updateActionState();
|
||
clearTimeout(progressTimer);
|
||
showProgress(true);
|
||
setProgress(0, "开始压缩…");
|
||
mobileBarMeta.textContent = "正在准备压缩…";
|
||
|
||
var options = {
|
||
quality: parseInt(qualityInput.value, 10) / 100,
|
||
format: formatSelect.value,
|
||
maxWidth: parseInt(maxWidthSelect.value, 10) || 0,
|
||
};
|
||
|
||
try {
|
||
for (var i = 0; i < images.length; i += 1) {
|
||
var progress = Math.round((i / images.length) * 92);
|
||
var text =
|
||
"正在压缩第 " + (i + 1) + " / " + images.length + " 张…";
|
||
setProgress(progress, text);
|
||
mobileBarMeta.textContent =
|
||
"正在压缩 " + (i + 1) + " / " + images.length + "…";
|
||
|
||
var result = await compressImage(images[i], options);
|
||
var keepOriginal =
|
||
!result.scaled &&
|
||
options.format === "original" &&
|
||
sourceFormat(images[i]) === result.format &&
|
||
result.size >= images[i].originalSize;
|
||
|
||
images[i].compressed = true;
|
||
images[i].unchanged = keepOriginal;
|
||
images[i].compressedDataUrl = keepOriginal
|
||
? images[i].dataUrl
|
||
: result.dataUrl;
|
||
images[i].compressedSize = keepOriginal
|
||
? images[i].originalSize
|
||
: result.size;
|
||
images[i].compressedFormat = result.format;
|
||
images[i].compressionRatio = keepOriginal
|
||
? 0
|
||
: images[i].originalSize
|
||
? Math.round(
|
||
(1 - result.size / images[i].originalSize) * 100,
|
||
)
|
||
: 0;
|
||
}
|
||
|
||
setProgress(100, "压缩完成");
|
||
isCompressing = false;
|
||
updateUI();
|
||
|
||
if (window.innerWidth <= 900) {
|
||
scrollToElement(mediaCard);
|
||
}
|
||
|
||
Toolbox.toast("已压缩 " + images.length + " 张图片", "success");
|
||
progressTimer = setTimeout(function () {
|
||
showProgress(false);
|
||
}, 1600);
|
||
} catch (error) {
|
||
isCompressing = false;
|
||
showProgress(false);
|
||
updateActionState();
|
||
updateSummary();
|
||
Toolbox.toast(
|
||
"压缩失败:" + (error.message || "未知错误"),
|
||
"error",
|
||
);
|
||
}
|
||
}
|
||
|
||
compressAllBtn.addEventListener("click", compressAll);
|
||
mobileCompressBtn.addEventListener("click", compressAll);
|
||
|
||
mobileSettingsBtn.addEventListener("click", function () {
|
||
scrollToElement(settingsCard);
|
||
});
|
||
|
||
/* ================= 下载 ================= */
|
||
|
||
function downloadFilename(image) {
|
||
var base = image.name.replace(/\.[^.]+$/, "") || "image";
|
||
var ext =
|
||
image.compressedFormat === "jpeg"
|
||
? "jpg"
|
||
: image.compressedFormat || "jpg";
|
||
return "compressed_" + base + "." + ext;
|
||
}
|
||
|
||
function downloadImage(index) {
|
||
var image = images[index];
|
||
if (!image || !image.compressed) {
|
||
Toolbox.toast("这张图片还没有压缩", "error");
|
||
return;
|
||
}
|
||
var link = document.createElement("a");
|
||
link.download = downloadFilename(image);
|
||
link.href = image.compressedDataUrl;
|
||
link.click();
|
||
}
|
||
|
||
downloadAllBtn.addEventListener("click", function () {
|
||
var compressed = images.filter(function (image) {
|
||
return image.compressed;
|
||
});
|
||
|
||
if (!compressed.length) {
|
||
Toolbox.toast("没有可下载的压缩图片", "error");
|
||
return;
|
||
}
|
||
|
||
compressed.forEach(function (image, index) {
|
||
setTimeout(function () {
|
||
var link = document.createElement("a");
|
||
link.download = downloadFilename(image);
|
||
link.href = image.compressedDataUrl;
|
||
link.click();
|
||
}, index * 400);
|
||
});
|
||
|
||
Toolbox.toast("正在下载 " + compressed.length + " 张图片", "success");
|
||
});
|
||
|
||
/* ================= 参数变更 ================= */
|
||
|
||
function invalidateResults() {
|
||
var changed = false;
|
||
images.forEach(function (image) {
|
||
if (image.compressed) {
|
||
image.compressed = false;
|
||
image.unchanged = false;
|
||
image.compressedDataUrl = null;
|
||
image.compressedSize = 0;
|
||
image.compressedFormat = "";
|
||
image.compressionRatio = 0;
|
||
changed = true;
|
||
}
|
||
});
|
||
|
||
if (changed) {
|
||
updateUI();
|
||
Toolbox.toast("压缩参数已更新,请重新压缩", "info");
|
||
} else {
|
||
updateSummary();
|
||
}
|
||
}
|
||
|
||
qualityInput.addEventListener("input", function () {
|
||
qualityBadge.textContent = qualityInput.value + "%";
|
||
if (!hasCompressed()) updateSummary();
|
||
});
|
||
|
||
qualityInput.addEventListener("change", invalidateResults);
|
||
formatSelect.addEventListener("change", invalidateResults);
|
||
maxWidthSelect.addEventListener("change", invalidateResults);
|
||
|
||
/* ================= 大图预览 ================= */
|
||
|
||
function setPreviewZoom(zoom) {
|
||
previewZoom = Math.min(5, Math.max(0.1, zoom));
|
||
previewImage.style.transform = "scale(" + previewZoom + ")";
|
||
previewZoomLabel.textContent = Math.round(previewZoom * 100) + "%";
|
||
}
|
||
|
||
function openPreview(index) {
|
||
var image = images[index];
|
||
if (!image) return;
|
||
previewIndex = index;
|
||
previewImage.src = image.compressed
|
||
? image.compressedDataUrl
|
||
: image.dataUrl;
|
||
previewImage.alt = image.name;
|
||
previewTitle.textContent =
|
||
image.name + (image.compressed ? " · 压缩后" : " · 原图");
|
||
setPreviewZoom(1);
|
||
previewOverlay.classList.add("show");
|
||
document.body.style.overflow = "hidden";
|
||
}
|
||
|
||
function closePreview() {
|
||
previewOverlay.classList.remove("show");
|
||
document.body.style.overflow = "";
|
||
previewIndex = -1;
|
||
}
|
||
|
||
document.getElementById("previewClose").addEventListener("click", closePreview);
|
||
|
||
previewOverlay.addEventListener("click", function (event) {
|
||
if (event.target === previewOverlay) closePreview();
|
||
});
|
||
|
||
document.querySelectorAll("[data-zoom]").forEach(function (button) {
|
||
button.addEventListener("click", function () {
|
||
setPreviewZoom(Number(button.dataset.zoom));
|
||
});
|
||
});
|
||
|
||
document.addEventListener("keydown", function (event) {
|
||
if (!previewOverlay.classList.contains("show")) return;
|
||
|
||
if (event.key === "Escape") {
|
||
closePreview();
|
||
} else if (event.key === "+" || event.key === "=") {
|
||
setPreviewZoom(previewZoom * 1.2);
|
||
} else if (event.key === "-" || event.key === "_") {
|
||
setPreviewZoom(previewZoom / 1.2);
|
||
} else if (event.key === "1") {
|
||
setPreviewZoom(1);
|
||
}
|
||
});
|
||
|
||
// 初始化
|
||
updateUI();
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|