
以前、Wordpressのプラグイン「Auto post Thumbnail」を改造してYoutubeの動画からアイキャッチ画像を生成する方法を紹介しましたが、それを拡張してVimeoからもアイキャッチが作成できるようにしてみました。
方法は前回と同じ。auto-post-thumbnail.phpの中から以下の記述の部分を見つけます。
// Get all images from post's body
preg_match_all('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $post[0]->post_content, $matches);
中央よりやや後ろ、apt_publish_postというFunctionの中にあります。見つけたら、その後ろに以下の記述を追加します。
if (empty($matches[0])) {
preg_match('%(?:player.vimeo\.com\/video\/)([^"&?/ ]{8})|(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $post[0]->post_content, $match);
if (!empty($match[1])) {
$imgid = $match[1];
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php"));
$matches=array();
$matches[0]=$matches[1]=array($hash[0]['thumbnail_large']);
}
if (!empty($match[2])) {
$matches=array(); $matches[0]=$matches[1]=array('http://i3.ytimg.com/vi/'.$match[2].'/hqdefault.jpg');
}
}
あんまりスマートではないですが、動いているので良しとしよう。
この方法だとプラグインがアップデートするたびに再修正しなければいけないので、根本的にはやっり有料版のAuto Post Thumbnail PROを使うのがいいのでしょうね。

