-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtinypng.php
152 lines (141 loc) · 4.45 KB
/
tinypng.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* TinyPNG Class
*
* @author Michael Donaldson
* @version 0.1.0
* @copyright Copyright (c), Michael Donaldson. All rights reserved.
* @license MIT
*/
class TinyPNG
{
// API Endpoint
const API_ENDPOINT = "https://api.tinypng.com/shrink";
/**
* Do we need to use the cacert.pem file?
*
* @var boolean
*/
private $use_pem;
/**
* API Key for TinyPNG
*
* @var string
*/
private $api_key;
/**
* Instance of cURL
*
* @var resource
*/
private $curl;
/**
* Constructor
*
* @param string $api_key API Key for TinyPNG
* @param boolean $use_pem If you are having issues connecting to the API endpoint, set this option to true
*/
function __construct($api_key, $use_pem = false)
{
// Set API Key
$this -> api_key = $api_key;
// Are we using the pem file?
$this -> use_pem = $use_pem;
// Init cURL
$this -> curl = curl_init();
}
/**
* Compress
*
* Runs through the specified images, sending a request to TinyPNG to compress each image. The response for
* each request is then stored in an array and returned upon completion of the final request.
*
* @param string|array $input This can be either a single path name or an array of input/output path name pairs
* @param string $output This should be a single path name or left blank if an array was provided to $input
*
* @return string|array Returns a single error or an array of responses received from TinyPNG
*/
public function compress($input, $output = "")
{
// Check that at least one image has been provided
if($input !== null){
// Single or Multiple images?
if(is_array($input)){
// Multiple images
$response = array();
// Loop through images, running compression on each
foreach($input as $in => $out)
{
// Push result into 'response' array
$response[$in] = $this -> compressImage($in, $out);
}
// Return response
return $response;
} else {
// Single image
if($output !== null){
return $this -> compressImage($input, $output);
} else {
return "Invalid output path provided.";
}
}
} else {
return "Invalid input path provided.";
}
}
/**
* Compress Image
*
* This is a private function and is called by the 'compress' function to send a request to TinyPNG
* to compress the specified image, if the request is successful the image will be saved in the
* location defined in $output.
*
* @param string $input This should be a single path name to the image that is to be compressed
* @param string $output This should be a single path name to where the compressed image should be saved
*
* @return array Returns the response from TinyPNG
*/
private function compressImage($input, $output)
{
// Set cURL options
curl_setopt_array($this -> curl, array(
CURLOPT_URL => self::API_ENDPOINT,
CURLOPT_USERPWD => "api:" . $this -> api_key,
CURLOPT_POSTFIELDS => file_get_contents($input),
CURLOPT_BINARYTRANSFER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => true
));
// Check if we need to use the pem file
if($this -> use_pem){
curl_setopt($this -> curl, CURLOPT_CAINFO, __DIR__ . "/cacert.pem");
}
// Execute request
$response = curl_exec($this -> curl);
// Extract response body
$headsize = curl_getinfo($this -> curl, CURLINFO_HEADER_SIZE);
$body = json_decode(substr($response, $headsize), true);
// Check to see if the request was successfull
if(curl_getinfo($this -> curl, CURLINFO_HTTP_CODE) === 201){
// Success, get headers
$headers = substr($response, 0, curl_getinfo($this -> curl, CURLINFO_HEADER_SIZE));
// Loop through headers, look for the location of the compressed image
foreach (explode("\r\n", $headers) as $header) {
if (substr($header, 0, 10) === "Location: ") {
// Get file
$file = file_get_contents(substr($header, 10));
// Save file
file_put_contents($output, $file);
// Return
return $body;
}
}
// Didn't find header, error
return "Sorry, an unexpected error has occurred.";
} else {
// Error, return body (body could contain the reason for the error)
return $body;
}
}
}