# 图片水印打码工具 # 原作者:joyqi,https://github.com/joyqi/sfz (GPL-3.0),基于提交 32523c3b9189bb73a831ee962cbd85dab1c51025 # 2026-12-04 郑州客客推网络科技有限公司修改: # - 去掉「实时刷新」开关,改动参数后总是立即重画; # - 支持拖入图片、WebP 格式,格式不支持时在页面上提示而不是弹窗; # - 加「下载图片」按钮,可选 JPG(体积小)或 PNG; # - 加常用水印文字一键填入; # - 记录匿名使用事件(选图、下载),不记录图片和文字内容。 # 本文件按 GPL-3.0 发布,许可证全文见同目录 LICENSE。 $ = (sel) -> document.querySelector sel inputItems = ['text', 'color', 'alpha', 'angle', 'space', 'size'] input = {} image = $ '#image' drop = $ '#drop' graph = $ '#graph' result = $ '#result' msg = $ '#msg' file = null canvas = null textCtx = null redraw = null log = (ev, data) -> window.kt? ev, data generateFileName = (ext) -> pad = (n) -> if n < 10 then '0' + n else n d = new Date '水印-' + d.getFullYear() + (pad d.getMonth() + 1) + (pad d.getDate()) + '-' + \ (pad d.getHours()) + (pad d.getMinutes()) + (pad d.getSeconds()) + '.' + ext download = (type) -> return if not canvas? ext = if type == 'image/png' then 'png' else 'jpg' canvas.toBlob (blob) -> link = document.createElement 'a' link.download = generateFileName ext link.href = URL.createObjectURL blob document.body.appendChild link link.click() setTimeout -> document.body.removeChild link URL.revokeObjectURL link.href , 1000 log 'wm_download', { fmt: ext, kb: Math.round blob.size / 1024 } , type, 0.92 readFile = -> return if not file? fileReader = new FileReader fileReader.onload = -> img = new Image img.onload = -> canvas = document.createElement 'canvas' canvas.width = img.width canvas.height = img.height textCtx = null ctx = canvas.getContext '2d' # JPG 没有透明背景,先铺白底 ctx.fillStyle = '#fff' ctx.fillRect 0, 0, canvas.width, canvas.height ctx.drawImage img, 0, 0 redraw = -> ctx.clearRect 0, 0, canvas.width, canvas.height ctx.fillStyle = '#fff' ctx.fillRect 0, 0, canvas.width, canvas.height ctx.drawImage img, 0, 0 drawText() graph.innerHTML = '' graph.appendChild canvas result.hidden = false ($ '#empty').hidden = true canvas.addEventListener 'click', -> download 'image/jpeg' img.onerror = -> showMsg '这张图片打不开,请换一张试试。' img.src = fileReader.result fileReader.readAsDataURL file makeStyle = -> match = input.color.value.match /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i 'rgba(' + (parseInt match[1], 16) + ',' + (parseInt match[2], 16) + ',' \ + (parseInt match[3], 16) + ',' + input.alpha.value + ')' drawText = -> return if not canvas? textSize = input.size.value * Math.max 15, (Math.min canvas.width, canvas.height) / 25 if textCtx? redraw() else textCtx = canvas.getContext '2d' textCtx.save() textCtx.translate(canvas.width / 2, canvas.height / 2) textCtx.rotate (input.angle.value) * Math.PI / 180 textCtx.fillStyle = makeStyle() textCtx.font = 'bold ' + textSize + 'px -apple-system,"Helvetica Neue",Helvetica,Arial,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif' width = (textCtx.measureText input.text.value).width step = Math.sqrt (Math.pow canvas.width, 2) + (Math.pow canvas.height, 2) margin = (textCtx.measureText '啊').width x = Math.ceil step / (width + margin) y = Math.ceil (step / (input.space.value * textSize)) / 2 for i in [-x..x] for j in [-y..y] textCtx.fillText input.text.value, (width + margin) * i, input.space.value * textSize * j textCtx.restore() return showMsg = (text) -> msg.textContent = text msg.hidden = not text useFile = (f) -> return if not f? if f.type not in ['image/png', 'image/jpeg', 'image/gif', 'image/webp'] return showMsg '只支持 JPG、PNG、GIF、WebP 格式的图片。' showMsg '' file = f readFile() image.addEventListener 'change', -> useFile @files[0] drop.addEventListener 'dragover', (e) -> e.preventDefault() drop.classList.add 'over' drop.addEventListener 'dragleave', -> drop.classList.remove 'over' drop.addEventListener 'drop', (e) -> e.preventDefault() drop.classList.remove 'over' useFile e.dataTransfer.files[0] inputItems.forEach (item) -> el = $ '#' + item input[item] = el el.addEventListener 'input', drawText document.querySelectorAll('[data-text]').forEach (b) -> b.addEventListener 'click', -> input.text.value = b.dataset.text drawText() input.text.focus() ($ '#dl-jpg').addEventListener 'click', -> download 'image/jpeg' ($ '#dl-png').addEventListener 'click', -> download 'image/png'