Wednesday, April 11, 2018

Tutorial: Resize an image and save it to a posting on AmiDb

The ability to resize and add images to postings is ground 0 feature for most apps and websites.

In this example, I will use AmiDb and show you how easy it to resize an image, save it to a posting and view the results.

Note: This example can be done entirely over JavaScript.

Step 1) Let us first create a model representing a posting.

POST URL: https://www.amidb.com/lgc/service/model/save/posting

Payload JSON:
    {
    "pscope": "O",
    "fields":
    {
        "ska":
        {
            "name": "posting_id",
            "type": "integer"
        },  
        "cola":
        {
            "name": "title"
        }
    },
    "storage":
    {
        "engine": "Cassandra"
    }  
}

Response:
{
  "n": "3n",
  "success": true,
  "modelname": "posting",
  "result": {
    "size": 1,
    "entry": [
      {
        "pscope": "O",
        "_metasysggmdl": {
          "_ak": "299f786a830c9a8bb4624d2159a4d344"
        },
        "storage": {
          "engine": "Cassandra"
        },
        "fields": {
          "ska": {
            "name": "posting_id",
            "type": "integer"
          },
          "cola": {
            "name": "title"
          }
        }
      }
    ]
  },
  "http_status": 200
}

 
Step 2) Upload an image from your phone or browser to the Resize API.
 
MultiPart POST URL: https://www.amidb.com/lgc/service/image/convert/png
 
Request Payload: 
Form-Data: Name:"convertopts", Value:"-resize 100x100"
File-Data: The image file 

Response:
{
  "n":"3n",
  "success":true,
  "result":{
     "size":1,
     "entry":[
      {"_metasysfilestage":
        {
         "_ak":"5e633c9ff2b0f8085cccad9d8cf8f1f2"
        },
        "stagekey":"5e633c9ff2b0f8085cccad9d8cf8f1f2",
        "_exts":1523442481023,
        "fstageid":8476561119032
      }
     ]
   },
   "http_status":200
}


Step 3) Create your posting and attach the stagekey to it.
 
POST URL: https://www.amidb.com/lgc/service/mcot/save/posting

Payload JSON:
Here we attach the stagekey to position0, variant0 (_perupv00).
 {
    "data":[
        {
            "posting_id":"1",
            "title":"My first posting with an image",
            "_perupv00":"5e633c9ff2b0f8085cccad9d8cf8f1f2",
            "_peruname0":"img.png"
        }
    ]
 }

Response:
{
  "n": "3n",
  "success": true,
  "result": {
    "size": 1,
    "entry": [
      {
        "posting_id": 1,
        "title": "My first posting with an image",
        "_metaposting": {
          "_ak": "5b1b3f67777235c3242923dc1ef23006"
        },
        "_peruname0": "img.png"
      }
    ]
  },
  "http_status": 200
} 
 
--------------------- 
SUMMARY:
With a few steps I have demonstrated how you can upload an image, 
resize it and attach it to your first posting. The attachment key of 
the posted row can be used to view the posting itself and its images.
 
To view the posting:
GET: https://www.amidb.com/lgc/service/mcot/get/posting/5b1b3f67777235c3242923dc1ef23006 

To view the image: 
GET: https://www.amidb.com/lgc/service/file/download/posting/5b1b3f67777235c3242923dc1ef23006/_perupv00
Here you refer the image's name by the standard naming 
convention _perupv00 (position0, variant0)

---------------------
Thank you for reading and hope you will try it out for yourself!

No comments:

Post a Comment