はじめに
プラグイン Exif Details を公開しましたが、その応用を記します。
- 撮影日時をフィルター
- 投稿時にメディアライブラリのキャプションに撮影データ挿入
- 撮影データ内に、Google Map へのリンクを入力
動作
まずは、どのようになるか以下の動画をご覧ください。
コード
/** ==================================================
* オリジナルフィルターフック('exif_details_data'),
* EXIF を取得してメタデータに保存する際の表示を変更する。
* 以下は、撮影日時の表示を変更します。
*
* @param array $exifdatas exifdatas.
* @param int $id id.
*/
function exif_details_change( $exifdatas, $id ) {
if ( array_key_exists( 'DateTimeOriginal', $exifdatas ) ) {
$shooting_date = str_replace( ':', '-', substr( $exifdatas['DateTimeOriginal'], 0, 10 ) );
$shooting_time = substr( $exifdatas['DateTimeOriginal'], 10 );
$exifdatas['DateTimeOriginal'] = $shooting_date . $shooting_time;
}
return $exifdatas;
}
add_filter( 'exif_details_data', 'exif_details_change', 10, 2 );
/** ==================================================
* メディア新規追加時に、キャプションに加工したデータを挿入します。
* 関数内でオリジナルアクションフック('exif_details_update')を使用します。
*
* @param array $metadata metadata.
* @param int $id id.
*/
function media_caption( $metadata, $id ) {
$mime_type = get_post_mime_type( $id );
if ( in_array( $mime_type, array( 'image/jpeg', 'image/tiff' ) ) ) {
do_action( 'exif_details_update', $id );
$exifdatas = get_post_meta( $id, '_exif_details', true );
if ( ! empty( $exifdatas ) ) {
$camera = null;
$f_number = null;
$s_speed = null;
$iso = null;
$date = null;
$googlemap = null;
if ( array_key_exists( 'Model', $exifdatas ) ) {
$camera = 'カメラ:' . $exifdatas['Model'];
}
if ( array_key_exists( 'ApertureFNumber', $exifdatas ) ) {
$f_number = 'F値:' . $exifdatas['ApertureFNumber'];
}
if ( array_key_exists( 'ExposureTime', $exifdatas ) ) {
$s_speed = 'シャッタースピード:' . $exifdatas['ExposureTime'];
}
if ( array_key_exists( 'ISOSpeedRatings', $exifdatas ) ) {
$isodata = json_decode( $exifdatas['ISOSpeedRatings'] );
if ( is_array( $isodata ) ) {
$iso = 'ISO:' . $isodata[0];
} else {
$iso = 'ISO:' . $isodata;
}
}
if ( array_key_exists( 'DateTimeOriginal', $exifdatas ) ) {
$date = '撮影日時:' . $exifdatas['DateTimeOriginal'];
}
if ( array_key_exists( 'latitude_dd', $exifdatas ) && array_key_exists( 'longtitude_dd', $exifdatas ) ) {
$googlemap = '<a href="https://www.google.com/maps?q=' . $exifdatas['latitude_dd'] . ',' . $exifdatas['longtitude_dd'] . '">Google Map</a>';
}
$caption = sprintf( '%1$s %2$s %3$s %4$s %5$s %6$s', $camera, $f_number, $s_speed, $iso, $date, $googlemap );
$caption = rtrim( $caption );
$caption = preg_replace( '/\s(?=\s)/', '', $caption );
$media_post = array(
'ID' => $id,
'post_excerpt' => $caption,
);
wp_update_post( $media_post );
}
}
return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'media_caption', 10, 2 );