Best laravel framework open-source packages.

Laravel image manipulation api

A Laravel-based REST API for resizing images with user-defined dimensions (width and height)
Updated 3 months ago

Image Manipulation API - Laravel Application

The Image Manipulation REST API is a Laravel application that provides a fast and secure way to resize images based on user-defined dimensions. This API utilizes Laravel's Sanctum authentication to ensure secure access to its features. Users can send an image to the API, along with the desired dimensions (width and height), and receive the resized image in return.

Screenshots:

Image Manipulation API Collection on my Postman profile:

image-manipulation-api-my-postman-profile

Database design:

image_manipulation_api database design

Creating an account to generate a Sanctum Access Token:

image-manipulation-api-homepage

sanctum-access-tokens

create-sanctum-access-token

Features:

1- Token-based Authentication using Laravel Sanctum package.

2- Using Laravel API Resources and Resource Collection classes.

3- Using Form Request classes for complex validation.

4- Rate Limiting to restrict the number of requests to the API per minute.

5- Using Regular Expression with the resizing API endpoints.

6- Laravel Breeze package.

7- Intervention Image package for image manipulation.

8- Route Model Binding.

Application Routes and API Endpoints:

All the application routes and API Endpoints are defined in both web.php (registration and login routes to get an acess token) and api.php (API endpoints) files.

Installation & Configuration:

1- Open your terminal, and use the 'git clone https://github.com/AhmedYahyaE/laravel-image-manipulation-api.git' command, or just download the ZIP project.

2- Navigate/Change into (using the cd command) to the project root directory, then run the 'composer install' command.

3- Run the 'npm install' command (and only in case you face any issues/errors, run the 'npm audit fix' command), and then run the 'npm run build' command.

4- Create a MySQL database named `image_manipulation_api`, then import the image_manipulation_api database SQL Dump File into your `image_manipulation_api` database.

5- Navigate to the .env file and configure/update it with your MySQL database credentials and other configuration settings.

6- Run the 'php artisan serve' command.

7- To get an access token to be able to use the API, open your browser and visit http://127.0.0.1:8000 to access Image Manipulation API application, then click on Register, then after completing the registration process, click on 'Create new token' to get a new access token, then give a name to the token, then click on 'Generate', then copy your new access token to paste it in your HTTP Client (like Postman).

** Here are the credentials for a ready-to-use registered account that you can use to log in:

Email: ahmed.yahya@example.com, Password: 123456

8- Open your HTTP Client such as Postman, create a new Collection, name it 'Image Manipulation API'. Click on your Imgae Manipulation API Collection, then go to 'Variables' tab, then create a variable with the value of the Access Token that you've copied when you registered your account (example: my Access Token), then click 'Save'. Go to the 'Authorization' tab, in the 'Type' drop-down menu, choose 'Bearer Token', then in the 'Token' field, enter (use) your Access Token 'Variable' name that you've created (example: {{my Access Token}}). This way you can inherit this Access token in ALL your requests to the API in this Collection.

9- Add your first request, then go to 'Authorization' tab, click on 'Inherit auth from parent'.

10- In your request, go to the 'Headers' tab and add two headers: 'Accept': 'application/json' and 'Content-Type': 'application/json'. (Note: This step is a must with ALL your requests to the Image Manipulation API.)

** Check the API Collection on my Postman Profile: https://www.postman.com/ahmed-yahya/workspace/my-public-portfolio-postman-workspace/collection/28181483-12ef8a00-7826-4bc6-b57a-016f48546432

** Also, you can test the API Endpoints using Postman. Here is the API's Postman Collection .json file Postman Collection file you can download and import in your Postman.

Note:

For ALL your requests to the Image Manipulation API, you must add the following three Headers:

a- 'Authorization' Header with the value of the Access Token you have obtained when you registered your account. The type of the Access Token is 'Bearer Token', so the Access Token value of the 'Authorization' Header must have the following format: 'Bearer xxxxxxxxxxxxxxxxxxx'. Example: Bearer 15|9BwhVAKORFQxbREqeeY8SPZTmrdWZ2382iNhBoPF

b- 'Accept' Header with the value of 'application/json'.

c- 'Content-Type' Header with the value of 'application/json'.

API Endpoints:

1- Create an album (POST):

POST /api/v1/album

  • "name" key, which is your album name, must be provided in your JSON HTTP Request body.

2- Get all albums (that ONLY belong to the authenticated/logged-in user) (GET):

GET /api/v1/album

3- Get a Single album By ID (that belongs to the authenticated/logged-in user) (GET):

GET /api/v1/album/{album_id}

  • {album_id} must be provided as a URL Parameter.

4- Update an album (that belongs to the authenticated/logged-in user) (PUT):

PUT /api/v1/album/{album_id}

  • {album_id} must be provided as a URL Parameter.
  • "name" key, which is your updated album name, must be provided in your JSON HTTP Request body.

5- Delete an album (that belongs to the authenticated/logged-in user) (DELETE):

DELETE /api/v1/album/{album_id}

  • {album_id} must be provided as a URL Parameter.

6- Get All images (that ONLY belong to the authenticated/logged-in user) (GET):

GET /api/v1/image

7- Get a Single image By ID (that belongs to the authenticated/logged-in user) (GET):

GET /api/v1/image/{image_id}

  • {image_id} must be provided as a URL Parameter.

8- Get images By Album (that belong to the authenticated/logged-in user) (GET):

GET /api/v1/image/by-album/{album_id}

  • {album_id} must be provided as a URL Parameter.

9- Delete an image (that belongs to the authenticated/logged-in user) (DELETE):

DELETE /api/v1/image/{image_id}

  • {image_id} must be provided as a URL Parameter.

10- Resize an image By URL with percentages % (POST):

POST /api/v1/image/resize?w={number}%&h={number}%

Example: POST /api/v1/image/resize?w=50%&h=70%

  • 'w' (width) query string parameter is required, 'h' (height) query string parameter is optional.
  • "image" key with the value of the URL/link of your image must be provided in your JSON HTTP Request body.
  • "album_id" key with the value of the album ID that you want to associate your image with can be optionally (not required) provided in your JSON HTTP Request body.

11- Resize an image By URL with px (POST):

POST /api/v1/image/resize?w={number}&h={number}

Example: POST /api/v1/image/resize?w=100&h=120

  • 'w' (width) query string parameter is required, 'h' (height) query string parameter is optional.
  • "image" key with the value of the URL/link of your image must be provided in your JSON HTTP Request body.
  • "album_id" key with the value of the album ID that you want to associate your image with can be optionally (not required) provided in your JSON HTTP Request body.

12- Resize an image By Upload with percentages % (POST):

POST /api/v1/image/resize?w={number}%&h={number}%

Example: POST /api/v1/image/resize?w=50%&h=70%

  • 'w' (width) query string parameter is required, 'h' (height) query string parameter is optional.
  • Upload your image as follows (required): Click on the 'Body' tab, then click on 'form-data'. In the 'Key' field, write in 'image' and then click on the drop-down menu of the 'image' Key field and change 'Text' to 'File', then in the 'Value' field, select your image to be uploaded.
  • Enter your album ID as follows (optional, not required): Click on the 'Body' tab, then click on 'form-data'. In the 'Key' field, enter 'album_id', then in the 'Value' field, enter your album ID number that you want to associate your image with.

13- Resize an image By Upload with px (POST):

POST /api/v1/image/resize?w={number}&h={number}

Example: POST /api/v1/image/resize?w=100&h=120

  • 'w' (width) query string parameter is required, 'h' (height) query string parameter is optional.

  • Upload your image as follows (required): Click on the 'Body' tab, then click on 'form-data'. In the 'Key' field, write in 'image' and then click on the drop-down menu of the 'image' Key field and change 'Text' to 'File', then in the 'Value' field, select your image to be uploaded.

  • Enter your album ID as follows (optional, not required): Click on the 'Body' tab, then click on 'form-data'. In the 'Key' field, enter 'album_id', then in the 'Value' field, enter your album ID number that you want to associate your image with.

Contribution:

Contributions to my Image Manipulation REST API Laravel application are most welcome! If you find any issues or have suggestions for improvements or want to add new features, please open an issue or submit a pull request.