-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsocial-media-cropper.html
196 lines (178 loc) · 5.9 KB
/
social-media-cropper.html
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
<!DOCTYPE html>
<html>
<head>
<title>Social Media Card Cropper</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.css">
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.drop-zone {
border: 2px dashed #ccc;
border-radius: 4px;
padding: 20px;
text-align: center;
margin-bottom: 20px;
background: #fafafa;
cursor: pointer;
}
.drop-zone.dragover {
border-color: #666;
background: #eee;
}
.img-container {
max-width: 100%;
margin-bottom: 20px;
}
#image {
display: block;
max-width: 100%;
}
.preview-container {
margin-top: 20px;
padding: 10px;
background: #fafafa;
border-radius: 4px;
}
#preview {
width: 100%;
max-width: 600px;
margin: 0 auto;
display: block;
}
.download-btn {
display: inline-block;
padding: 10px 20px;
background: #0066cc;
color: white;
text-decoration: none;
border-radius: 4px;
margin-top: 10px;
}
.download-btn:hover {
background: #0052a3;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Social Media Card Cropper</h1>
<p>Drop an image or click to select. The crop area is fixed to 2:1 ratio.</p>
<div class="drop-zone" id="dropZone">
Drop image here, click to select, or paste from clipboard
<input type="file" id="fileInput" accept="image/*" class="hidden">
</div>
<div class="img-container">
<img id="image" class="hidden">
</div>
<div class="preview-container hidden" id="previewContainer">
<h3>Preview (0.7 quality JPEG)</h3>
<img id="preview">
<div style="text-align: center; margin-top: 10px;">
<a href="#" id="downloadBtn" class="download-btn">Download Social Media Card</a>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.js"></script>
<script>
let cropper = null;
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
// Handle paste events
document.addEventListener('paste', (e) => {
e.preventDefault();
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (const item of items) {
if (item.type.indexOf('image') === 0) {
const file = item.getAsFile();
handleFile(file);
break;
}
}
});
const image = document.getElementById('image');
const preview = document.getElementById('preview');
const previewContainer = document.getElementById('previewContainer');
const downloadBtn = document.getElementById('downloadBtn');
// Handle drag and drop
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
handleFile(e.dataTransfer.files[0]);
});
// Handle click to select
dropZone.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
handleFile(e.target.files[0]);
}
});
function handleFile(file) {
if (!file.type.startsWith('image/')) {
alert('Please select an image file.');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
image.src = e.target.result;
image.classList.remove('hidden');
initCropper();
};
reader.readAsDataURL(file);
}
function initCropper() {
if (cropper) {
cropper.destroy();
}
cropper = new Cropper(image, {
aspectRatio: 2,
viewMode: 1,
dragMode: 'move',
autoCropArea: 1,
restore: false,
guides: true,
center: true,
highlight: false,
cropBoxMovable: true,
cropBoxResizable: false,
toggleDragModeOnDblclick: false,
crop: updatePreview
});
}
function updatePreview() {
if (!cropper) return;
const canvas = cropper.getCroppedCanvas();
if (!canvas) return;
// Convert to JPEG with 0.7 quality
const previewUrl = canvas.toDataURL('image/jpeg', 0.7);
preview.src = previewUrl;
previewContainer.classList.remove('hidden');
// Update download link
downloadBtn.href = previewUrl;
downloadBtn.download = 'social-media-card.jpg';
}
</script>
</body>
</html>