問題描述
我有一個自定義帖子型別,可以接受 YouTube 嵌入 HTML 片段,標題和精選圖片。目前,我正在尋找特色影像的影像,但理想情況下,我可以根據影片網址自動下載和調整影片本身的第一幀,在儲存過程中的影片。如果做得正確,我的帖子型別可能不需要連結,可以從中獲取影像和嵌入程式碼。
例如,如果影片連結是 http://www.youtube.com/watch?v=2Jnpi-uBiIg,則 v 的值將被提取並用於以 http://img.youtube.com/vi/2Jnpi-uBiIg/0.jpg 下載影像。
我對於 WordPress 的開發非常新鮮,但是有些東西告訴我,我會研究一下鉤子 (如果我正確理解這些) 。
最佳解決方法
嗨 @Jonathan Sampson:
雖然這並不完全是你所要求的,但實際上可能是一個可行的解決方案,它可以免費從 WordPress.com 感謝 @yoast 的推文,他今天上午引用了這篇博文:
基本上你可以使用 WordPress.com 的截圖生成器 URL 在這種形式 (根據部落格文章 Matt 似乎保佑免費使用):
-
https://s.wordpress.com/mshots/v1/{URL-encoded URL}?w={width}
所以從上面拿你的 URL:
然後 URL 編碼,所以你可以使用截圖生成器,你會得到一個這樣的 URL(寬度為 400px):
https://s.wordpress.com/mshots/v1/http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2Jnpi-uBiIg?w=400
當然,接下來是螢幕截圖如何 (我複製了後代的螢幕截圖,所以即使修改了 WordPress 服務也不會改變,而不是直接連結到服務來顯示影像。) 不知道為什麼它拍攝影片而不是整個頁面,但影片更好的為您想要的:
當然,第一次 HTTP GET 請求 URL 時,它不會返回螢幕截圖,因為 WordPress.com 的伺服器需要首先捕獲螢幕截圖,這需要太多時間讓您的 HTTP GET 請求等待。所以首先請求你的 HTTP GET 將被重定向到這個 URL:
-
https://s.wordpress.com/wp-content/plugins/mshots/default.gif
該網址顯示此圖片:
但是,如果您在第一個 HTTP GET 請求後等待一分鐘,並再次發出請求,您會發現您的螢幕截圖。我的想法是,你想要做的是呼叫它來快取它,等待下載它,然後在本地快取在自己的伺服器上,以儘量減少 WordPress.com 的伺服器上的負載,所以他們不會重新思考提供免費的 (或者如果有足夠的流量,他們甚至可以提供它作為付費服務,也可能新增付費 API 功能!)
附:順便說一下,為了證明它在網頁中實際工作的是直接從 WordPress.com 請求的截圖。請注意,它可能與上面儲存和連結的螢幕截圖不同,或者已經有一段時間,因為有人看過這個頁面,因此他們的快取清楚,甚至可能是”Generating Preview” 影像。如果是這樣,請稍等片刻,然後重新整理此頁面,並返回:
次佳解決方法
我的影片部落格生成器 (http://v.leau.co/) 這樣做但不是在 wp 上下文中。
您提供查詢”superman”(然後等待 (沒有注意到它正在做某事)(因為唯一的使用者)),然後單擊你想釋出的影片,點選生成程式碼,你有程式碼,我的網站上託管的大拇指,因為它下載這些在同一時間。該程式碼可以複製並貼上到一個帖子中。
換句話說,如果你將程式碼放在一個函式呼叫中,它會返回一段程式碼,例如具有指向被新增到內容的影片的連結的 href 。指向本地下載的特色影像的連結。
這是你正在尋找的程式碼片段?我認為核心是:
檢索更多結果的功能 (如果您只想在最終的程式碼中顯示超過 1 個影片,而不是 1 個特定)
function retrieveMoreResults($key, $q, $start, $cache) {
$url = "http://googleajax.admincdn.com/ajax/services/search/video?v=1.0&q=" . $q . "&rsz=large&start=" . $start. "&key=" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($body, true);
$tempOutputString .= display($responseData, $cache, $q);
return $tempOutputString;
}
獲取初始結果頁的功能:
function retrieveResults($key, $q, $cache) {
# first call
$url = "http://googleajax.admincdn.com/ajax/services/search/video?v=1.0&q=" . $q . "&rsz=large&key=" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($body, true);
$tempOutputString = "";
$tempOutputString .= display($responseData, $cache, $q);
$boolFirstRequest = true;
foreach ($responseData["responseData"]["cursor"]["pages"] as $GsearchResultClass) {
$start = $GsearchResultClass["start"];
if ($boolFirstRequest) {
$boolFirstRequest = false;
} else {
$tempOutputString .= retrieveMoreResults($key, $q, $start, $cache);
}
}
return $tempOutputString;
}
一個功能來顯示在某個目錄 (變數) 中下載縮圖並返回一段程式碼放入帖子中:
function display($responseData, $cache, $tag) {
$strBuffer="";
foreach ($responseData["responseData"]["results"] as $GsearchResultClass) {
#
# there are YouTube urls and also Google Video urls they are both different
# the one from Google video has the word "ThumbnailServer" in it
# example:
# youtube: http://1.gvt0.com/vi/6jKzr143K8U/default.jpg
# video.google: http://3.gvt0.com/ThumbnailServer2?app=vss&contentid=7efbd69963e4cc67&offsetms=30000&itag=w160&hl=en&sigh=J6N1fv_It6H5jJWX51fKt-eYqNk
#
$thumbId="";
$imageThumb=$GsearchResultClass["tbUrl"];
if (strstr($imageThumb, 'ThumbnailServer')) {
$imgStringBits = explode('&',$imageThumb);
$parsedImgStr=strstr($imgStringBits[1],'=');
$parsedImgStr = substr($parsedImgStr,1);
$thumbId = $parsedImgStr;
} else {
$imgStringBits = explode('/',$imageThumb);
$thumbId = $imgStringBits[4];
}
$imgFile=$cache . "/" . $thumbId . ".jpg";
#
# Now that we have the imageFile Name check if we already have it in the cache:
# - if we have it NEVER delete it (why should we?)
# - if we dont well... get it via curl
#
if (!file_exists($imgFile)) {
$ch = curl_init ();
$timeout = 5;
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0');
curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
curl_setopt ($ch, CURLOPT_URL, $imageThumb);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt ($ch, CURLOPT_VERBOSE, 1);
$rawdata = curl_exec ($ch);
curl_close ($ch);
if($rawdata) {
$fp = fopen($imgFile, 'w');
fwrite($fp, $rawdata);
fclose($fp);
} else {
#
# If our Curl action retrieves nothing then use the default image
#
$imgfile="images/default.jpg";
}
}
#
# Now that we have the image url create a div (hmm.. might drop that
# styling later on in a seperate csss) containg the video thumb, link
# and the description. When you like it you can add all the other
# parameters that Google returns like length etc...
#
$strBuffer .= "<div style="float:left;width:125px;height:130px;font-size:8px;font-family:arial;" class="thumb"><div>";
$strBuffer .= "<a href="#" class="del" id="$thumbId"> ADD</a><br />";
$strBuffer .= "<a href="" . $GsearchResultClass["playUrl"] . "" target="_blank">";
$strBuffer .= "<img src="" . $imgFile . "" alt="" . $GsearchResultClass["title"] . "" border="0" width="120">";
$strBuffer .= "</a><br />n";
#
# Note that we also add a delete option, for now that only removes it from the page
# but in the next version it should do an AJAX call to write the id somewhere so
# that we never see it again.
#
$strBuffer .= $GsearchResultClass["titleNoFormatting"] . "<br />";
$strBuffer .= "</div></div>n";
}
return $strBuffer;
}
呼叫上述功能:
function moviePage($tag, $cacheName, $cacheTime, $key) {
$cache = $cacheName . "/" . $tag;
checkCache($cache);
cleanCacheHTML($cache);
$filename = $tag . ".html";
$spFile=$cache . "/" . $filename;
if (file_exists($spFile) && filemtime($spFile) > $cacheTime ) {
$strBuffer = file_get_contents($spFile) . "<!-- " . $spFile . " from cache -->";
} else {
$strBuffer = retrieveResults($key, $tag, $cache);
}
$strBuffer .= "<br clear="all">";
$fp = fopen($spFile, 'w');
fwrite($fp, $strBuffer);
fclose($fp);
return $strBuffer;
}
($ key 是您的 Google API 金鑰)(http://code.google.com/intl/nl-NL/more/)
IMHO 我認為其餘的只是 「獲取返回的東西,並將其新增到帖子的內容+將下載的快取縮圖設定為特色?
附:引用 YouTube 影片時,IMHO 總是會在影片中釋出縮圖,因為 YouTube 會刪除較舊的影片,因此 YouTube 會出現醜陋的貼子。用你自己的拇指,至少影像永遠在那裡,所以以後你確實有一個線索你最近釋出的東西。
第三種解決方法
我不知道我是否明白了你的意思,但是我發現了這個解決方法:
http://wpworks.wordpress.com/2010/12/23/display-youtube-thumbnail-with-wordpress-custom-field/
最好的祝福
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。


