1

This is response come from api

apim-request-id: a6ac620f-8484-4bdf-a9ed-d26080186769
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Mon, 24 Jun 2019 09:10:23 GMT
Content-Length: 49368
Content-Type: image/jpeg

Here,The code of call api

const express = require("express");
const app = express();
const http = require("http").Server(app).listen(8080);
const fs=require("fs");
const request = require('request');

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = 'key';

const uriBase =
    'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';

const imageUrl =
    'https://smworld.co.in/images/bg1.jpg';

// Request parameters.
const params = {
    'width': '300',
    'height': '300',
    'smartCropping': 'true'
};

const options = {
    uri: uriBase,
    qs: params,
    body: '{"url": ' + '"' + imageUrl + '"}',
    headers: {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key' : subscriptionKey
    }
};
volvereabhi
  • 50
  • 13
  • 1
    You can possibly convert response bytes to base64 and return an image tag in the HTML page that contains the image data as shown [here](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html). – Wiktor Zychla Jun 24 '19 at 11:07
  • it's not working – volvereabhi Jun 25 '19 at 09:15
  • @AbhiSavadiya The description of your question is too simple, I don't know how to help you. Could you post more details about your title, and post some necessary code in NodeJS? Please refer to the SO help topic https://stackoverflow.com/help/how-to-ask to update your post. Thanks. – Peter Pan Jun 25 '19 at 09:25
  • @peterpan now see is this okay ? – volvereabhi Jun 25 '19 at 09:31

1 Answers1

2

I tried to test the solution in @Wiktor Zychla comment, but it only works for png image, not jpeg. So I tried to give my solution to embed an image url into a static html content.

Here is my sample code as reference for you.

var request = require('request');

const subscriptionKey = 'key';

const uriBase = 'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';

const imageUrl = 'https://smworld.co.in/images/bg1.jpg';

// Request parameters.
const params = {
    'width': '300',
    'height': '300',
    'smartCropping': 'true'
};

const options = {
    uri: uriBase,
    qs: params,
    body: '{"url": ' + '"' + imageUrl + '"}',
    headers: {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key' : subscriptionKey
    }
};

const express = require('express')
const app = express()

app.get('/img', function(req, res) {
    request.post(options).pipe(res)
})

// embed an image url `/img` into the html code
app.get('/', function (req, res) {
        var html = '<h2>Hello world</h2></br><img src="/img"/>'
        res.send(html)

})

app.listen(3000)

The result of the above code as below run on my local env.

enter image description here

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43