Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizilla API #402

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions New_APIs/Optimizilla API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Optimizilla Image Compressor</title>
</head>
<body>
<header>
<h1>Optimizilla Image Compressor</h1>
</header>
<main>
<section>
<form id="image-form" enctype="multipart/form-data">
<input type="file" id="image" accept="image/*" required>
<button type="submit">Compress Image</button>
</form>
<div id="status"></div>
<div id="result">
<h2>Compressed Image</h2>
<img id="compressed-image" src="" alt="Compressed Image">
</div>
</section>
</main>
<script src="index.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions New_APIs/Optimizilla API/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
document.getElementById('image-form').addEventListener('submit', async (e) => {
e.preventDefault();

const formData = new FormData();
formData.append('image', document.getElementById('image').files[0]);

try {
const response = await fetch('/compress-image', {
method: 'POST',
body: formData
});

const data = await response.json();

if (data.success) {
document.getElementById('compressed-image').src = data.imageUrl;
document.getElementById('status').textContent = 'Image compressed successfully!';
} else {
document.getElementById('status').textContent = 'Failed to compress image.';
}
} catch (error) {
console.error('Error:', error);
document.getElementById('status').textContent = 'An error occurred while compressing the image.';
}
});
37 changes: 37 additions & 0 deletions New_APIs/Optimizilla API/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions New_APIs/Optimizilla API/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "optimizilla-image-compressor",
"version": "1.0.0",
"description": "A simple app to compress images using the Optimizilla API",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"multer": "^1.4.5",
"node-fetch": "^2.6.1",
"body-parser": "^1.19.0"
}
}

49 changes: 49 additions & 0 deletions New_APIs/Optimizilla API/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const express = require('express');
const multer = require('multer');
const fetch = require('node-fetch');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');

const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());

const upload = multer({ dest: 'uploads/' });

app.post('/compress-image', upload.single('image'), async (req, res) => {
const filePath = path.join(__dirname, 'uploads', req.file.filename);
const fileStream = fs.createReadStream(filePath);

try {
const response = await fetch('https://api.optimizilla.com/v1/compress', {
method: 'POST',
body: fileStream,
headers: {
'Content-Type': 'multipart/form-data'
}
});

const result = await response.json();

if (result.success) {
// Remove the uploaded file
fs.unlinkSync(filePath);

res.json({
success: true,
imageUrl: result.compressedImageUrl
});
} else {
console.error('Optimizilla API error:', result.error);
res.json({ success: false });
}
} catch (error) {
console.error('Error compressing image:', error);
res.status(500).json({ success: false });
}
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
72 changes: 72 additions & 0 deletions New_APIs/Optimizilla API/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #3189a4, #0e2e9f);
color: #333;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}

header {
text-align: center;
margin-bottom: 20px;
}

header h1 {
font-size: 2.5em;
color: #fff;
}

main {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 300px;
}

form {
display: flex;
flex-direction: column;
gap: 10px;
}

input[type="file"] {
padding: 10px;
font-size: 1em;
}

button {
padding: 10px;
font-size: 1em;
color: #fff;
background-color: #8625b7;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #f6d365;
}

#status {
margin-top: 10px;
font-weight: bold;
color: #fda085;
}

#result {
margin-top: 20px;
text-align: center;
}

#compressed-image {
max-width: 100%;
height: auto;
margin-top: 10px;
}
Loading