Repository navigation
Expand file tree
/
Copy pathclass-tiny-image-size.php
More file actions
308 lines (270 loc) · 7.66 KB
/
Copy pathclass-tiny-image-size.php
File metadata and controls
308 lines (270 loc) · 7.66 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?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.
*/
class Tiny_Image_Size {
public $filename;
public $meta = array();
/**
* Whether the file exists on disk.
*
* @var bool|null $exists
*/
private $exists;
/**
* File size in bytes.
*
* @var int|null $file_size
*/
private $file_size;
/**
* MIME type of the file.
*
* @var string|null $mime_type
*/
private $mime_type;
private $duplicate = false;
private $duplicate_of_size = '';
public function __construct( $filename = null ) {
$this->filename = $filename;
}
public function end_time() {
if ( isset( $this->meta['end'] ) ) {
return $this->meta['end'];
} elseif ( isset( $this->meta['timestamp'] ) ) {
return $this->meta['timestamp'];
} else {
return null;
}
}
public function add_tiny_meta_start() {
$this->meta = array(
'start' => time(),
);
}
public function add_tiny_meta( $response ) {
if ( isset( $this->meta['start'] ) ) {
$this->meta = $response;
$this->meta['end'] = time();
$this->clear_memoized_filesystem();
}
}
/**
* Clears the memoized exists/file_size/mime_type values.
*
* Must be called whenever the file on disk changed after those were
* memoized, e.g. after compression overwrites it, so the next read
* reflects the new file instead of the stale cached one.
*
* @return void
*/
private function clear_memoized_filesystem() {
clearstatcache( true, $this->filename );
$this->exists = null;
$this->file_size = null;
$this->mime_type = null;
}
public function add_tiny_meta_error( $exception ) {
if ( isset( $this->meta['start'] ) ) {
$this->meta = array(
'error' => $exception->get_type(),
'message' => $exception->get_message(),
'timestamp' => time(),
);
}
}
/**
* Marks the image size as compressed without actually processing it.
*
* This method simulates the compression process by creating metadata that
* indicates the image has been processed, while keeping the original file
* size and format unchanged. Useful for marking images as compressed when
* they don't need actual compression or have been processed externally.
*
* @since 3.0.0
*
* @param bool $include_conversion Optional. Whether to include conversion metadata.
* When true, adds conversion data with current
* file information. Default false.
* @return void
*/
public function mark_as_compressed( $include_conversion = false ) {
if ( ! $this->has_been_compressed() ) {
$this->add_tiny_meta_start();
$tiny_image_size_meta = array(
'input' => array(
'size' => $this->filesize(),
),
'output' => array(
'size' => $this->filesize(),
'type' => $this->mimetype(),
),
);
$this->add_tiny_meta( $tiny_image_size_meta );
}
if ( ! $this->has_been_converted() && $include_conversion ) {
$this->meta['convert'] = array(
'size' => $this->filesize(),
'type' => $this->mimetype(),
'path' => $this->filename,
);
}
}
public function has_been_compressed() {
return isset( $this->meta['output'] );
}
public function has_been_converted() {
return isset( $this->meta['convert'] );
}
public function never_compressed() {
return ! $this->has_been_compressed();
}
public function filesize() {
if ( is_null( $this->file_size ) ) {
if ( $this->exists() ) {
$this->file_size = filesize( $this->filename );
} else {
$this->file_size = 0;
}
}
return $this->file_size;
}
public function mimetype() {
if ( is_null( $this->mime_type ) ) {
if ( $this->exists() ) {
$file = file_get_contents( $this->filename );
$this->mime_type = Tiny_Helpers::get_mimetype( $file );
} else {
$this->mime_type = 'application/octet-stream';
}
}
return $this->mime_type;
}
public function exists() {
if ( is_null( $this->exists ) ) {
$this->exists = $this->filename && file_exists( $this->filename );
}
return $this->exists;
}
private function same_size() {
return ( $this->filesize() == $this->meta['output']['size'] );
}
public function still_exists() {
return $this->has_been_compressed() && $this->exists();
}
public function missing() {
return $this->has_been_compressed() && ! $this->exists();
}
/**
* Checks wether the image has been processed for conversion.
* Will still return true if conversion was not needed.
*
* @return bool true if image is processed for conversion
*/
public function converted() {
return isset( $this->meta['convert'] );
}
/**
* Checks wether the image is applicable for conversion and has
* not been converted yet.
*
* @return bool true if image can be converted and has not been converted
*/
public function unconverted() {
return ! $this->converted() && $this->exists();
}
/**
* Checks if the converted image size exists
*
* @return boolean true if the image size has a optimized alternative format
*/
public function converted_image_exists() {
if ( ! $this->converted() ) {
return false;
}
return file_exists( $this->meta['convert']['path'] );
}
public function conversion_text() {
if ( ! $this->converted() ) {
return esc_html__( 'Not converted', 'tiny-compress-images' );
}
$conversion_text = $this->meta['convert']['type'] . ' (' .
size_format( $this->meta['convert']['size'], 1 ) . ')';
return $conversion_text;
}
public function compressed() {
return $this->still_exists() && $this->same_size();
}
public function modified() {
return $this->still_exists() && ! $this->same_size();
}
public function uncompressed() {
return $this->exists() &&
! $this->is_duplicate() &&
! ( isset( $this->meta['output'] ) && $this->same_size() );
}
public function in_progress() {
return $this->recently_started() && ! isset( $this->meta['output'] );
}
public function resized() {
return (
isset( $this->meta['output'] ) &&
isset( $this->meta['output']['resized'] ) &&
$this->meta['output']['resized']
);
}
public function mark_duplicate( $duplicate_size_name ) {
$this->duplicate = true;
$this->duplicate_of_size = $duplicate_size_name;
}
public function is_duplicate() {
return $this->duplicate;
}
public function duplicate_of_size() {
return $this->duplicate_of_size;
}
/**
* Deletes the converted image file for this image size.
*
* @return void
*/
public function delete_converted_image_size() {
if ( ! $this->converted_image_exists() ) {
return;
}
$upload_dir = wp_upload_dir();
$convert_real_path = realpath( $this->meta['convert']['path'] );
$real_basedir = realpath( $upload_dir['basedir'] );
if (
$convert_real_path &&
Tiny_Helpers::str_starts_with(
$convert_real_path,
trailingslashit( $real_basedir )
)
) {
unlink( $convert_real_path );
}
}
private function recently_started() {
$thirty_minutes_ago = gmdate( 'U' ) - ( 60 * 30 );
return (
isset( $this->meta['start'] ) &&
$this->meta['start'] > $thirty_minutes_ago
);
}
}