Source of file KrakenService.php
Size: 3,983 Bytes - Last Modified: 2021-12-24T06:43:57+00:00
/var/www/docs.ssmods.com/process/src/code/KrakenService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | <?php /** * the kraken service handles authenication to the kraken * api and provides functions for calling the api * @author Ryan */ class KrakenService extends Object { private static $enabled = false; private static $api_key = null; private static $api_secret = null; private static $lossy = true; private static $wait = true; private static $process_on_upload = false; /** * * @var type */ protected $krakenConnection = null; /** * establish connection to the Kraken API * @return Kraken */ public function getConnection() { if ($this->krakenConnection) { return $this->krakenConnection; } //establish connection if api keys are set if ($this->checkApiKeys()) { $kraken = new Kraken($this->config()->api_key, $this->config()->api_secret); $this->krakenConnection = $kraken; return $this->krakenConnection; } } /** * optimize an image * @param String $file * @return Array */ public function optimizeImage($file) { $kraken = $this->getConnection(); if ($kraken) { $params = array( "file" => $file, "lossy" => $this->getLossy(), //"dev" => $this->getDev() ); if ($this->getWait()) { $params['wait'] = true; } set_time_limit(400); $data = $kraken->upload($params); return $data; } } /** * optimize an image by remote URL * @param String $file * @return Array */ public function optimizeImageByURL($url) { $kraken = $this->getConnection(); if ($kraken) { $params = array( "url" => $url, "lossy" => $this->getLossy(), //"dev" => $this->getDev() ); if ($this->getWait()) { $params['wait'] = true; } set_time_limit(400); $data = $kraken->url($params); return $data; } } /** * Uses cURL to fetch an optimized image from Kraken * @param String $krakedUrl * @return JSON */ public function getOptimizedImage($krakedUrl) { $ch = curl_init($krakedUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36'); $result = curl_exec($ch); if ($result) { return $result; } } /** * return wait option * @return Boolean */ public function getWait() { return $this->config()->wait; } /** * return the lossy option * @return type */ public function getLossy() { return $this->config()->lossy; } /** * check if the API key and API secret have been * configured * @return boolean */ private function checkApiKeys() { if ($this->config()->api_key === null || $this->config()->api_secret === null) { return user_error('The Kraken API Key and API Secret have not been configured in SiteConfig.', E_USER_ERROR); } else { return true; } } /** * check api status * @return boolean */ public function getAPIStatus() { if ($this->checkApiKeys() ) { $kraken = new Kraken($this->config()->api_key, $this->config()->api_secret); $status = $kraken->status(); return $status; } return false; } } |