2016-06-23 29 views
1

我想使用Azure Computer Vision API爲我的Wordpress網站生成縮略圖。我試圖使它在PHP與wp_remote_post工作,但我不知道如何解析參數?它會返回縮略圖,質量很差,默認爲500x500px。有關如何解決此問題的任何想法?在php中生成縮略圖,發佈到Azure計算機視覺API

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']; 
    }  
} 

編輯1

感謝您@Gary吧!現在裁剪是正確的,但是我的質量存在很大問題!我正在使用試用版,但是我看不到Azure的降級試用用戶的拇指質量信息。他們聲稱要提供高質量的縮略圖,但如果這是標準,那麼它就完全沒用。 我一定忽略了一些我猜的東西?

當然Gary,如果我在質量問題上得不到正確的答案,我會用您的答案關閉線程,以確保正確。

Generated thumbSource picture

+0

你有沒有嘗試到'smartCropping'設置爲false。 –

回答

2

據的Get Thumbnail的描述中,widthheightsmartCropping應設置爲應在URL合併請求參數。

但是wp_remote_post()中的第二個參數不接受URL parameters,並且對它們不做任何處理。因此,您需要先將網址合併到wp_remote_post()之前。

你可以嘗試使用add_query_arg()到您的網址第一組合,

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