WEB API PROGRAMMING!

web creators 2008年4月号
WEB API PROGRAMMING!:サンプルデータ

<注意書き>
※Web APIの仕様が変更されたなどの理由により、記事と動作が異なる場合があります。あらかじめご了承ください。

●●●●●サンプルデータのタイトル1

kaolabo.php

●●●●●サンプルデータ1

<?php
include_once('kaolabo_common.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift-JIS" />
<title>顔ラボ - 顔検出 Web API</title>
</head>

<body style="text-align: center;">
<div style="width: 800px; margin: 0 auto;">
<h2>顔ラボ - 顔検出 Web API を使う</h2>
<p>次の写真から選択して、「実行」ボタンを押してください。</p>
<form enctype="multipart/form-data" action="kaolabo_result.php" method="get">
<ul>
<?php foreach ($PHOTOS as $key => $val): ?>
<span>
<li style="float: left; list-style-type: none; height: 250px;">
<input type="radio" name="index" value="<?php echo $key ?>" style="margin-left: 100px; margin-bottom: 125px;" />
<span style="vertical-align: middle;"><img src="<?php echo "{$val}_m.jpg" ?>"></span>
</li>
</span>
<?php endforeach; ?>
</ul>
<br clear="both">
<input type="submit" value="実行" /></p>
</form>
</div>
<a href="http://kaolabo.com/webapi">
<img src="http://kaolabo.com/wp-content/uploads/images/banner_125_50.gif" border="0" />
</a>

</body>
</html>

●●●●●サンプルデータのタイトル2

kaolabo_common.php

●●●●●サンプルデータ2

<?php
/**
* サンプルの写真一覧
* いずれのサンプルの写真も Flickr から CreativeCommons に設定されている写真のみ
*/
$PHOTOS = array(
// http://www.flickr.com/photos/notsogoodphotography/1061598016/
'http://farm2.static.flickr.com/1119/1061598016_d4b174ab59',

// http://www.flickr.com/photos/eelssej_/439444161/
'http://farm1.static.flickr.com/182/439444161_3005bb460e',

// http://www.flickr.com/photos/jenniferwoodardmaderazo/561146390/
'http://farm2.static.flickr.com/1377/561146390_b101319430',

// http://www.flickr.com/photos/griffhome/70341004/
'http://farm1.static.flickr.com/35/70341004_acdd270d8a'
);

/**
* 顔検出 Web API に接続して、顔部分の写真を生成する
*
* $photo: 対象の写真 URL
* 戻り値: 検出結果と生成した顔部分の写真名
*/
function kaolabo_create_image($photo)
{
$photo = "{$photo}.jpg";
$results = kaolabo_api_detect($photo);
if (!$results) {
return false;
}

$results_size = count($results);
for ($i = 0; $i < $results_size; $i++) {
// 顔部分の写真を生成する
$face_name = "face_{$i}";
if (!create_image_from_range($photo, $results[$i]['face'], $face_name)) {
continue;
}

$results[$i]['face_name'] = "{$face_name}.jpg";
}

return $results;
}

/**
* 指定された範囲だけの写真を生成する
*
* $source: 対象の写真
* $range: 範囲データ (x => 'X 座標', y => 'Y 座標', width => '幅', height => '高さ')
* $new_soruce: 生成する写真のファイル名
* 戻り値: 生成結果
*/
function create_image_from_range($source, $range, $new_source)
{
static $source_width = 0, $source_height = 0;
if ($source_width === 0 && $source_height === 0) {
list($source_width, $source_height) = getimagesize($source);
}

static $image_source = null;
if ($image_source === null) {
$image_source = imagecreatefromjpeg($source);
}
if (!$image_source) {
return false;
}

$new_image = imagecreatetruecolor($range['width'], $range['height']);
imagecopy($new_image, $image_source, 0, 0, $range['x'], $range['y'],
$source_width, $source_height);
imagejpeg($new_image, "{$_SERVER['DOCUMENT_ROOT']}\\{$new_source}.jpg");
imagedestroy($new_image);
return true;
}

/**
* 顔検出 Web API に接続して、結果データを取得する
*
* $photo: 顔検出対象の写真
* 戻り値: 結果データ (http://kaolabo.com/webapi/spec のレスポンスフィード)
*/
function kaolabo_api_detect($photo)
{
$api_key = '';

$photo = urlencode($photo);
$response = http_get_request("https://kaolabo.com/api/detect?apikey={$api_key}&url={$photo}");
$xml = simplexml_load_string($response);
if (!$xml || !isset($xml->faces)) {
return false;
}

// 検出結果データ
$results = array();
foreach ($xml->faces->face as $val) {
// 顔検出結果データ
$face = array('x' => $val['x'],
'y' => $val['y'],
'width' => $val['width'],
'height' => $val['height'],
'score' => $val['score']);

// 左目検出結果データ
$left_eye = array('x' => $val->{'left-eye'}['x'],
'y' => $val->{'left-eye'}['y']);

// 右目検出結果データ
$right_eye = array('x' => $val->{'right-eye'}['x'],
'y' => $val->{'right-eye'}['y']);

$results []= array('face' => $face,
'left_eye' => $left_eye,
'right_eye' => $right_eye);
}

return $results;
}

/**
* ウェブサーバへ HTTP GET メソッドで接続する
*
* $url: リクエスト先のURL
* 戻り値: レスポンスデータ
*/
function http_get_request($url)
{
$fp = @fopen($url, "r");
if (!$fp) {
die("Problem with {$url}");
}

$response = @stream_get_contents($fp);
if ($response === false) {
die("Problem reading data from {$url}");
}

return $response;
}

●●●●●サンプルデータのタイトル3

kaolabo_result.php

●●●●●サンプルデータ3

<?php
include_once('kaolabo_common.php');

if (!isset($_GET['index'])) {
die('Could not found index.');
}

$index = $_GET['index'];
$results = kaolabo_create_image($PHOTOS[$index]);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift-JIS" />
<title>顔ラボ - 顔検出 Web API</title>
</head>

<body style="text-align: center;">
<div style="width: 800px; margin: 0 auto;">
<h2>顔ラボ - 顔検出 Web API 結果</h2>
<p>顔検出結果は、次のとおりです。</p>

<table>
<tr><td>元写真</td><td><img src="<?php echo "{$PHOTOS[$index]}_m.jpg" ?>"></td></tr>

<?php foreach ($results as $result): ?>
<tr>
<td>顔写真</td>
<td>
<img src="http://<?php echo $_SERVER['HTTP_HOST'] ?>/<?php echo $result['face_name'] ?>">
顔らしさ: <?php echo $result['face']['score'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>

</div>
<a href="kaolabo.php">トップへ戻る</a><br />
<a href="http://kaolabo.com/webapi">
<img src="http://kaolabo.com/wp-content/uploads/images/banner_125_50.gif" border="0" />
</a>

</body>
</html>

●●●●●

←BACK

Copyright (c) 2008 MdN Corporation  All rights reserved.