PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/example/AviaryFX.php

https://github.com/aviaryapi/AviaryFxPhp
PHP | 344 lines | 235 code | 23 blank | 86 comment | 27 complexity | 9a5c87fe02ee24e14747e36ab3f87bc9 MD5 | raw file
  1. <?php
  2. /**
  3. * AviaryFX PHP SDK
  4. *
  5. * @version 1.0
  6. * @author Bruce Drummond
  7. * @package AviaryFX
  8. */
  9. class AviaryFX
  10. {
  11. const VERSION = "0.2";
  12. const PLATFORM = "html";
  13. const HARDWARE_VERSION = "1.0";
  14. const SOFTWARE_VERSION = "PHP";
  15. const APP_VERSION = "1.0";
  16. const SERVER = "http://cartonapi.aviary.com/services";
  17. const GET_TIME_URL = "/util/getTime";
  18. const GET_FILTERS_URL = "/filter/getFilters";
  19. const UPLOAD_URL = "/ostrich/upload";
  20. const RENDER_URL = "/ostrich/render";
  21. protected $api_key;
  22. protected $api_secret;
  23. /**
  24. * Constructor
  25. *
  26. * @param string $api_key
  27. * @param string $api_secret
  28. */
  29. public function __construct($api_key, $api_secret)
  30. {
  31. $this->api_key = $api_key;
  32. $this->api_secret = $api_secret;
  33. }
  34. /**
  35. * Gets a list of available filters.
  36. *
  37. * @return array Array of filters with label, uid, description and parameter list for each filter.
  38. */
  39. public function getFilters()
  40. {
  41. $api_key = $this->api_key;
  42. $api_secret = $this->api_secret;
  43. $version = self::VERSION;
  44. $platform = self::PLATFORM;
  45. $hardware_version = self::HARDWARE_VERSION;
  46. $software_version = self::SOFTWARE_VERSION;
  47. $app_version = self::APP_VERSION;
  48. $ts = $this->getTime();
  49. $paramsToHash = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version');
  50. $api_sig = $this->getApiSignature($paramsToHash);
  51. $params = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'api_sig');
  52. $url = self::SERVER . self::GET_FILTERS_URL;
  53. $result = $this->request($url, $params);
  54. if($result != null) {
  55. $xml = new SimpleXMLElement($result);
  56. $filters = new SimpleXMLElement('<filters />');
  57. foreach ($xml->filters->filter as $item) {
  58. $filter = $filters->addChild('filterInfo');
  59. $filter->addChild('label', $item['label']);
  60. $filter->addChild('uid', $item['uid']);
  61. $filter->addChild('description', $item->description);
  62. $parameters = $filter->addChild('parameterList');
  63. foreach ($item->filtermetadata->parameter as $parameter) {
  64. $parameterNode = $parameters->addChild("parameter");
  65. foreach ($parameter->attributes() as $key => $value) {
  66. $parameterNode->addAttribute($key, $value);
  67. }
  68. }
  69. }
  70. return $this->objectsIntoArray( $filters );
  71. }
  72. }
  73. /**
  74. * Uploads an image to the Aviary server.
  75. *
  76. * @param string $filename
  77. * @return array Array with url to the file
  78. */
  79. public function upload($filename)
  80. {
  81. $api_key = $this->api_key;
  82. $api_secret = $this->api_secret;
  83. $version = self::VERSION;
  84. $platform = self::PLATFORM;
  85. $hardware_version = self::HARDWARE_VERSION;
  86. $software_version = self::SOFTWARE_VERSION;
  87. $app_version = self::APP_VERSION;
  88. $ts = $this->getTime();
  89. $paramsToHash = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version');
  90. $api_sig = $this->getApiSignature($paramsToHash);
  91. $file = '@' . $filename;
  92. $params = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'file', 'api_sig');
  93. $url = self::SERVER . self::UPLOAD_URL;
  94. $result = $this->request($url, $params);
  95. if($result != null) {
  96. $xml = new SimpleXMLElement($result);
  97. $url = $xml->response->files->file['url'];
  98. $uploadresponse = new SimpleXMLElement('<uploadresponse />');
  99. $uploadresponse->addChild('url', $url);
  100. return $this->objectsIntoArray( $uploadresponse );
  101. }
  102. }
  103. /**
  104. * Renders a filter options thumbnail grid and returns render parameters for each option.
  105. *
  106. * @param string $backgroundcolor
  107. * @param string $format
  108. * @param string $quality
  109. * @param string $scale
  110. * @param string $filepath
  111. * @param string $filterid
  112. * @param string $cols
  113. * @param string $rows
  114. * @param string $cellwidth
  115. * @param string $cellheight
  116. * @return array Array with url to thumbnail grid and render option parameters
  117. */
  118. public function renderOptions($backgroundcolor, $format, $quality, $scale, $filepath, $filterid, $cols, $rows, $cellwidth, $cellheight)
  119. {
  120. $api_key = $this->api_key;
  121. $api_secret = $this->api_secret;
  122. $version = self::VERSION;
  123. $platform = self::PLATFORM;
  124. $hardware_version = self::HARDWARE_VERSION;
  125. $software_version = self::SOFTWARE_VERSION;
  126. $app_version = self::APP_VERSION;
  127. $calltype = "previewRender";
  128. $ts = $this->getTime();
  129. $paramsToHash = compact( 'api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'calltype', 'backgroundcolor',
  130. 'format', 'quality', 'scale', 'filepath', 'filterid', 'cols', 'rows', 'cellwidth', 'cellheight');
  131. $api_sig = $this->getApiSignature($paramsToHash);
  132. $params = compact( 'api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'calltype', 'backgroundcolor',
  133. 'format', 'quality', 'scale', 'filepath', 'filterid', 'cols', 'rows', 'cellwidth', 'cellheight', 'api_sig');
  134. $url = self::SERVER . self::RENDER_URL;
  135. $result = $this->request($url, $params);
  136. if($result != null) {
  137. $xml = new SimpleXMLElement($result);
  138. $renderOptionsGrid = new SimpleXMLElement('<renderOptionsGrid />');
  139. $renderOptionsGrid->addChild('url', $xml->ostrichrenderresponse->url);
  140. $renderOptionParams = $xml->ostrichrenderresponse->renders;
  141. $renderReponse = compact('renderOptionsGrid', 'renderOptionParams');
  142. return $this->objectsIntoArray( $renderReponse );
  143. }
  144. }
  145. /**
  146. * Renders image based on render parameters.
  147. *
  148. * @param string $backgroundcolor
  149. * @param string $format
  150. * @param string $quality
  151. * @param string $scale
  152. * @param string $filepath
  153. * @param string $filterid
  154. * @param string $width
  155. * @param string $height
  156. * @param array $renderparameters
  157. * @return array Array with URL to rendered image
  158. */
  159. public function render($backgroundcolor, $format, $quality, $scale, $filepath, $filterid, $width, $height, $renderparameters)
  160. {
  161. $api_key = $this->api_key;
  162. $api_secret = $this->api_secret;
  163. $version = self::VERSION;
  164. $platform = self::PLATFORM;
  165. $hardware_version = self::HARDWARE_VERSION;
  166. $software_version = self::SOFTWARE_VERSION;
  167. $app_version = self::APP_VERSION;
  168. $calltype = "filteruse";
  169. $cols = "0";
  170. $rows = "0";
  171. $cellwidth = $width;
  172. $cellheight = $height;
  173. $ts = $this->getTime();
  174. $renderparameters = $this->array2xml($renderparameters);
  175. $paramsToHash = compact( 'api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'calltype', 'backgroundcolor',
  176. 'format', 'quality', 'scale', 'filepath', 'filterid', 'cols', 'rows', 'cellwidth', 'cellheight', 'renderparameters');
  177. $api_sig = $this->getApiSignature($paramsToHash);
  178. $params = compact( 'api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'version', 'calltype', 'backgroundcolor',
  179. 'format', 'quality', 'scale', 'filepath', 'filterid', 'cols', 'rows', 'cellwidth', 'cellheight', 'renderparameters', 'api_sig');
  180. $url = self::SERVER . self::RENDER_URL;
  181. $result = $this->request($url, $params);
  182. if($result != null) {
  183. $xml = new SimpleXMLElement($result);
  184. $renderedImage = $xml->ostrichrenderresponse->url;
  185. $renderResponse = new SimpleXMLElement('<renderresponse />');
  186. $renderResponse->addChild('url', $renderedImage);
  187. return $this->objectsIntoArray($renderResponse);
  188. }
  189. }
  190. /**
  191. * Returns the current server time
  192. *
  193. * @return string Current time on the server
  194. */
  195. protected function getTime()
  196. {
  197. $api_key = $this->api_key;
  198. $platform = self::PLATFORM;
  199. $hardware_version = self::HARDWARE_VERSION;
  200. $software_version = self::SOFTWARE_VERSION;
  201. $app_version = self::APP_VERSION;
  202. $ts = time();
  203. $paramsToHash = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts');
  204. $api_sig = $this->getApiSignature($paramsToHash);
  205. $params = compact('api_key', 'platform', 'hardware_version', 'software_version', 'app_version', 'ts', 'api_sig');
  206. $url = self::SERVER . self::GET_TIME_URL;
  207. $result = $this->request($url, $params);
  208. if($result != null) {
  209. $xml = new SimpleXMLElement($result);
  210. $serverTime = $xml['servertime'];
  211. return $serverTime;
  212. }
  213. }
  214. /**
  215. * Returns the api signature for the params
  216. *
  217. * @param array $paramsToHash
  218. * @return string Api signature string
  219. */
  220. protected function getApiSignature($paramsToHash)
  221. {
  222. $paramString = '';
  223. ksort($paramsToHash);
  224. foreach($paramsToHash as $keys => $values) {
  225. $paramString[] = $keys . $values;
  226. }
  227. $stringToMD5 = implode("", $paramString);
  228. $api_sig = md5( $this->api_secret . $stringToMD5 );
  229. return $api_sig;
  230. }
  231. /**
  232. * Makes a curl request and returns the response
  233. *
  234. * @param string $url
  235. * @param array $params
  236. * @return string Server response as a string
  237. */
  238. protected function request($url, $params)
  239. {
  240. $curl = curl_init();
  241. curl_setopt($curl, CURLOPT_URL, $url);
  242. curl_setopt($curl, CURLOPT_HEADER, FALSE);
  243. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  244. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
  245. curl_setopt($curl, CURLOPT_POST, TRUE);
  246. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  247. if( curl_exec($curl) === false ) {
  248. echo "Error: " . curl_error($curl);
  249. } else {
  250. $response = curl_exec($curl);
  251. }
  252. curl_close($curl);
  253. return $response;
  254. }
  255. /**
  256. * Converts a SimpleXMLObject into an array. http://php.net/manual/en/book.simplexml.php
  257. *
  258. * @param SimpleXMLObject $arrObjData
  259. * @param array $arrSkipIndices
  260. * @return array Array for the converted object
  261. */
  262. protected function objectsIntoArray($arrObjData, $arrSkipIndices = array())
  263. {
  264. $arrData = array();
  265. if ( is_object($arrObjData) ) {
  266. $arrObjData = get_object_vars($arrObjData);
  267. }
  268. if ( is_array($arrObjData) ) {
  269. foreach ($arrObjData as $index => $value) {
  270. if ( is_object($value) || is_array($value) ) {
  271. $value = $this->objectsIntoArray($value, $arrSkipIndices); // recursive call
  272. }
  273. if ( in_array($index, $arrSkipIndices) ) {
  274. continue;
  275. }
  276. $arrData[$index] = $value;
  277. }
  278. }
  279. return $arrData;
  280. }
  281. /**
  282. * Converts an array into a SimpleXMLObject and returns the filter parameters as xml
  283. *
  284. * @param array $data
  285. * @param string $rootNodeName
  286. * @param string $xml
  287. * @return xml Filter parameters as XML
  288. */
  289. protected function array2xml($data, $rootNodeName = 'parameters', $xml=null)
  290. {
  291. $output = new SimpleXMLElement('<response />');
  292. if (ini_get('zend.ze1_compatibility_mode') == 1) {
  293. ini_set ('zend.ze1_compatibility_mode', 0);
  294. }
  295. if ( is_null($xml) ) {
  296. $xml = $output->addChild($rootNodeName);
  297. }
  298. foreach($data as $key => $value) {
  299. if ( is_numeric($key) ) {
  300. $key = $rootNodeName;
  301. }
  302. $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
  303. if ( is_array($value) ) {
  304. if($key != "attributes") {
  305. $node = $this->isAssoc($value) ? $xml->addChild($key) : $xml;
  306. } else {
  307. $node = $this->isAssoc($value) ? $xml : $xml;
  308. }
  309. $this->array2xml($value, $key, $node);
  310. } else {
  311. $value = htmlentities($value);
  312. $xml->addAttribute($key, (string) $value);
  313. }
  314. }
  315. return $output->parameters->asXML();
  316. }
  317. protected function isAssoc( $array ) {
  318. return ( is_array($array) && 0 !== count( array_diff_key( $array, array_keys( array_keys($array) ) ) ) );
  319. }
  320. }
  321. ?>