Skip to content

Commit bc4b815

Browse files
feat(docs): add phpdocs
* docs: add docs for methods and vars in Tinify class * Document client * Document Source * docs: result and resultmeta * Small improvements * Docs * Fix grammar in docs
1 parent 9b2085b commit bc4b815

5 files changed

Lines changed: 286 additions & 0 deletions

File tree

lib/Tinify.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,89 @@
55
const VERSION = "1.6.4";
66

77
class Tinify {
8+
/**
9+
* @var string|null API key.
10+
*/
811
private static $key = NULL;
12+
13+
/**
14+
* @var string|null Identifier used for requests.
15+
*/
916
private static $appIdentifier = NULL;
17+
18+
/**
19+
* @var string|null URL to the compression API.
20+
*/
1021
private static $proxy = NULL;
1122

23+
/**
24+
* @var int|null The number of compressions.
25+
*/
1226
private static $compressionCount = NULL;
27+
28+
/**
29+
* @var Client|null Tinify client.
30+
*/
1331
private static $client = NULL;
1432

33+
/**
34+
* Sets the key and resets the client.
35+
*
36+
* @param string $key The API key.
37+
* @return void
38+
*/
1539
public static function setKey($key) {
1640
self::$key = $key;
1741
self::$client = NULL;
1842
}
1943

44+
/**
45+
* Sets the app identifier and resets the client.
46+
*
47+
* @param string $appIdentifier The app identifier.
48+
* @return void
49+
*/
2050
public static function setAppIdentifier($appIdentifier) {
2151
self::$appIdentifier = $appIdentifier;
2252
self::$client = NULL;
2353
}
2454

55+
/**
56+
* Sets the proxy and resets the client.
57+
*
58+
* @param string $proxy URL to the proxy server.
59+
* @return void
60+
*/
2561
public static function setProxy($proxy) {
2662
self::$proxy = $proxy;
2763
self::$client = NULL;
2864
}
2965

66+
/**
67+
* Retrieves the compression count.
68+
*
69+
* @return int|null
70+
*/
3071
public static function getCompressionCount() {
3172
return self::$compressionCount;
3273
}
3374

75+
/**
76+
* Sets the compression count
77+
*
78+
* @param int $compressionCount
79+
* @return void
80+
*/
3481
public static function setCompressionCount($compressionCount) {
3582
self::$compressionCount = $compressionCount;
3683
}
3784

85+
/**
86+
* Retrieves the tinify client.
87+
* Will initiate a new client with the current key, identifier and proxy.
88+
*
89+
* @return Client
90+
*/
3891
public static function getClient() {
3992
if (!self::$key) {
4093
throw new AccountException("Provide an API key with Tinify\setKey(...)");
@@ -47,6 +100,12 @@ public static function getClient() {
47100
return self::$client;
48101
}
49102

103+
/**
104+
* Sets a new client
105+
*
106+
* @param Client $client
107+
* @return void
108+
*/
50109
public static function setClient($client) {
51110
self::$client = $client;
52111
}

lib/Tinify/Client.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,54 @@
33
namespace Tinify;
44

55
class Client {
6+
/**
7+
* The default endpoint
8+
*/
69
const API_ENDPOINT = "https://api.tinify.com";
710

11+
/**
12+
* Number of retries on failure
13+
*/
814
const RETRY_COUNT = 1;
15+
16+
/**
17+
* The time between retrieves
18+
*/
919
const RETRY_DELAY = 500;
1020

1121
private $options;
1222

23+
/**
24+
* Retrieves the user agent identifier
25+
* contains client version, php version and curl version
26+
*
27+
* @return string
28+
*/
1329
public static function userAgent() {
1430
$curl = curl_version();
1531
return "Tinify/" . VERSION . " PHP/" . PHP_VERSION . " curl/" . $curl["version"];
1632
}
1733

34+
/**
35+
* Path the the certificates
36+
*
37+
* @return string
38+
*/
1839
private static function caBundle() {
1940
return __DIR__ . "/../data/cacert.pem";
2041
}
2142

43+
/**
44+
* Constructor for client
45+
* validates if curl meets requirements,
46+
*
47+
* @param string $key api key
48+
* @param string|null $app_identifier identifier for the client
49+
* @param string|null $proxy an optional proxy url to go to first
50+
* @return void
51+
*
52+
* @throws ClientException when curl version is not supported
53+
*/
2254
function __construct($key, $app_identifier = NULL, $proxy = NULL) {
2355
$curl = curl_version();
2456

@@ -70,6 +102,17 @@ function __construct($key, $app_identifier = NULL, $proxy = NULL) {
70102
}
71103
}
72104

105+
/**
106+
* Makes an HTTP request to the API.
107+
*
108+
* @param string $method The HTTP method (GET, POST, etc.).
109+
* @param string $url The endpoint path.
110+
* @param array|string|null $body Optional. The request body. Default null.
111+
* @return object An object with 'body' and 'headers' properties.
112+
*
113+
* @throws ConnectionException If the connection fails.
114+
* @throws Exception If the API returns an error response.
115+
*/
73116
function request($method, $url, $body = NULL) {
74117
$header = array();
75118
if (is_array($body)) {
@@ -155,6 +198,12 @@ function request($method, $url, $body = NULL) {
155198
}
156199
}
157200

201+
/**
202+
* Parses HTTP headers
203+
*
204+
* @param array|string $headers The request headers
205+
* @return array lowercased headers
206+
*/
158207
protected static function parseHeaders($headers) {
159208
if (!is_array($headers)) {
160209
$headers = explode("\r\n", $headers);

lib/Tinify/Result.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,88 @@
22

33
namespace Tinify;
44

5+
/**
6+
* Represents a compressed image result from the Tinify API.
7+
* Contains the compressed image data and metadata such as content type and size.
8+
*
9+
* @see ResultMeta
10+
*/
511
class Result extends ResultMeta {
12+
/**
13+
* The compressed image data.
14+
*
15+
* @var string $data
16+
*/
617
protected $data;
718

19+
/**
20+
* Constructs a Result with metadata and image data.
21+
*
22+
* @param array $meta The response headers containing metadata.
23+
* @param string $data The compressed image data.
24+
* @return void
25+
*/
826
public function __construct($meta, $data) {
927
$this->meta = $meta;
1028
$this->data = $data;
1129
}
1230

31+
/**
32+
* Retrieves the compressed image data.
33+
*
34+
* @return string The compressed image data.
35+
*/
1336
public function data() {
1437
return $this->data;
1538
}
1639

40+
/**
41+
* Retrieves the compressed image as a buffer.
42+
*
43+
* Alias for data()
44+
* @see data()
45+
*
46+
* @return string The compressed image data.
47+
*/
1748
public function toBuffer() {
1849
return $this->data;
1950
}
2051

52+
/**
53+
* Writes the compressed image to a file.
54+
*
55+
* @param string $path The file path where the image should be written.
56+
* @return int|false The bytes written, or false on failure.
57+
*/
2158
public function toFile($path) {
2259
return file_put_contents($path, $this->toBuffer());
2360
}
2461

62+
/**
63+
* Retrieves the size of the compressed image in bytes.
64+
* @return int The size of the compressed image.
65+
*/
2566
public function size() {
2667
return intval($this->meta["content-length"]);
2768
}
2869

70+
/**
71+
* Retrieves the media type of the compressed image.
72+
*
73+
* @return string The media type eg 'image/png' or 'image/jpeg'.
74+
*/
2975
public function mediaType() {
3076
return $this->meta["content-type"];
3177
}
3278

79+
/**
80+
* Retrieves the content type of the compressed image.
81+
*
82+
* Alias for mediaType()
83+
* @see mediaType()
84+
*
85+
* @return string The content type e.g. 'image/png' or 'image/jpeg'.
86+
*/
3387
public function contentType() {
3488
return $this->mediaType();
3589
}

lib/Tinify/ResultMeta.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,55 @@
22

33
namespace Tinify;
44

5+
/**
6+
* Class containing compressed image meta data
7+
*/
58
class ResultMeta {
9+
/**
10+
* The response headers containing image metadata.
11+
*
12+
* @var array $meta
13+
*/
614
protected $meta;
715

16+
/**
17+
* Constructs a ResultMeta with response metadata.
18+
*
19+
* @param array $meta The response headers containing image metadata.
20+
*/
821
public function __construct($meta) {
922
$this->meta = $meta;
1023
}
1124

25+
/**
26+
* Width of the image in pixels.
27+
* @return int The image width.
28+
*/
1229
public function width() {
1330
return intval($this->meta["image-width"]);
1431
}
1532

33+
/**
34+
* Height of the image in pixels.
35+
* @return int The image height.
36+
*/
1637
public function height() {
1738
return intval($this->meta["image-height"]);
1839
}
1940

41+
/**
42+
* Location of the compressed image.
43+
* @return string|null The URL to the compressed image, or null if not available.
44+
*/
2045
public function location() {
2146
return isset($this->meta["location"]) ? $this->meta["location"] : null;
2247
}
2348

49+
/**
50+
* Retrieves the file extension of the image.
51+
* Extracts the extension from the content-type header.
52+
* @return string|null The file extension or null if not available.
53+
*/
2454
public function extension() {
2555
if (isset($this->meta["content-type"])) {
2656
$parts = explode("/", $this->meta["content-type"]);

0 commit comments

Comments
 (0)