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+ }
0 commit comments