This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
/
improvement_img.php
executable file
·103 lines (93 loc) · 2.86 KB
/
improvement_img.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
header('Content');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
require_once 'config_api.php';
$data = [
'client_id'=>'mcs9883751818.ml.vision.jFW25ocYnpgdK4jpRSF5',
'client_secret'=>'4MBG481H7kAW2bY2Jyx5mbWtqnoKeTsai5wFqAEoi9bKovDHjiDfMPnkAKrn3Q',
'grant_type'=>'client_credentials'
];
$url = "https://smarty.mail.ru/api/v1/persons/recognize?oauth_provider=mcs&oauth_token=EeMGiQ3qah52CKHt3DwJZ1ZBQbfi7dp9JsV4fFHeZsqBbPfz3";
$path = 'http://pictures.govoru.com/news/2013/11/27/8676/da75219e.jpg';
$meta = '{"space":"1", "images":[{"name":"http://pictures.govoru.com/news/2013/11/27/8676/da75219e.jpg"}], "create_new":false}';
$filenames = array($path);
$files = array();
foreach ($filenames as $f)
{
if (strlen($f) == 0)
{
# just to make correct multipart/form-data request
$files[$f] = 'fake content';
}
else
{
$files[$f] = file_get_contents($f);
}
}
//$post_data = http_build_query($data);
$fields = array("meta"=> $meta);
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$curl = curl_init();
$post_data = build_data_files($boundary, $fields, $files);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
#CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data),
),
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
//echo "code: ${info['http_code']}\n";
//var_dump($response);
curl_close($curl);
$json = json_decode($response);
$info = $json->body->objects[0];
$info = $info->persons;
//print_r($info);
$i =0;
$person = array('person1'=>'Джесика','person2'=>'Кристина');
foreach($info as $item){
//print_r($item);
$arr[$i]['coord'] = $item->coord;
if(!empty($person[$item->tag])){
$arr[$i]['tag'] = $person[$item->tag];
}else{
$arr[$i]['tag'] = $item->tag;
}
$i++;
}
echo json_encode($arr);
function build_data_files($boundary, $fields, $files)
{
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields as $name => $content)
{
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
foreach ($files as $name => $content)
{
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
. 'Content-Type: image/jpeg'.$eol
. 'Content-Transfer-Encoding: binary'.$eol
;
$data .= $eol;
$data .= $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
?>