Skip to content

Instantly share code, notes, and snippets.

@xxxkurosukexxx
Last active August 29, 2015 14:03
Show Gist options
  • Save xxxkurosukexxx/32e216425864be70b2ee to your computer and use it in GitHub Desktop.
Save xxxkurosukexxx/32e216425864be70b2ee to your computer and use it in GitHub Desktop.
ブログ書くときに使うやつ。flickrのAlbumをWordpressに貼り付ける時に使う。
<?php
class photosetToWordress {
/** APIキー */
private $apiKey;
/** photosetのID */
private $photosetId;
/** cache消去の有無 */
private $cacheClear;
/** cacheの保存先 */
private $cacheBaseDir;
/** photosetのcache保存先ディレクトリ */
private $cachePhotosetDir;
/** photoのchache保存先ディレクトリ */
private $cachePhotoDir;
/** userのcache保存先ディレクトリ */
private $cacheUserDir;
function __construct($_apiKey, $_photosetId, $_cacheBaseDir, $_cacheClear = false) {
$this -> apiKey = $_apiKey;
$this -> photosetId = $_photosetId;
$this -> cacheClear = $_cacheClear;
$this -> cacheBaseDir = $_cacheBaseDir;
$this -> cachePhotosetDir = $this -> cacheBaseDir . "/photoset";
$this -> cachePhotoDir = $this -> cacheBaseDir . "/photo";
$this -> cacheUserDir = $this -> cacheBaseDir . "/user";
$this -> initDir();
}
/**
* html生成。
*
* @return string HTML文字列。
*/
public function createHtml() {
$htmlArr = array();
/** photoset情報取得 */
$photosetJson = $this -> getJson("/photoset/{$this->photosetId}.json", "flickr.photosets.getPhotos", array(
"photoset_id" => "{$this->photosetId}",
"extras" => "url_m"
));
/** ユーザー関連情報取得 */
$nsid = $photosetJson -> photoset -> owner;
$userJson = $this -> getJson("/user/{$nsid}.json", "flickr.people.getInfo", array("user_id" => "{$nsid}"));
$realname = $userJson -> person -> realname -> _content;
$pathAlias = $userJson -> person -> path_alias;
foreach($photosetJson->photoset->photo as $photo) {
/** レスポンス用tmpオブジェクト初期化 */
$x = array(
"Model" => null,
"LensModel" => null,
"ExposureTime" => null,
"FNumber" => null,
"ISO" => null,
"FocalLength" => null,
"CreateDate" => null
);
/** photo情報取得 */
$exifJson = $this -> getJson("/photo/{$photo->id}.json", "flickr.photos.getExif", array("photo_id" => "{$photo->id}"));
/** photo情報分解 */
// FIXME: もうちょっとスマートに出来ないか?
foreach($exifJson->photo->exif as $_exif) {
switch($_exif->tag) {
case "Model" :
$x["Model"] = $_exif -> raw -> _content;
break;
case "LensModel" :
// ここはボクの使っているレンズのモデル名が出なかったので...。
if($_exif -> raw -> _content == "28-75mm") {
$x["LensModel"] = "TAMRON SP AF 28-75mm F/2.8 XR Di LD Aspherical [IF] MACRO A09";
} else {
$x["LensModel"] = $_exif -> raw -> _content;
}
break;
case "ExposureTime" :
$x["ExposureTime"] = $_exif -> raw -> _content;
break;
case "FNumber" :
$x["FNumber"] = $_exif -> clean -> _content;
break;
case "ISO" :
$x["ISO"] = $_exif -> raw -> _content;
break;
case "FocalLength" :
$x["FocalLength"] = $_exif -> raw -> _content;
break;
case "CreateDate" :
$x["CreateDate"] = $this -> convertExifDate2FriendlyDate($_exif -> raw -> _content);
}
};
/** htmlタグ及びEXIF情報出力 */
$htmlArr[] = ("<a href=\"https://www.flickr.com/photos/{$pathAlias}/{$photo->id}\" title=\"{$photo->title} by {$realname}, on Flickr\" target=\"_blank\"><img src=\"{$photo->url_m}\" alt=\"{$photo->title}\" width=\"{$photo->width_m}\" height=\"{$photo->height_m}\" /></a>");
$htmlArr[] = ("{$x["Model"]} / {$x["LensModel"]} / {$x["FocalLength"]} / {$x["ExposureTime"]} / {$x["FNumber"]} / ISO:{$x["ISO"]} / {$x["CreateDate"]}");
$htmlArr[] = ("");
}
return implode("\n", $htmlArr);
}
/**
* json返却
* $cacheClear == true || $_filePathにファイルが存在しない場合、APIをcallする。
*
* @param $_filePath 読み込むファイルパス。ファイルが存在しない場合書込に行くのでパーミッション注意。
* @param $_method APIのMethod名
* @param $_params APIに渡すパラメータ
*
* @return json_decodeしたオブジェクト
*/
private function getJson($_filePath, $_method, $_params) {
$_filePath = $this -> cacheBaseDir . $_filePath;
if($this -> cacheClear || file_exists($_filePath) === FALSE) {
if(is_array($_params) === FALSE) {
$_params = array();
}
$_params = array_merge(array(
"method" => "{$_method}",
"api_key" => "{$this->apiKey}",
"format" => "json",
"nojsoncallback" => "1"
), $_params);
$_endPoint = "https://api.flickr.com/services/rest/?" . http_build_query($_params);
file_put_contents($_filePath, file_get_contents($_endPoint));
}
return json_decode(file_get_contents($_filePath));
}
/**
* 必要なディレクトリが存在していない場合は生成する。
* ディレクトリ生成時にエラー(パーミッション不足など)の場合は異常終了する。
*/
private function initDir() {
if(! $this -> checkDir($this -> cacheBaseDir)) {
// cacheBaseDirディレクトリが存在しないし、作成できない or 書き込めない。
throw new Exception("Directory Error : {$this -> cacheBaseDir}");
} else {
if(! $this -> checkDir($this -> cachePhotosetDir)) {
// cachePhotosetDirディレクトリが存在しないし、作成できない or 書き込めない。
throw new Exception("Directory Error : {$this -> cachePhotosetDir}");
}
if(! $this -> checkDir($this -> cachePhotoDir)) {
// cachePhotoDirディレクトリが存在しないし、作成できない or 書き込めない。
throw new Exception("Directory Error : {$this -> cachePhotoDir}");
}
if(! $this -> checkDir($this -> cacheUserDir)) {
// cacheUserDirディレクトリが存在しないし、作成できない or 書き込めない。
throw new Exception("Directory Error : {$this -> cacheUserDir}");
}
}
}
/**
* ディレクトリの存在確認と書き込み可否判定
*
* @param string $targetDir ターゲットディレクトリパス
*
* @return boolean
*/
private function checkDir($targetDir) {
if(! file_exists($targetDir) && ! mkdir($targetDir)) {
// tempディレクトリが存在しないし、作成できない
return false;
} else if(! is_writable($targetDir)) {
// tempディレクトリが存在するが書き込めない
return false;
} else {
return true;
}
}
/**
* Exifの日時フォーマットを変換する
*
* @param string $exifDate "yyyy:MM:dd HH:mm:ss"
*
* @return string "yyyy/MM/dd HH:mm:ss"
*/
private function convertExifDate2FriendlyDate($exifDate) {
return (string)preg_replace("/^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/", "$1/$2/$3 $4:$5:$6", $exifDate);
}
}
<?php
header("Content-Type: text/plain; charset=UTF-8");
clearstatcache();
require "photosetToWordpress.php";
/** ----- sysconfig ----- **/
// Flickr API key
$apiKey = "";
// Flickr Photoset(Album) ID
$photosetId = "";
// キャッシュ保存先
$cacheBaseDir = "./tmp";
// キャッシュ強制クリアフラグ
$cacheClear = false;
/** ----- sysconfig ----- **/
$p = new photosetToWordress($apiKey, $photosetId, $cacheBaseDir, $cacheClear);
echo $p -> createHtml();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment