Tinify.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tinify; |
| 4 | |
| 5 | const VERSION = "1.5.0"; |
| 6 | |
| 7 | class Tinify { |
| 8 | const AUTHENTICATED = true; |
| 9 | const ANONYMOUS = false; |
| 10 | |
| 11 | private static $key = NULL; |
| 12 | private static $appIdentifier = NULL; |
| 13 | private static $proxy = NULL; |
| 14 | |
| 15 | private static $compressionCount = NULL; |
| 16 | |
| 17 | private static $client = NULL; |
| 18 | |
| 19 | public static function setKey($key) { |
| 20 | self::$key = $key; |
| 21 | self::$client = NULL; |
| 22 | } |
| 23 | |
| 24 | public static function getKey() { |
| 25 | return self::$key; |
| 26 | } |
| 27 | |
| 28 | public static function createKey($email, $options) { |
| 29 | $body = array_merge(array("email" => $email), $options); |
| 30 | $response = self::getClient(self::ANONYMOUS)->request("post", "/keys", $body); |
| 31 | self::setKey($response->body->key); |
| 32 | } |
| 33 | |
| 34 | public static function setAppIdentifier($appIdentifier) { |
| 35 | self::$appIdentifier = $appIdentifier; |
| 36 | self::$client = NULL; |
| 37 | } |
| 38 | |
| 39 | public static function setProxy($proxy) { |
| 40 | self::$proxy = $proxy; |
| 41 | self::$client = NULL; |
| 42 | } |
| 43 | |
| 44 | public static function getCompressionCount() { |
| 45 | return self::$compressionCount; |
| 46 | } |
| 47 | |
| 48 | public static function setCompressionCount($compressionCount) { |
| 49 | self::$compressionCount = $compressionCount; |
| 50 | } |
| 51 | |
| 52 | public static function getClient($mode = self::AUTHENTICATED) { |
| 53 | if ($mode == self::AUTHENTICATED && !self::$key) { |
| 54 | throw new AccountException("Provide an API key with Tinify\setKey(...)"); |
| 55 | } |
| 56 | |
| 57 | if (!self::$client) { |
| 58 | self::$client = new Client(self::$key, self::$appIdentifier, self::$proxy); |
| 59 | } |
| 60 | |
| 61 | return self::$client; |
| 62 | } |
| 63 | |
| 64 | public static function setClient($client) { |
| 65 | self::$client = $client; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function setKey($key) { |
| 70 | return Tinify::setKey($key); |
| 71 | } |
| 72 | |
| 73 | function getKey() { |
| 74 | return Tinify::getKey(); |
| 75 | } |
| 76 | |
| 77 | function createKey($email, $options) { |
| 78 | return Tinify::createKey($email, $options); |
| 79 | } |
| 80 | |
| 81 | function setAppIdentifier($appIdentifier) { |
| 82 | return Tinify::setAppIdentifier($appIdentifier); |
| 83 | } |
| 84 | |
| 85 | function setProxy($proxy) { |
| 86 | return Tinify::setProxy($proxy); |
| 87 | } |
| 88 | |
| 89 | function getCompressionCount() { |
| 90 | return Tinify::getCompressionCount(); |
| 91 | } |
| 92 | |
| 93 | function compressionCount() { |
| 94 | return Tinify::getCompressionCount(); |
| 95 | } |
| 96 | |
| 97 | function fromFile($path) { |
| 98 | return Source::fromFile($path); |
| 99 | } |
| 100 | |
| 101 | function fromBuffer($string) { |
| 102 | return Source::fromBuffer($string); |
| 103 | } |
| 104 | |
| 105 | function fromUrl($string) { |
| 106 | return Source::fromUrl($string); |
| 107 | } |
| 108 | |
| 109 | function validate() { |
| 110 | try { |
| 111 | Tinify::getClient()->request("post", "/shrink"); |
| 112 | } catch (AccountException $err) { |
| 113 | if ($err->status == 429) return true; |
| 114 | throw $err; |
| 115 | } catch (ClientException $err) { |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 |