Compare commits

...

4 Commits

2
.gitignore vendored

@ -41,7 +41,7 @@ bower_components
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
build/
# Dependency directories
node_modules/

@ -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

@ -36,8 +36,8 @@
"typescript": "^4.5.4"
},
"dependencies": {
"axios": "^0.25.0",
"express": "^4.17.2",
"path": "^0.12.7",
"sharp": "^0.29.3"
}
}

@ -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;

@ -74,7 +74,7 @@
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"strict": false, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
@ -98,5 +98,5 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["node_modules", "dist", "spec"]
"exclude": ["node_modules", "build", "spec"]
}

Loading…
Cancel
Save