-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageResizer.html
More file actions
301 lines (262 loc) · 8.87 KB
/
Copy pathImageResizer.html
File metadata and controls
301 lines (262 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片尺寸调整 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.resizer-controls {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 15px 0;
}
.control-item {
display: flex;
flex-direction: column;
}
.control-item label {
margin-bottom: 5px;
color: var(--accent-color);
}
.control-item input, .control-item select {
padding: 8px;
}
.image-preview-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin: 20px 0;
}
.image-preview {
flex: 1;
min-width: 300px;
}
.image-preview img {
max-width: 100%;
max-height: 400px;
display: block;
margin: 0 auto;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>图片尺寸调整</h2>
<p>调整图片尺寸大小</p>
<div class="info-box">
<p><strong>使用说明:</strong></p>
<p>• 上传图片后,设置新的宽度和高度,然后点击调整尺寸</p>
<p>• 可以保持原始宽高比,或自定义宽高</p>
<p>• 调整后的图片会自动下载</p>
</div>
<div>
<input type="file" @change="handleFileUpload" accept="image/*" />
<div class="resizer-controls">
<div class="control-item">
<label for="width">宽度 (px)</label>
<input type="number" id="width" v-model.number="width" :disabled="keepAspectRatio" />
</div>
<div class="control-item">
<label for="height">高度 (px)</label>
<input type="number" id="height" v-model.number="height" :disabled="keepAspectRatio" />
</div>
<div class="control-item">
<label for="quality">质量 (1-100)</label>
<input type="number" id="quality" v-model.number="quality" min="1" max="100" />
</div>
</div>
<div class="control-item">
<label>
<input type="checkbox" v-model="keepAspectRatio"> 保持原始宽高比
</label>
</div>
<div class="controls">
<button @click="resizeImage" :disabled="!originalImage">调整尺寸</button>
<button @click="resetForm">重置</button>
</div>
<div class="image-preview-container" v-if="originalImage">
<div class="image-preview">
<h3>原始图片</h3>
<img :src="originalImage" alt="Original Image" />
<p>尺寸: {{ originalWidth }} x {{ originalHeight }}</p>
</div>
<div class="image-preview" v-if="resizedImage">
<h3>调整后图片</h3>
<img :src="resizedImage" alt="Resized Image" />
<p>尺寸: {{ resizedWidth }} x {{ resizedHeight }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, watch } = Vue;
createApp({
setup() {
const originalImage = ref('');
const originalWidth = ref(0);
const originalHeight = ref(0);
const width = ref(800);
const height = ref(600);
const quality = ref(80);
const keepAspectRatio = ref(true);
const resizedImage = ref('');
const resizedWidth = ref(0);
const resizedHeight = ref(0);
const handleFileUpload = (event) => {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
originalImage.value = e.target.result;
// 获取原始图片尺寸
const img = new Image();
img.onload = () => {
originalWidth.value = img.width;
originalHeight.value = img.height;
// 如果保持宽高比,则根据原始比例设置新尺寸
if (keepAspectRatio.value) {
const aspectRatio = img.width / img.height;
width.value = Math.round(height.value * aspectRatio);
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
};
watch(keepAspectRatio, (newVal) => {
if (newVal && originalImage.value) {
const aspectRatio = originalWidth.value / originalHeight.value;
width.value = Math.round(height.value * aspectRatio);
}
});
watch(height, (newHeight) => {
if (keepAspectRatio.value && originalImage.value) {
const aspectRatio = originalWidth.value / originalHeight.value;
width.value = Math.round(newHeight * aspectRatio);
}
});
watch(width, (newWidth) => {
if (keepAspectRatio.value && originalImage.value) {
const aspectRatio = originalHeight.value / originalWidth.value;
height.value = Math.round(newWidth * aspectRatio);
}
});
const resizeImage = () => {
if (!originalImage.value) return;
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = width.value;
canvas.height = height.value;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width.value, height.value);
const mimeType = 'image/jpeg';
const newQuality = quality.value / 100;
resizedImage.value = canvas.toDataURL(mimeType, newQuality);
resizedWidth.value = width.value;
resizedHeight.value = height.value;
// 创建下载链接
const link = document.createElement('a');
link.href = resizedImage.value;
link.download = 'resized_image.jpg';
link.click();
};
img.src = originalImage.value;
};
const resetForm = () => {
originalImage.value = '';
width.value = 800;
height.value = 600;
quality.value = 80;
keepAspectRatio.value = true;
resizedImage.value = '';
originalWidth.value = 0;
originalHeight.value = 0;
resizedWidth.value = 0;
resizedHeight.value = 0;
};
return {
originalImage,
originalWidth,
originalHeight,
width,
height,
quality,
keepAspectRatio,
resizedImage,
resizedWidth,
resizedHeight,
handleFileUpload,
resizeImage,
resetForm
};
}
}).mount('#app');
</script>
</body>
</html>