PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 2.2.4
TinyPNG – JPEG, PNG & WebP image compression v2.2.4
3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-image-size.php
tiny-compress-images / src Last commit date
config 9 years ago css 8 years ago data 9 years ago images 9 years ago js 8 years ago vendor 9 years ago views 8 years ago class-tiny-compress-client.php 9 years ago class-tiny-compress-fopen.php 9 years ago class-tiny-compress.php 9 years ago class-tiny-exception.php 9 years ago class-tiny-image-size.php 9 years ago class-tiny-image.php 9 years ago class-tiny-notices.php 9 years ago class-tiny-php.php 9 years ago class-tiny-plugin.php 8 years ago class-tiny-settings.php 8 years ago class-tiny-wp-base.php 9 years ago
class-tiny-image-size.php
153 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2017 Voormedia B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 class Tiny_Image_Size {
22 public $filename;
23 public $meta = array();
24
25 /* Used more than once and not trivial, so we are memoizing these */
26 private $_exists;
27 private $_file_size;
28 private $_duplicate = false;
29 private $_duplicate_of_size = '';
30
31 public function __construct( $filename = null ) {
32 $this->filename = $filename;
33 }
34
35 public function end_time() {
36 if ( isset( $this->meta['end'] ) ) {
37 return $this->meta['end'];
38 } elseif ( isset( $this->meta['timestamp'] ) ) {
39 return $this->meta['timestamp'];
40 } else {
41 return null;
42 }
43 }
44
45 public function add_tiny_meta_start() {
46 $this->meta = array(
47 'start' => time(),
48 );
49 }
50
51 public function add_tiny_meta( $response ) {
52 if ( isset( $this->meta['start'] ) ) {
53 $this->meta = $response;
54 $this->meta['end'] = time();
55 }
56 }
57
58 public function add_tiny_meta_error( $exception ) {
59 if ( isset( $this->meta['start'] ) ) {
60 $this->meta = array(
61 'error' => $exception->get_type(),
62 'message' => $exception->get_message(),
63 'timestamp' => time(),
64 );
65 }
66 }
67
68 public function has_been_compressed() {
69 return isset( $this->meta['output'] );
70 }
71
72 public function never_compressed() {
73 return ! $this->has_been_compressed();
74 }
75
76 public function filesize() {
77 if ( is_null( $this->_file_size ) ) {
78 if ( $this->exists() ) {
79 $this->_file_size = filesize( $this->filename );
80 } else {
81 $this->_file_size = 0;
82 }
83 }
84 return $this->_file_size;
85 }
86
87 public function exists() {
88 if ( is_null( $this->_exists ) ) {
89 $this->_exists = $this->filename && file_exists( $this->filename );
90 }
91 return $this->_exists;
92 }
93
94 private function same_size() {
95 return ( $this->filesize() == $this->meta['output']['size'] );
96 }
97
98 public function still_exists() {
99 return $this->has_been_compressed() && $this->exists();
100 }
101
102 public function missing() {
103 return $this->has_been_compressed() && ! $this->exists();
104 }
105
106 public function compressed() {
107 return $this->still_exists() && $this->same_size();
108 }
109
110 public function modified() {
111 return $this->still_exists() && ! $this->same_size();
112 }
113
114 public function uncompressed() {
115 return $this->exists() &&
116 ! $this->is_duplicate() &&
117 ! ( isset( $this->meta['output'] ) && $this->same_size() );
118 }
119
120 public function in_progress() {
121 return $this->recently_started() && ! isset( $this->meta['output'] );
122 }
123
124 public function resized() {
125 return (
126 isset( $this->meta['output'] ) &&
127 isset( $this->meta['output']['resized'] ) &&
128 $this->meta['output']['resized']
129 );
130 }
131
132 public function mark_duplicate( $duplicate_size_name ) {
133 $this->_duplicate = true;
134 $this->_duplicate_of_size = $duplicate_size_name;
135 }
136
137 public function is_duplicate() {
138 return $this->_duplicate;
139 }
140
141 public function duplicate_of_size() {
142 return $this->_duplicate_of_size;
143 }
144
145 private function recently_started() {
146 $thirty_minutes_ago = date( 'U' ) - ( 60 * 30 );
147 return (
148 isset( $this->meta['start'] ) &&
149 $this->meta['start'] > $thirty_minutes_ago
150 );
151 }
152 }
153