Files
PHP/upload.php
2026-11-27 15:41:43 +08:00

153 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/layout.php';
$user = require_password_ready($conn, require_login($conn));
if ($user['is_admin']) {
header('Location: admin.php');
exit;
}
$points = (int)$user['points'];
render_header('上传图片', $user, 'upload.php', '自动压缩为 600px PNG');
?>
<div class="page-head">
<div>
<div class="page-head__title">上传图片</div>
<div class="page-head__desc">
上传一张扣 1 分,当前剩余 <strong id="remain"><?php echo $points; ?></strong> 分
· 服务端自动压缩到 600px 以内并以 PNG 保存
</div>
</div>
<div class="page-head__actions">
<a href="gallery.php" class="btn btn--ghost"><?php echo icon('arrow-left', 16); ?>返回图库</a>
</div>
</div>
<?php if ($points <= 0): ?>
<div class="alert alert--warning">
<?php echo icon('warn'); ?><span>积分已用完,暂时无法上传。删除图片可返还积分,或联系管理员。</span>
</div>
<?php endif; ?>
<div class="card" style="max-width:720px">
<div class="card__body">
<div class="dropzone" id="dropzone">
<div class="dropzone__icon"><?php echo icon('upload', 28); ?></div>
<div class="dropzone__title">点击选择图片,或把图片拖到这里</div>
<div class="dropzone__hint">支持 JPG / PNG / GIF / WEBP,最大 8MB</div>
<input type="file" id="image" accept="image/*" hidden>
</div>
<div class="preview hidden" id="previewWrap" style="margin-top:1.25rem">
<img id="previewImg" class="preview__img" src="" alt="预览">
<div>
<div style="font-weight:650" id="fileName"></div>
<div class="muted small" id="fileMeta"></div>
<div class="progress" style="margin:1rem 0">
<div class="progress__bar" id="progressBar"></div>
</div>
<div class="toolbar" style="margin:0">
<button class="btn btn--primary" id="uploadButton" disabled><?php echo icon('upload', 15); ?>开始上传</button>
<button class="btn btn--ghost" id="reselectButton">重新选择</button>
</div>
</div>
</div>
</div>
</div>
<script>
var dropzone = document.getElementById('dropzone');
var input = document.getElementById('image');
var previewWrap = document.getElementById('previewWrap');
var previewImg = document.getElementById('previewImg');
var fileName = document.getElementById('fileName');
var fileMeta = document.getElementById('fileMeta');
var uploadBtn = document.getElementById('uploadButton');
var progressBar = document.getElementById('progressBar');
var selectedFile = null;
var MAX_BYTES = 8 * 1024 * 1024;
function humanSize(n) {
return n > 1024 * 1024 ? (n / 1024 / 1024).toFixed(1) + ' MB' : Math.round(n / 1024) + ' KB';
}
function pickFile(file) {
if (!file) return;
if (!file.type.match('image.*')) { toast('请选择图片文件', 'warning'); return; }
if (file.size > MAX_BYTES) { toast('图片超过 8MB,请压缩后再传', 'warning'); return; }
selectedFile = file;
previewImg.src = URL.createObjectURL(file);
fileName.textContent = file.name;
fileMeta.textContent = humanSize(file.size) + ' · 上传后会转为 PNG';
progressBar.style.width = '0%';
previewWrap.classList.remove('hidden');
uploadBtn.disabled = false;
}
dropzone.addEventListener('click', function () { input.click(); });
input.addEventListener('change', function () { pickFile(input.files[0]); });
document.getElementById('reselectButton').addEventListener('click', function (e) {
e.stopPropagation();
input.value = '';
input.click();
});
['dragenter', 'dragover'].forEach(function (ev) {
dropzone.addEventListener(ev, function (e) { e.preventDefault(); dropzone.classList.add('is-over'); });
});
['dragleave', 'drop'].forEach(function (ev) {
dropzone.addEventListener(ev, function (e) { e.preventDefault(); dropzone.classList.remove('is-over'); });
});
dropzone.addEventListener('drop', function (e) {
if (e.dataTransfer.files.length) pickFile(e.dataTransfer.files[0]);
});
uploadBtn.addEventListener('click', function () {
if (!selectedFile) return;
var fd = new FormData();
fd.append('image', selectedFile, selectedFile.name);
fd.append('csrf_token', window.CSRF_TOKEN);
uploadBtn.disabled = true;
uploadBtn.textContent = '上传中…';
$.ajax({
url: 'up_pic.php',
type: 'POST',
// 进度条需要自定义 xhr;关掉 global,避免全局 ajax 事件被重复触发
data: fd,
processData: false,
contentType: false,
global: false,
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
progressBar.style.width = Math.round(e.loaded / e.total * 100) + '%';
}
});
return xhr;
},
success: function (r) {
if (typeof r === 'string') { try { r = JSON.parse(r); } catch (e) { r = {}; } }
if (r.success) {
toast('上传成功,剩余 ' + r.points + ' 分');
setTimeout(function () { location.href = 'gallery.php'; }, 700);
} else {
toast(r.message || '上传失败', 'danger');
uploadBtn.disabled = false;
uploadBtn.textContent = '开始上传';
}
},
error: function (xhr) {
var msg = '上传失败';
try { msg = JSON.parse(xhr.responseText).message || msg; } catch (e) {}
toast(msg, 'danger');
uploadBtn.disabled = false;
uploadBtn.textContent = '开始上传';
progressBar.style.width = '0%';
}
});
});
</script>
<?php render_footer($user, 'upload.php');