Generate thumbnail in php, posting to Azure Comput

2019-07-25 04:23发布

问题:

I want to use Azure Computer Vision API to generate thumbnails for my Wordpress site. I'm trying to make it work in php with wp_remote_post, but i don't know how to parse the parameters ? It returns a thumbnail in really bad quality and default 500x500px. Any ideas on how to resolve this issue ?

function get_thumbnail($URL)   //* * * * Azure Computer Vision API - v1.0 * * * *
{
$posturl='https://api.projectoxford.ai/vision/v1.0/generateThumbnail'; 

$request = wp_remote_post($posturl, array(
 'headers' => array(
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'),
 'body' => array('url' => $URL)
));

if ( is_wp_error( $request ) ) 
{
    $error_message = $request->get_error_message();
    return "Something went wrong: $error_message";
    } else 
    {
      return $request['body'];
    }    
}

EDIT 1

Thanks @Gary your right! Now the cropping is correct, but i got a huge problem with the quality! I'm using a trial but i see no info from Azure on downgrading the thumb quality for trial users. They are claiming to deliver high quality thumbnails, but if thats the standard it's totaly useless. I must have overlooked something i guess?

Of course Gary, if i get no correct answer on my quality question i will close the thread with your answer as correct.

回答1:

According the description of Get Thumbnail, the width,height and smartCropping should be set as request parameters which should combined in URL.

However the second args in wp_remote_post() do not accept the URL parameters and will do nothing on them. So you need to combine the url first before set into wp_remote_post().

You can try to use add_query_arg() to combine your url first,

$posturl='https://api.projectoxford.ai/vision/v1.0/generateThumbnail';
$posturl=add_query_arg( array(
    'width' => 600,
    'height' => 400,
    'smartCropping' => true
), $posturl);