File size: 6,413 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload · Automatic Watermark</title>
<style>
  * { box-sizing: border-box; }
  body { font-family: system-ui, sans-serif; margin: 0; background: #f6f8fa; color: #1f2328; }
  header { background: #24292f; color: #fff; padding: 16px 24px; }
  header h1 { margin: 0; font-size: 20px; }
  main { max-width: 860px; margin: 0 auto; padding: 24px; }
  .panel { background: #fff; border: 1px solid #d0d7de; border-radius: 10px; padding: 20px; }
  .controls { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; margin-top: 16px; }
  .controls label { display: flex; flex-direction: column; font-size: 13px; gap: 4px; }
  .controls input, .controls select { padding: 6px 8px; border: 1px solid #c7ced6; border-radius: 6px; }
  canvas { display: block; margin-top: 20px; max-width: 100%; border: 1px solid #d0d7de; border-radius: 8px; background: #fafbfc; }
  .hint { color: #57606a; margin-top: 8px; }
  .result { margin-top: 16px; }
  .result img { max-width: 100%; border: 1px solid #d0d7de; border-radius: 8px; }
</style>
</head>
<body>
  <header><h1>Image Upload · Automatic Watermark</h1></header>
  <main>
    <section class="panel">
      <p>Select an image to upload. The system adds a text watermark automatically while preserving the original aspect ratio.</p>
      <input data-testid="image-upload" type="file" accept="image/*" aria-label="upload image">

      <div class="controls">
        <label>Watermark Text
          <input data-testid="wm-text" type="text" value="Answer">
        </label>
        <label>Font Size
          <input data-testid="wm-size" type="number" value="24" min="8" max="120">
        </label>
        <label>Opacity
          <input data-testid="wm-opacity" type="range" min="0" max="1" step="0.05" value="0.45">
        </label>
        <label>Position
          <select data-testid="wm-position">
            <option value="bottom-right" selected>Bottom Right</option>
            <option value="bottom-left">Bottom Left</option>
            <option value="top-right">Top Right</option>
            <option value="top-left">Top Left</option>
            <option value="center">Center</option>
          </select>
        </label>
      </div>

      <p class="hint" data-testid="status">No image uploaded</p>
      <canvas data-testid="watermark-canvas" width="400" height="280"></canvas>

      <!-- local in-memory store for original + watermarked images -->
      <div data-testid="image-store" data-storage="local" hidden></div>

      <div class="result">
        <img data-testid="result-image" alt="watermarked result" hidden>
      </div>
    </section>
  </main>

<script>
  (function () {
    var store = { originals: [], watermarked: [] };   // local in-memory store
    window.__getImageStore=()=>JSON.parse(JSON.stringify(store));
    var input = document.querySelector('[data-testid="image-upload"]');
    var canvas = document.querySelector('[data-testid="watermark-canvas"]');
    var ctx = canvas.getContext('2d');
    var status = document.querySelector('[data-testid="status"]');
    var resultImg = document.querySelector('[data-testid="result-image"]');
    var cfg = {
      text: document.querySelector('[data-testid="wm-text"]'),
      size: document.querySelector('[data-testid="wm-size"]'),
      opacity: document.querySelector('[data-testid="wm-opacity"]'),
      position: document.querySelector('[data-testid="wm-position"]')
    };
    var currentImage = null;   // last uploaded HTMLImageElement (original, untouched)

    function placeWatermark(w, h, fontSize, pos) {
      var pad = Math.round(fontSize * 0.6);
      switch (pos) {
        case 'bottom-left': return { x: pad, y: h - pad, align: 'left', baseline: 'alphabetic' };
        case 'top-right':   return { x: w - pad, y: pad + fontSize, align: 'right', baseline: 'alphabetic' };
        case 'top-left':    return { x: pad, y: pad + fontSize, align: 'left', baseline: 'alphabetic' };
        case 'center':      return { x: w / 2, y: h / 2, align: 'center', baseline: 'middle' };
        default:            return { x: w - pad, y: h - pad, align: 'right', baseline: 'alphabetic' };
      }
    }

    function render() {
      if (!currentImage) return;
      // Preserve the original image dimensions / aspect ratio exactly.
      var w = currentImage.naturalWidth || currentImage.width;
      var h = currentImage.naturalHeight || currentImage.height;
      canvas.width = w;
      canvas.height = h;
      ctx.clearRect(0, 0, w, h);
      ctx.drawImage(currentImage, 0, 0, w, h);

      var text = cfg.text.value || 'Answer';
      var fontSize = Math.max(8, parseInt(cfg.size.value, 10) || 24);
      var opacity = Math.min(1, Math.max(0, parseFloat(cfg.opacity.value)));
      var pos = placeWatermark(w, h, fontSize, cfg.position.value);

      ctx.save();
      ctx.globalAlpha = opacity;
      ctx.font = 'bold ' + fontSize + 'px system-ui, sans-serif';
      ctx.textAlign = pos.align;
      ctx.textBaseline = pos.baseline;
      ctx.lineWidth = Math.max(1, fontSize / 12);
      ctx.strokeStyle = 'rgba(0,0,0,0.55)';
      ctx.fillStyle = '#ffffff';
      ctx.strokeText(text, pos.x, pos.y);
      ctx.fillText(text, pos.x, pos.y);
      ctx.restore();

      var dataUrl = canvas.toDataURL('image/png');
      store.watermarked[store.watermarked.length - 1] = dataUrl;
      resultImg.src = dataUrl;
      resultImg.hidden = false;
      status.textContent = 'Watermark "' + text + '" added · Original dimensions: ' + w + '×' + h;
    }

    input.addEventListener('change', function (e) {
      var file = (e.target.files || [])[0];
      if (!file) return;
      var reader = new FileReader();
      reader.onload = function () {
        var img = new Image();
        img.onload = function () {
          currentImage = img;
          store.originals.push(reader.result);
          store.watermarked.push(null);
          render();
        };
        img.src = reader.result;
      };
      reader.readAsDataURL(file);
    });

    // Re-apply the watermark when configuration changes (existing originals untouched).
    Object.keys(cfg).forEach(function (k) {
      cfg[k].addEventListener('input', render);
      cfg[k].addEventListener('change', render);
    });
  })();
</script>
</body>
</html>