Skip to content

Commit 6a6736c

Browse files
committed
Version 0.1 of Tag recognition
* In the picture modification page, we can generate tag with an image * Add Imagga for image recognition api
1 parent b1cb3a2 commit 6a6736c

40 files changed

Lines changed: 1968 additions & 2 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# piwigo-auto-tag
2-
Piwigo plugin to suggest tags on image (using external API)
1+
# AutoTag
2+
Piwigo plugin to suggest tags on image (using external API).

admin.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
defined('COLOR_PALETTE_PATH') or die('Hacking attempt!');
3+
4+
global $template, $page, $conf;
5+
6+
include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
7+
8+
$page['tab'] = (isset($_GET['tab'])) ? $_GET['tab'] : $page['tab'] = 'config';
9+
10+
$tabsheet = new tabsheet();
11+
$tabsheet->add('config', '<span class="tr-icon-robot"></span>'.l10n('Configuration'), TR_ADMIN.'-config');
12+
$tabsheet->select($page['tab']);
13+
$tabsheet->assign();
14+
15+
if (isset($_POST['use'])||isset($_POST['save']))
16+
{
17+
check_pwg_token();
18+
if (in_array($_POST['api'], TR_API_LIST))
19+
{
20+
$newConf = tr_getConf();
21+
22+
if (isset($_POST['use']))
23+
{
24+
$newConf->setSelectedAPI($_POST['api']);
25+
}
26+
27+
$apiObject = tr_getAPI($_POST['api']);
28+
foreach ($apiObject->getParams() as $key => $value) {
29+
$newConf->setParam($_POST['api'], $key, $_POST[$key]);
30+
}
31+
tr_setConf($newConf);
32+
}
33+
}
34+
35+
$tr_api_info = [];
36+
$tr_api_params = [];
37+
$tr_api_conf = [];
38+
39+
foreach (TR_API_LIST as $apiName) {
40+
$apiObject = tr_getAPI($apiName);
41+
$tr_api_info[$apiName] = $apiObject->getInfo();
42+
$tr_api_params[$apiName] = $apiObject->getParams();
43+
$tr_api_conf[$apiName] = tr_getConf()->getConf($apiName);
44+
}
45+
46+
47+
48+
$template->assign(array(
49+
'TR_PATH' => TR_PATH,
50+
'TR_API_LIST' => TR_API_LIST,
51+
'TR_API_INFO' => $tr_api_info,
52+
'TR_API_PARAMS' => $tr_api_params,
53+
'TR_API_CONF' => $tr_api_conf,
54+
'TR_API_SELECTED' => tr_getConf()->getSelectedAPI(),
55+
'PWG_TOKEN' => get_pwg_token()
56+
));
57+
58+
$template->set_filename('plugin_admin_content', realpath(TR_PATH . 'template/admin.tpl'));
59+
$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
60+

api_classes/Imagga.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
class Imagga extends API {
4+
5+
function getInfo() : array
6+
{
7+
return [
8+
"icon" => 'https://imagga.com/static/images/logo.svg',
9+
"site" => 'https://imagga.com/',
10+
"color" => '#3DBCB2',
11+
"info" => `
12+
Imagga is a computer vision artificial intelligence company. Imagga Image Recognition API features auto-tagging, auto-categorization, face recognition, visual search, content moderation, auto-cropping, color extraction, custom training and ready-to-use models. Available in the Cloud and on On-Premise. It is currently deployed in leading digital asset management solutions and personal cloud platforms and consumer facing apps.
13+
`
14+
];
15+
}
16+
17+
function getParams() : array
18+
{
19+
return [
20+
'USER' => 'API Key',
21+
'USER_PASSWORD'=> 'API Secret'
22+
];
23+
}
24+
25+
function getRemainingRequest($params) : int
26+
{
27+
$api_credentials = array(
28+
'key' => $params['USER'],
29+
'secret' => $params['USER_PASSWORD']
30+
);
31+
32+
$ch = curl_init();
33+
34+
curl_setopt($ch, CURLOPT_URL, 'https://api.imagga.com/v2/usage');
35+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
36+
curl_setopt($ch, CURLOPT_HEADER, FALSE);
37+
curl_setopt($ch, CURLOPT_USERPWD, $api_credentials['key'].':'.$api_credentials['secret']);
38+
39+
$response = curl_exec($ch);
40+
curl_close($ch);
41+
42+
$json_response = json_decode($response);
43+
44+
return $json_response->result->monthly_limit - $json_response->result->monthly_processed;
45+
}
46+
47+
function generateTags($params) : array
48+
{
49+
$file_path = $this->getFileName($params['imageId']);
50+
51+
$api_credentials = array(
52+
'key' => $params['USER'],
53+
'secret' => $params['USER_PASSWORD']
54+
);
55+
56+
$type = pathinfo($file_path, PATHINFO_EXTENSION);
57+
$data = file_get_contents($file_path);
58+
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
59+
60+
$ch = curl_init();
61+
62+
curl_setopt($ch, CURLOPT_URL, "https://api.imagga.com/v2/tags");
63+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
64+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
65+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
66+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
67+
curl_setopt($ch, CURLOPT_USERPWD, $api_credentials['key'].':'.$api_credentials['secret']);
68+
curl_setopt($ch, CURLOPT_HEADER, FALSE);
69+
curl_setopt($ch, CURLOPT_POST, 1);
70+
$fields = [
71+
'image_base64' => $base64,
72+
'language' => $params['language'],
73+
'limit' => $params['limit'],
74+
];
75+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
76+
77+
78+
if (curl_errno($ch))
79+
{
80+
return [curl_error($ch)];
81+
}
82+
83+
$response = curl_exec($ch);
84+
curl_close($ch);
85+
86+
$json_response = json_decode($response);
87+
88+
$tags = [];
89+
90+
foreach ($json_response->result->tags as $tagObject)
91+
{
92+
$tagObjectArray = json_decode(json_encode($tagObject), true);
93+
array_push($tags, $tagObjectArray["tag"][$params['language']]);
94+
}
95+
96+
return $tags;
97+
}
98+
}

api_types.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
define('TR_API_LIST', ['Imagga']);
4+
5+
abstract class API
6+
{
7+
abstract function getInfo() : array ;
8+
9+
abstract function getParams() : array ;
10+
11+
abstract function getRemainingRequest($conf) : int;
12+
13+
abstract function generateTags($params) : array;
14+
15+
function getFileName($imageId) {
16+
$query = '
17+
SELECT path
18+
FROM '.IMAGES_TABLE.'
19+
WHERE id = '.((int)$imageId).'
20+
;';
21+
$result = query2array($query);
22+
return $result[0]['path'];
23+
}
24+
}
25+
26+
foreach (TR_API_LIST as $apiName) {
27+
include_once(TR_PATH.'api_classes/'.$apiName.'.php');
28+
}

conf.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
class TR_Conf
4+
{
5+
6+
var $selected = "Imagga";
7+
8+
var $conf = [];
9+
10+
function getSelectedAPI()
11+
{
12+
return $this->selected;
13+
}
14+
15+
function setSelectedAPI($api)
16+
{
17+
if (in_array($api,TR_API_LIST))
18+
$this->selected = $api;
19+
}
20+
21+
function getConf($api)
22+
{
23+
if (in_array($api,TR_API_LIST) && array_key_exists($api, $this->conf))
24+
return $this->conf[$api];
25+
return [];
26+
}
27+
28+
function getParam($api, $param)
29+
{
30+
if (in_array($api,TR_API_LIST) && array_key_exists($api, $this->conf))
31+
return $this->conf[$api][$param];
32+
return [];
33+
}
34+
35+
function setParam($api, $param, $value)
36+
{
37+
38+
if (in_array($api,TR_API_LIST))
39+
{
40+
if(!array_key_exists($api, $this->conf))
41+
$this->conf[$api] = [];
42+
$this->conf[$api][$param] = $value;
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)