-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclass-tiny-compress.php
More file actions
220 lines (184 loc) · 5.58 KB
/
class-tiny-compress.php
File metadata and controls
220 lines (184 loc) · 5.58 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
abstract class Tiny_Compress {
const KEY_MISSING = 'Register an account or provide an API key first';
const FILE_MISSING = 'File does not exist';
const WRITE_ERROR = 'No permission to write to file';
protected $after_compress_callback;
public static function create( $api_key, $after_compress_callback = null ) {
if ( Tiny_PHP::client_supported() ) {
$class = 'Tiny_Compress_Client';
} elseif ( Tiny_PHP::fopen_available() ) {
$class = 'Tiny_Compress_Fopen';
} else {
throw new Tiny_Exception(
'No HTTP client is available (cURL or fopen)',
'NoHttpClient'
);
}
return new $class( $api_key, $after_compress_callback );
}
/* Based on pricing April 2016. */
public static function estimate_cost( $compressions, $compressions_used ) {
return round(
self::compression_cost( $compressions + $compressions_used ) -
self::compression_cost( $compressions_used ),
2
);
}
protected function __construct( $after_compress_callback ) {
$this->after_compress_callback = $after_compress_callback;
}
abstract public function can_create_key();
abstract public function get_compression_count();
abstract public function get_remaining_credits();
abstract public function get_paying_state();
abstract public function get_email_address();
abstract public function get_key();
public function limit_reached() {
return $this->get_remaining_credits() === 0;
}
public function get_status() {
if ( $this->get_key() == null ) {
return (object) array(
'ok' => false,
'message' => self::KEY_MISSING,
);
}
$result = false;
$message = null;
try {
$result = $this->validate();
} catch ( Tiny_Exception $err ) {
if ( $err->get_status() == 404 ) {
$message = 'The key that you have entered is not valid';
} else {
list($message) = explode( ' (HTTP', $err->getMessage(), 2 );
}
}
$this->call_after_compress_callback();
return (object) array(
'ok' => $result,
'message' => $message,
);
}
/**
* Compresses a single file
*
* @param string $file path to file
* @param array $resize_opts
* @param array $preserve_opts
* @param array{ string } conversion options
* @return void
*/
public function compress_file(
$file,
$resize_opts = array(),
$preserve_opts = array(),
$convert_to = array()
) {
if ( $this->get_key() == null ) {
throw new Tiny_Exception( self::KEY_MISSING, 'KeyError' );
}
if ( ! file_exists( $file ) ) {
throw new Tiny_Exception( self::FILE_MISSING, 'FileError' );
}
if ( ! is_writable( $file ) ) {
throw new Tiny_Exception( self::WRITE_ERROR, 'FileError' );
}
if ( ! $this->needs_resize( $file, $resize_opts ) ) {
$resize_opts = false;
}
try {
$file_data = file_get_contents( $file );
list($output, $details, $convert_output ) = $this->compress(
$file_data,
$resize_opts,
$preserve_opts,
$convert_to
);
} catch ( Tiny_Exception $err ) {
$this->call_after_compress_callback();
throw $err;
}
try {
file_put_contents( $file, $output );
} catch ( Exception $e ) {
throw new Tiny_Exception( $e->getMessage(), 'FileError' );
}
if ( $convert_output ) {
$converted_filepath = Tiny_Helpers::replace_file_extension(
$details['convert']['type'],
$file
);
try {
file_put_contents( $converted_filepath, $convert_output );
} catch ( Exception $e ) {
throw new Tiny_Exception( $e->getMessage(), 'FileError' );
}
$details['convert']['path'] = $converted_filepath;
}
if ( $resize_opts ) {
$details['output']['resized'] = true;
}
$this->call_after_compress_callback();
return $details;
}
abstract protected function validate();
abstract protected function compress(
$input,
$resize_options,
$preserve_options,
$convert_to
);
protected static function identifier() {
return 'WordPress/' . Tiny_Plugin::wp_version() . ' Plugin/' . Tiny_Plugin::version();
}
private function call_after_compress_callback() {
if ( $this->after_compress_callback ) {
call_user_func( $this->after_compress_callback, $this );
}
}
private static function needs_resize( $file, $resize_options ) {
if ( ! $resize_options ) {
return false;
}
list($width, $height) = getimagesize( $file );
$should_resize_width = isset( $resize_options['width'] ) &&
$width > $resize_options['width'];
$should_resize_height = isset( $resize_options['height'] ) &&
$height > $resize_options['height'];
return $should_resize_width || $should_resize_height;
}
private static function compression_cost( $total ) {
$cost = 0;
if ( $total > 10000 ) {
$compressions = $total - 10000;
$cost += $compressions * 0.002;
$total -= $compressions;
}
if ( $total > 500 ) {
$compressions = $total - 500;
$cost += $compressions * 0.009;
$total -= $compressions;
}
return $cost;
}
}