Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit e30003b

Browse files
committed
Merge pull request #8 from redorb8/imageAPICalls
added URLGetRankedImageKeywords to php SDK
2 parents 1dbee65 + 8a7ad08 commit e30003b

File tree

5 files changed

+215
-1
lines changed

5 files changed

+215
-1
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,51 @@ To use each function, you must first create an AlchemyAPI object. The easiest wa
5757

5858
You can now use this object to access AlchemyAPI's text analysis functions.
5959

60+
### Image Keywords ###
61+
62+
The Image Keywords API tags an image identified by a URL or image data posted in the body of an html request. For more high-level information on AlchemyAPI's Image keywords API, please visit: http://www.alchemyapi.com/products/features/image-tagging. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/image-tagging.
63+
64+
To extract entities, use:
65+
66+
<?php
67+
$response = $alchemyapi->image_keywords(FLAVOR, DATA, OPTIONS);
68+
?>
69+
70+
Where FLAVOR can be 'url' or 'image' DATA is your url or uri-argument encoded image data, and OPTIONS is an array containing the optional parameters to modify the behavior of the call.
71+
72+
73+
**Options**
74+
75+
The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call.
76+
77+
- imagePostMode -> (only applicable to image flavor)
78+
- not-raw : pass an unencoded image file with "image=URI_ENCODED_DATA"
79+
- raw : pass an unencoded image file using POST
80+
- extractMode ->
81+
- always-infer : (more CPU intensive, more accurate)
82+
- trust-metadata : (less CPU intensive, less accurate) (default)
83+
- only-metadata : (even less CPU intensive, less accurate)
84+
85+
**Parsing**
86+
87+
To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the keyword and score of each tagged image:
88+
89+
<?php
90+
$response = $alchemyapi->image_keywords('url','http://www.lolcats.com/images/u/08/50/lolcatsdotcomur5dhkw464f8hb16.jpg', null);
91+
foreach ($response['imageKeywords'] as $imagekeyword) {
92+
echo 'keyword: ', $imagekeyword['text'], PHP_EOL;
93+
echo 'score: ', $imagekeyword['score'], PHP_EOL;
94+
}
95+
?>
96+
97+
This should print out:
98+
99+
keyword: cat
100+
score: 0.999697
101+
keyword: kitten
102+
score: 0.942676
103+
104+
60105

61106

62107
### Entities ###

alchemyapi.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,54 @@ public function AlchemyAPI() {
9999
$this->_ENDPOINTS['combined']['url'] = '/url/URLGetCombinedData';
100100
$this->_ENDPOINTS['combined']['text'] = '/text/TextGetCombinedData';
101101
$this->_ENDPOINTS['image']['url'] = '/url/URLGetImage';
102+
$this->_ENDPOINTS['image_keywords']['url'] = '/url/URLGetRankedImageKeywords';
103+
$this->_ENDPOINTS['image_keywords']['image'] = '/image/ImageGetRankedImageKeywords';
102104
$this->_ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy';
103105
$this->_ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy';
104106
$this->_ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy';
105107
}
106108

107109

110+
111+
/**
112+
* Returns tag for an image URL or image included in the body of the http request.
113+
* For an overview, please refer to: http://www.alchemyapi.com/products/features/image-tagging/
114+
* For the docs, please refer to: http://www.alchemyapi.com/api/image-tagging/
115+
*
116+
* INPUT:
117+
* flavor -> which version of the call, i.e. url or image.
118+
* image -> the image to analyze, either the url or image data.
119+
* options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
120+
*
121+
* Available Options:
122+
* imagePostMode -> (only applicable to image flavor)
123+
* not-raw : pass an unencoded image file with "image=URI_ENCODED_DATA"
124+
* raw : pass an unencoded image file using POST
125+
* extractMode ->
126+
* always-infer : (more CPU intensive, more accurate)
127+
* trust-metadata : (less CPU intensive, less accurate) (default)
128+
* only-metadata : (even less CPU intensive, less accurate)
129+
*
130+
* OUTPUT:
131+
* The response, already converted from JSON to a PHP object.
132+
*/
133+
public function image_keywords($flavor, $image, $options) {
134+
//Make sure this request supports the flavor
135+
if (!array_key_exists($flavor, $this->_ENDPOINTS['image_keywords'])) {
136+
return array('status'=>'ERROR','statusInfo'=>'Image tagging for ' . $flavor . ' not available');
137+
}
138+
139+
//Add the image to the options and analyze
140+
if($flavor=='url'){
141+
$options[$flavor] = $image;
142+
return $this->analyze($this->_ENDPOINTS['image_keywords'][$flavor], $options);
143+
}
144+
else{
145+
return $this->analyzeImage($this->_ENDPOINTS['image_keywords'][$flavor], $options, $image);
146+
}
147+
}
148+
149+
108150
/**
109151
* Extracts the entities for text, a URL or HTML.
110152
* For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/
@@ -694,6 +736,30 @@ private function analyze($endpoint, $params) {
694736
//Create the HTTP header
695737
$header = array('http' => array('method' => 'POST','header'=>'Content-Type: application/x-www-form-urlencode', 'content'=>http_build_query($params)));
696738

739+
//Fire off the HTTP Request
740+
try {
741+
$fp = @fopen($url, 'rb',false, stream_context_create($header));
742+
$response = @stream_get_contents($fp);
743+
fclose($fp);
744+
return json_decode($response, true);
745+
} catch (Exception $e) {
746+
return array('status'=>'ERROR', 'statusInfo'=>'Network error');
747+
}
748+
}
749+
//Use to create request for image API
750+
private function analyzeImage($endpoint, $params, $imageData) {
751+
752+
753+
//Add the API Key and set the output mode to JSON
754+
$params['apikey'] = $this->_api_key;
755+
$params['outputMode'] = 'json';
756+
757+
//Insert the base URL
758+
$url = $this->_BASE_URL . $endpoint . '?' . http_build_query($params);
759+
760+
//Create the HTTP header
761+
$header = array('http' => array('method' => 'POST','header'=>'Content-Type: application/x-www-form-urlencode', 'content'=>$imageData));
762+
697763
//Fire off the HTTP Request
698764
try {
699765
$fp = @fopen($url, 'rb',false, stream_context_create($header));

example.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
$demo_url = 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen';
2424
$demo_html = '<html><head><title>PHP Demo | AlchemyAPI</title></head><body><h1>Did you know that AlchemyAPI works on HTML?</h1><p>Well, you do now.</p></body></html>';
2525

26-
2726
echo PHP_EOL;
2827
echo PHP_EOL;
2928
echo ' , ', PHP_EOL;
@@ -49,7 +48,74 @@
4948
echo ' :~ ', PHP_EOL;
5049

5150

51+
echo PHP_EOL;
52+
echo PHP_EOL;
53+
echo '############################################', PHP_EOL;
54+
echo '# Image Keyword Example #', PHP_EOL;
55+
echo '############################################', PHP_EOL;
56+
echo PHP_EOL;
57+
echo PHP_EOL;
58+
59+
echo 'Processing Image URL: ', $demo_url, PHP_EOL;
60+
echo PHP_EOL;
61+
62+
$response = $alchemyapi->image_keywords('url', $demo_url, array('extractMode'=>'trust-metadata'));
63+
64+
if ($response['status'] == 'OK') {
65+
echo '## Response Object ##', PHP_EOL;
66+
echo print_r($response);
67+
68+
echo PHP_EOL;
69+
echo '## Image Keywords ##', PHP_EOL;
70+
foreach ($response['imageKeywords'] as $imageKeywords) {
71+
echo 'image keyword: ', $imageKeywords['text'], PHP_EOL;
72+
echo 'score: ', $imageKeywords['score'], PHP_EOL;
73+
echo PHP_EOL;
74+
}
75+
} else {
76+
echo 'Error in the image keyword extraction call: ', $response['statusInfo'];
77+
}
78+
echo PHP_EOL;
79+
echo PHP_EOL;
80+
echo PHP_EOL;
81+
echo PHP_EOL;
82+
/*
83+
$imageName = "grumpy-cat-meme-hmmm.jpg";
84+
$imageFile = fopen($imageName, "r") or die("Unable to open file!");
85+
$imageData = fread($imageFile,filesize($imageName));
86+
fclose($imageFile);
87+
88+
89+
echo PHP_EOL;
90+
echo PHP_EOL;
91+
echo '############################################', PHP_EOL;
92+
echo '# Image Keyword Example with image #', PHP_EOL;
93+
echo '############################################', PHP_EOL;
94+
echo PHP_EOL;
95+
echo PHP_EOL;
96+
97+
echo 'Processing Image File: ', $imageName, PHP_EOL;
98+
echo PHP_EOL;
99+
100+
$response = $alchemyapi->image_keywords('image', $imageData, array('imagePostMode'=>'raw'));
52101
102+
if ($response['status'] == 'OK') {
103+
echo '## Response Object ##', PHP_EOL;
104+
echo print_r($response);
105+
106+
echo PHP_EOL;
107+
echo '## Image Keywords ##', PHP_EOL;
108+
foreach ($response['imageKeywords'] as $imageKeywords) {
109+
echo 'image keyword: ', $imageKeywords['text'], PHP_EOL;
110+
echo 'score: ', $imageKeywords['score'], PHP_EOL;
111+
echo PHP_EOL;
112+
}
113+
} else {
114+
echo 'Error in the image keyword extraction call: ', $response['statusInfo'];
115+
}
116+
echo PHP_EOL;
117+
echo PHP_EOL;*/
118+
echo PHP_EOL;
53119
echo PHP_EOL;
54120
echo PHP_EOL;
55121
echo '############################################', PHP_EOL;

grumpy-cat-meme-hmmm.jpg

112 KB
Loading

tests.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,44 @@
2323
$test_text = 'Bob broke my heart, and then made up this silly sentence to test the PHP SDK';
2424
$test_html = '<html><head><title>The best SDK Test | AlchemyAPI</title></head><body><h1>Hello World!</h1><p>My favorite language is PHP</p></body></html>';
2525
$test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0';
26+
$imageName = "grumpy-cat-meme-hmmm.jpg";
27+
$imageFile = fopen($imageName, "r") or die("Unable to open file!");
28+
$imageData = fread($imageFile,filesize($imageName));
29+
fclose($imageFile);
2630

31+
//image keywords
32+
echo 'Checking image keywords . . . ', PHP_EOL;
33+
$response = $alchemyapi->image_keywords('url', $test_url, null);
34+
assert($response['status'] == 'OK');
35+
$response = $alchemyapi->image_keywords('image', $imageData, array('imagePostMode'=>'raw'));
36+
assert($response['status'] == 'OK');
37+
$response = $alchemyapi->image_keywords('random', $test_url, null);
38+
assert($response['status'] == 'ERROR'); //invalid flavor
39+
echo 'Image keyword tests complete!', PHP_EOL, PHP_EOL;
40+
41+
//image extraction
42+
echo 'Checking image extraction . . . ', PHP_EOL;
43+
$response = $alchemyapi->imageExtraction('url',$test_url, null);
44+
assert($response['status'] == 'OK');
45+
$response = $alchemyapi->imageExtraction('random', $test_url, null);
46+
assert($response['status'] == 'ERROR'); //invalid flavor
47+
echo 'Image extraction tests complete!', PHP_EOL, PHP_EOL;
48+
49+
//taxonomy
50+
echo 'Checking Taxonomy . . . ', PHP_EOL;
51+
$response = $alchemyapi->taxonomy('text',$test_text, null);
52+
assert($response['status'] == 'OK');
53+
$response = $alchemyapi->taxonomy('random', $test_text, null);
54+
assert($response['status'] == 'ERROR'); //invalid flavor
55+
echo 'Taxonomy tests complete!', PHP_EOL, PHP_EOL;
56+
57+
//combined
58+
echo 'Checking Combined . . . ', PHP_EOL;
59+
$response = $alchemyapi->combined('text',$test_text, null);
60+
assert($response['status'] == 'OK');
61+
$response = $alchemyapi->combined('random', $test_text, null);
62+
assert($response['status'] == 'ERROR'); //invalid flavor
63+
echo 'Combined tests complete!', PHP_EOL, PHP_EOL;
2764

2865
//Entities
2966
echo 'Checking entities . . . ', PHP_EOL;

0 commit comments

Comments
 (0)