<!DOCTYPE html>
<html>
<head>
<title>URL to Image with Download Button</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/downloadjs/1.4.8/download.min.js"></script>
<style>
<style>
/* Style for the form container */
.form-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 20px;
padding: 20px;
background-color: #f2f2f2;
border-radius: 5px;
}
.form-container h1 {
margin: 0 0 20px 0;
text-align: center;
font-size: 2em;
}
/* Style for the form elements */
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
label {
margin: 10px;
}
input {
padding: 10px;
border: 2px solid #ccc;
border-radius: 5px;
width: 100%;
max-width: 500px;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px;
cursor: pointer;
border-radius: 5px;
}
button:hover {
background-color: #3e8e41;
}
/* Style for the image container */
.image-container {
display: flex;
justify-content: center;
align-items: center;
margin: 20px;
}
/* Style for the image */
.image-container img {
max-width: 100%;
max-height: 100%;
}
/* Style for the download button container */
.download-button-container {
display: flex;
justify-content: center;
align-items: center;
margin: 20px;
}
</style>
</head>
<body>
<div class="form-container">
<h1>URL to Image with Download Button</h1>
<form>
<label for="image-url">Image URL:</label>
<input type="text" id="image-url" />
<button type="button" onclick="previewImage()">Preview Image</button>
</form>
</div>
<div class="image-container" id="image-preview-container" style="display: none;">
<img id="image-preview" alt="Preview Image" />
</div>
<div class="download-button-container" id="download-button-container" style="display: none;">
<button type="button" onclick="downloadImage()">Download Image</button>
</div>
<script>
function previewImage() {
// Get the image URL from the user input
const imageUrl = document.getElementById("image-url").value;
// Set the <img> element's source to the image URL and show the image container
const imagePreview = document.getElementById("image-preview");
imagePreview.src = imageUrl;
const imagePreviewContainer = document.getElementById("image-preview-container");
imagePreviewContainer.style.display = "flex";
// Show the download button container
const downloadButtonContainer = document.getElementById("download-button-container");
downloadButtonContainer.style.display = "flex";
}
function downloadImage() {
const imageUrl = document.getElementById("image-preview").src;
const link = document.createElement("a");
link.download = "image.jpg";
link.href = imageUrl;
link.click();
}
</script>
</body>
</html>
Comments
Post a Comment