An API for processing images implemented as part of the Advanced Web Development Track offered by egFWD Initiative through Udacity.
This project was structured from scratch, with ZERO starter files.
To use this project, you need to follow the commands below:
-
Clone the repository into your local machine:
git clone https://github.com/ibrahimelmokhtar/ts-image-processing-api.git
-
Redirect inside the cloned repository:
cd ts-image-processing-api/
-
Install the required packages:
npm install
-
For applying configured styling, run the following commands:
-
Prettier styling:
npm run prettier
-
ESLint styling:
npm run lint
-
-
For working on the development phase, run the following commands:
-
Live debugging while development:
npm run server
-
Jasmine Testing:
npm run test
-
-
For working with the production phase, run the following commands:
-
Build the project:
npm run build
Then, Run the compiled server:
node build/server.js
-
OR simply, Start the server with one command:
npm run start
-
-
Open the local website on
http://127.0.0.1:5000/api/{processType}?{queryParameters}
, more information about {processType} and {queryParameters} will be explained in API Endpoints
(Back to top) This section will explain how the code works and how everything is put together.
This project has the structure shown below:
├─── images/
├─── encenadaport.jpg
├─── fjord.jpg
├─── icelandwaterfall.jpg
├─── palmtunnel.jpg
├─── santamonica.jpg
├─── spec/
├─── support/
├─── jasmine.json
├─── src/
├─── middlewares/
├─── validator.middleware.ts
├─── modules/
├─── dir-exists.ts
├─── img-collect-names.ts
├─── img-create-name.ts
├─── img-exists.ts
├─── process-grayscale.ts
├─── process-negative.ts
├─── process-resize.ts
├─── process-threshold.ts
├─── routes/
├─── api/
├─── grayscale.route.ts
├─── negative.route.ts
├─── resize.route.ts
├─── threshold.route.ts
├─── index.ts
├─── schemas/
├─── grayscale.schema.ts
├─── negative.schema.ts
├─── resize.schema.ts
├─── threshold.schema.ts
├─── server/
├─── server.config.ts
├─── tests/
├─── helpers/
├─── reporter.ts
├─── dir-exists.spec.ts
├─── img-collect-names.spec.ts
├─── img-create-name.spec.ts
├─── img-exists.spec.ts
├─── server.spec.ts
├─── server.ts
├─── .eslintrc
├─── .gitignore
├─── .prettierrc
├─── package.json
├─── README.md
├─── tsconfig.json
This API has FOUR endpoints using the GET
method as explained below:
/api/resize?filename={value}&width={value}&height={value}
The user can pass a specific image name, desired width and desired height. After query validation, this API will use these parameters to apply resize
operation on this image. queryParameters
explained:
filename
: specific image to be processed, with some specifications:- pass
filename
only, without any file extension. - the image MUST be in the
images/
directory.
- pass
width
: desired width of the processed image, with some specifications:- pass a number between 100 and 1000
height
: desired height of the processed image, with some specifications:- pass a number between 100 and 1000
For example:
http://127.0.0.1:5000/api/resize?filename=encenadaport&width=500&height=800
This request will return an image encenadaport_resize_w500_h800
with .jpg
as the file extension.
/api/grayscale?filename={value}
The user can pass a specific image name. After query validation, this API will use this parameter to apply grayscale
operation on this image. queryParameters
explained:
filename
: specific image to be processed. with some specifications:- pass
filename
only, without any file extension. - the image MUST be in the
images/
directory.
- pass
For example:
http://127.0.0.1:5000/api/grayscale?filename=fjord
This request will return an image fjord_grayscale
with .jpg
as the file extension.
/api/threshold?filename={value}&threshold={value}
The user can pass a specific image name and desired threshold level. After query validation, this API will use these parameters to apply threshold
operation on this image. queryParameters
explained:
filename
: specific image to be processed, with some specifications:- pass
filename
only, without any file extension. - the image MUST be in the
images/
directory.
- pass
threshold
: desired threshold level of the processed image, with some specifications:- pass a number between 0 and 255
For example:
http://127.0.0.1:5000/api/threshold?filename=palmtunnel&threshold=200
This request will return an image palmtunnel_threshold_th200
with .jpg
as the file extension.
/api/negative?filename={value}
The user can pass a specific image name. After query validation, this API will use this parameter to apply negative
operation on this image. queryParameters
explained:
filename
: specific image to be processed. with some specifications:- pass
filename
only, without any file extension. - the image MUST be in the
images/
directory.
- pass
For example:
http://127.0.0.1:5000/api/negative?filename=santamonica
This request will return an image santamonica_negative
with .jpg
as the file extension.
-
Place images to be processed inside
images/
directory with.jpg
format.- NOTE:
- The server will figure out the available images automatically.
- Currently available images: encenadaport, fjord, icelandwaterfall, palmtunnel, and santamonica.
- NOTE:
-
Call one of the available API endpoints with its query parameters.
-
Apply query validation rules on the called request.
- If validation failed:
- Send specific error back based on the failure type.
- If validation failed:
-
Construct processed image filename depending on the applied process.
-
Check
out/
directory existence.- If not found:
- Create an empty
out/
directory.
- Create an empty
- If not found:
-
Check the existence of a previously processed image with the same query parameters.
- if found:
- no additional processing will be applied.
- if not found:
- apply the desired processing operation on the original image.
- Store processed image at
out/
directory.
- if found:
-
Send the processed image back as a response to the called request.
These packages are required to run this project smoothly without any errors.
These packages can be found in the "dependencies"
object inside the package.json
file.
- express - Fast, unopinionated, minimalist web framework.
- express-validator - Express middleware for the validator module.
- sharp - High-performance Node.js image processing.
These packages can be found in the "devDependencies"
object inside the package.json
file.
- typescript - TypeScript is a language for application scale JavaScript development.
- ts-node - TypeScript execution environment and REPL for node.js, with source map support.
- nodemon - Simple monitor script for use during the development of a node.js app.
- prettier - Prettier is an opinionated code formatter.
- eslint - An AST-based pattern checker for JavaScript.
- eslint-config-prettier - Turns off all rules that are unnecessary or might conflict with Prettier.
- eslint-plugin-prettier - Runs prettier as an eslint rule.
- jasmine - CLI for Jasmine, a simple JavaScript testing framework for browsers and Node.
- jasmine-spec-reporter - Spec reporter for jasmine behavior-driven development framework.
- supertest - SuperAgent driven library for testing HTTP servers.
- Documentation: Prettier Options
- Documentation: ESLint Configuring Rules
- Documentation: Jasmine Module
- Documentation: Express-Validator Module
- Documentation: Sharp Module
- Youtube Video: Use Express-Validator Module with NodeJs
- Youtube Video: Use Sharp Module with NodeJs
- Article: How to use Code Blocks within Markdown Files