Commit 593b1628 authored by Sachini  Gamalathge's avatar Sachini Gamalathge

Update light_imgopt_api.md

parent 818d7c3f
Pipeline #9443 passed with stage
in 1 minute and 6 seconds
# GETTING STARTED WITH THE IMAGE OPTIMISATION API
ShimmerCat’s imgopt (Image Optimization) API enables you to optimize your image by using your authentication token. There are different ways of using the API, and you can for example check out the ShimmerCat imgopt API web app
## What makes this API different?
There are many ways of transcoding images to WEBP. This API however has two key features:
* A size check: the resulting WEBP images should be of a smaller size than the submitted JPEG or PNG file.
* An image similarity metric that ensures that no visual distortions are present in the resulting image. The metric is based in a neural network trained with feedback from keen human eyes.
* Auto-scaling: thousands of API requests can be made in a very short period of time, (say, a few seconds), each with an image to be transcoded. Even under high load, an image should not take more than 30 minutes to be optimized.
* Refusal on non-viable optimization: if there is no conversion from to WEBP that preserves quality and decreases size, the API returns a deny status for that file.
Software Prerequisites
* Python2 installed on your system
* Curl installed on your system
## Prerequisites needed for an Image optimization API
* Image file Hash
* Image file encoded to base64
* ShimmerCat Authentication token
* The Domain associated with the token
* Original size of the image in bytes (optional)
* precursor_name (Must be "webp0", to denote "WEBP" Image format)
* Relative path (relative to the domain, as reported in ShimmerCat's job record)
## Collecting the prerequisites
You have many ways to encode and create the file hash image
### Using a web based tool
You can use a web based tool to get the hash of an image file https://emn178.github.io/online-tools/sha1.html and to encode an image to base64 https://base64.guru/converter/encode/file
### Using Python Script
The Python script automates the creation of the hash and the encoding process for you using Python libraries.
You will see more about using the script for hashing, encoding and optimizing the images in the following section.
### Linux CLI commands
You can use the following utilities to encode and hash
* md5sum for hashing using the md5sum hash function
* base64 for encoding/decoding data
## Getting into action
Finally after collecting the prerequistes, You are ready to optimize your image
You have 3 ways for submitting a request to imgotpt API
### Using the Python script
Download the script from our [public repository](https://gitlab.zunzun.se/public-items/imgopt-example-scripts/tree/master/light-api), open it and start editing the following variables
Assign the domain, image_file_path, token variables and relative_path to your variables respectively according to your case
Run the following commands
```
$ python2 send_image_to_optimize.py
```
Output
```
{"status": "queued"}
```
Next run the following to check the status
```
$ python2 check_optimization_status.py
```
Output
```
{"success_or_deny": "processing"}
```
### Using Curl
If you are familiar with Curl and you like working with the SHELL then you can go executing the following commands
```
curl -X POST "https://accelerator.shimmercat.com/imgopt3/2020.1/light/optimized/$image_file_hash/" -H "Authorization: Token $token Domain $domain" -H 'Content-type: application/json' -d
{
"shimmercat_job_record": {
"task_thread": "$image_file_hash"
},
"image_file_hash": "$image_file_hash",
"relative_path": "$relative_path",
"improve_image_quality": false,
"original_image_size_in_bytes": $size,
"domains": [
"$domain"
],
"precursor_name": "webp0",
"expected_optimized_image_filename": "somewhere/data",
"file": "$img_base64",
"force_reprocessing": true
}
```
You can now remove the `force_reprocessing: true` option and run the Curl command once again to get the status of the image
### Using Postman
We maintain Postman collections with examples of invocation of the API. You can find all of that in our public repository. These collections show examples of the behavior of the API under different scenarios.
So you can import an example and start editing the values and create post requests
Note: This requires knowledge with how to make API calls using postman
## Analyze the output
When you submit an Image for optimization or you check an already recieved request, you will get one of the following responses.
* success: This value is used to denote an image body.
* deny: This value is used to denote a deny for this particular file due to technical limitations... more precisely, it means we weren't able to reduce the file's size
* expecting-file: This value means that we haven't received the contents of the file.
* processing: This value means that your image is under processing by our API
* presigned_url: A URL you can simply get to download the opimtized image. This value while only appear if the Image is optimized successfully alongside with the success field.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment