Compare commits
4 Commits
dc83ccf8f8
...
3a0849efeb
Author | SHA1 | Date |
---|---|---|
Vic | 3a0849efeb | 2 years ago |
Vic | 28133e7c1c | 2 years ago |
Vic | 9af57a4e49 | 2 years ago |
Vic | 7f83cb6d16 | 2 years ago |
@ -1,2 +1,33 @@
|
||||
# Resize-Api
|
||||
|
||||
The resize API allow to place images into your frontend with the size set via URL parameters for rapid prototyping
|
||||
|
||||
A library to serve properly scaled versions of your images to the front end to reduce page load size
|
||||
|
||||
|
||||
# Installation
|
||||
|
||||
1- Install packages dependencies
|
||||
`npm install`
|
||||
|
||||
2- build the project
|
||||
`npm build`
|
||||
|
||||
3- run the node server
|
||||
`node ./build/index`
|
||||
|
||||
You can also run the code through the nodemon devepement server
|
||||
`npm run start`
|
||||
|
||||
When the server is running, you can resize an image by going to this url and adding parameters to resize the image `http://localhost:5000/api/image?filename=encenadaport&width=200&height=200`
|
||||
|
||||
# Built with
|
||||
|
||||
- Typescript
|
||||
- Node
|
||||
- Sharp module to resize the image
|
||||
- Express
|
||||
- Git
|
||||
|
||||
# Author
|
||||
Anis Benziane
|
||||
|
@ -1 +0,0 @@
|
||||
"use strict";
|
@ -1,24 +0,0 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = __importDefault(require("express"));
|
||||
const app = (0, express_1.default)();
|
||||
const port = 3000;
|
||||
function myFunc(num) {
|
||||
return num * num;
|
||||
}
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World' + myFunc(5));
|
||||
});
|
||||
app.get('/api', (req, res) => {
|
||||
res.send('welcome to api');
|
||||
});
|
||||
// start Express server
|
||||
app.listen(port, () => {
|
||||
console.log(`server started at http://localhost:${port}`);
|
||||
});
|
||||
exports.default = {
|
||||
myFunc
|
||||
};
|
@ -1,15 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const jasmine_spec_reporter_1 = require("jasmine-spec-reporter");
|
||||
class CustomProcessor extends jasmine_spec_reporter_1.DisplayProcessor {
|
||||
displayJasmineStarted(info, log) {
|
||||
return `TypeScript ${log}`;
|
||||
}
|
||||
}
|
||||
jasmine.getEnv().clearReporters();
|
||||
jasmine.getEnv().addReporter(new jasmine_spec_reporter_1.SpecReporter({
|
||||
spec: {
|
||||
displayStacktrace: jasmine_spec_reporter_1.StacktraceOption.NONE
|
||||
},
|
||||
customProcessors: [CustomProcessor],
|
||||
}));
|
@ -1,9 +0,0 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const index_1 = __importDefault(require("../index"));
|
||||
it('expect myFunc(5) to equal 25', () => {
|
||||
expect((0, index_1.default)(5)).toEqual(25);
|
||||
});
|
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
@ -1,10 +1,58 @@
|
||||
import express from "express"
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import sharp from "sharp"
|
||||
|
||||
const image = express.Router();
|
||||
|
||||
image.get('/', (req, res) => {
|
||||
res.send('Welcome to image route please add parametres in the url');
|
||||
const resizeImg = async (width: number, height: number, inputimgPath: string, fileName: string): Promise<string> => {
|
||||
// output file for resized img
|
||||
let outputImg = path.resolve("images/thumb", `${fileName}.jpg`);
|
||||
await sharp(path.resolve(inputimgPath))
|
||||
.resize(width, height)
|
||||
.toFormat("jpeg")
|
||||
.jpeg({
|
||||
quality: 100,
|
||||
mozjpeg: true
|
||||
})
|
||||
.toFile(path.resolve(outputImg))
|
||||
|
||||
return outputImg
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
image.get('/', async (req: express.Request, res: express.Response): Promise<void> => {
|
||||
let fileName = req.query.filename as string;
|
||||
let width = parseInt(req.query.width as string);
|
||||
let height = parseInt(req.query.height as string);
|
||||
|
||||
// fetch file from server
|
||||
let inputimgPath: string = path.resolve("images/full", `${fileName}.jpg`);
|
||||
|
||||
|
||||
// check if the inputFile exist
|
||||
if (!fs.existsSync(inputimgPath)) {
|
||||
res.status(404).send("Image not found");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// display image if we have fileName, width, height attributs in the request
|
||||
if (fileName && width && height) {
|
||||
|
||||
//resize image
|
||||
let outputImg: string = await resizeImg(width, height, inputimgPath, fileName);
|
||||
res.sendFile(outputImg);
|
||||
|
||||
} else if (fileName) {
|
||||
// display original image if we only have filename parameter in the request
|
||||
res.sendFile(inputimgPath);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
export default image;
|
Loading…
Reference in New Issue