Image to URLh
Image to URLh
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Upload to Imgbb</title>
<style>
.upload-box {
background: white;
padding: 25px;
border-radius: 15px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 500px;
text-align: center;
}
.drop-zone {
border: 2px dashed #c3c7d1;
border-radius: 10px;
padding: 40px 20px;
cursor: pointer;
color: #666;
transition: all 0.3s;
margin-bottom: 20px;
}
.drop-zone.dragover {
border-color: #007bff;
background-color: #f0f8ff;
}
.output {
margin-top: 20px;
word-break: break-all;
background: #f9f9f9;
padding: 15px;
border-radius: 5px;
text-align: left;
}
button {
margin: 10px 5px;
padding: 10px 15px;
border: none;
background: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #0056b3;
}
button.secondary {
background: #6c757d;
}
button.secondary:hover {
background: #5a6268;
}
.preview-container {
margin: 15px 0;
text-align: center;
}
.preview-image {
max-width: 100%;
max-height: 300px;
border-radius: 5px;
border: 1px solid #ddd;
}
.progress-container {
width: 100%;
background-color: #e9ecef;
border-radius: 5px;
margin: 15px 0;
display: none;
}
.progress-bar {
width: 0%;
height: 20px;
background-color: #28a745;
border-radius: 5px;
transition: width 0.3s;
text-align: center;
color: white;
line-height: 20px;
font-size: 12px;
}
.url-container {
background: #f1f1f1;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
word-break: break-all;
}
.share-buttons {
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 10px;
margin-top: 15px;
}
.success-message {
color: #28a745;
font-weight: bold;
margin: 10px 0;
}
.error-message {
color: #dc3545;
font-weight: bold;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="upload-box">
<h2>Image Uploader</h2>
<p>Drag & drop or click to upload images</p>
<script>
const dropZone = document.getElementById('dropZone');
const dropZoneContent = document.getElementById('dropZoneContent');
const fileInput = document.getElementById('fileInput');
const output = document.getElementById('output');
const previewContainer = document.getElementById('previewContainer');
const resultMessage = document.getElementById('resultMessage');
const urlContainer = document.getElementById('urlContainer');
const shareButtons = document.getElementById('shareButtons');
const progressContainer = document.getElementById('progressContainer');
const progressBar = document.getElementById('progressBar');
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
function handleFileUpload(file) {
if (!file || !file.type.startsWith('image/')) {
showError("Only image files are allowed.");
return;
}
// Show preview
const reader = new FileReader();
reader.onload = (e) => {
previewContainer.innerHTML = `<img class="preview-image" src="$
{e.target.result}" alt="Preview">`;
};
reader.readAsDataURL(file);
// Simulate progress (since we can't track actual upload progress with imgbb
API)
let progress = 0;
const progressInterval = setInterval(() => {
progress += Math.random() * 10;
if (progress > 90) progress = 90;
updateProgress(progress);
}, 200);
if (data.success) {
const imageUrl = data.data.url;
const deleteUrl = data.data.delete_url;
shareButtons.innerHTML = `
<button onclick="copyToClipboard('${imageUrl}')">Copy URL</button>
<button class="secondary" onclick="copyToClipboard('$
{deleteUrl}')">Copy Delete URL</button>
<button class="secondary"
onclick="shareImage('${imageUrl}')">Share</button>
<button onclick="window.open('${imageUrl}', '_blank')">View
Image</button>
`;
} else {
showError("Upload failed. " + (data.error ? data.error.message : "Please
try again."));
}
})
.catch(err => {
clearInterval(progressInterval);
showError("Upload error. " + err.message);
console.error(err);
});
}
function updateProgress(percent) {
progressBar.style.width = `${percent}%`;
progressBar.textContent = `${Math.round(percent)}%`;
}
function showError(message) {
output.style.display = 'block';
resultMessage.innerHTML = `<div class="error-message">❌ ${message}</div>`;
urlContainer.innerHTML = '';
shareButtons.innerHTML = '';
progressContainer.style.display = 'none';
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert("Copied to clipboard!");
}).catch(err => {
console.error("Could not copy text: ", err);
// Fallback for older browsers
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
alert("Copied to clipboard!");
});
}
function shareImage(url) {
if (navigator.share) {
navigator.share({
title: 'Check out this image',
text: 'I uploaded this image using Imgbb',
url: url
}).catch(err => {
console.log('Error sharing:', err);
window.open(`https://www.facebook.com/sharer/sharer.php?u=$
{encodeURIComponent(url)}`, '_blank');
});
} else {
// Fallback for browsers that don't support Web Share API
window.open(`https://www.facebook.com/sharer/sharer.php?u=$
{encodeURIComponent(url)}`, '_blank');
}
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
</script>
</body>
</html>