|
<?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;
}
|