compatibility
1 year ago
config
4 years ago
css
1 year ago
data
4 years ago
images
4 years ago
js
1 year ago
vendor
1 year ago
views
11 months ago
class-tiny-bulk-optimization.php
1 year ago
class-tiny-cli.php
11 months ago
class-tiny-compress-client.php
1 year ago
class-tiny-compress-fopen.php
1 year ago
class-tiny-compress.php
1 year ago
class-tiny-exception.php
4 years ago
class-tiny-helpers.php
1 year ago
class-tiny-image-size.php
1 year ago
class-tiny-image.php
1 year ago
class-tiny-notices.php
11 months ago
class-tiny-php.php
4 years ago
class-tiny-picture.php
11 months ago
class-tiny-plugin.php
11 months ago
class-tiny-settings.php
11 months ago
class-tiny-wp-base.php
11 months ago
class-tiny-image-size.php
204 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2018 Tinify 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 has_been_converted() { |
| 73 | return isset( $this->meta['convert'] ); |
| 74 | } |
| 75 | |
| 76 | public function never_compressed() { |
| 77 | return ! $this->has_been_compressed(); |
| 78 | } |
| 79 | |
| 80 | public function filesize() { |
| 81 | if ( is_null( $this->_file_size ) ) { |
| 82 | if ( $this->exists() ) { |
| 83 | $this->_file_size = filesize( $this->filename ); |
| 84 | } else { |
| 85 | $this->_file_size = 0; |
| 86 | } |
| 87 | } |
| 88 | return $this->_file_size; |
| 89 | } |
| 90 | |
| 91 | public function exists() { |
| 92 | if ( is_null( $this->_exists ) ) { |
| 93 | $this->_exists = $this->filename && file_exists( $this->filename ); |
| 94 | } |
| 95 | return $this->_exists; |
| 96 | } |
| 97 | |
| 98 | private function same_size() { |
| 99 | return ( $this->filesize() == $this->meta['output']['size'] ); |
| 100 | } |
| 101 | |
| 102 | public function still_exists() { |
| 103 | return $this->has_been_compressed() && $this->exists(); |
| 104 | } |
| 105 | |
| 106 | public function missing() { |
| 107 | return $this->has_been_compressed() && ! $this->exists(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Checks wether the image has been processed for conversion. |
| 112 | * Will still return true if conversion was not needed. |
| 113 | * |
| 114 | * @return bool true if image is processed for conversion |
| 115 | */ |
| 116 | public function converted() { |
| 117 | return isset( $this->meta['convert'] ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Checks wether the image is applicable for conversion and has |
| 122 | * not been converted yet. |
| 123 | * |
| 124 | * @return bool true if image can be converted and has not been converted |
| 125 | */ |
| 126 | public function unconverted() { |
| 127 | return ! $this->converted() && $this->exists(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks if the converted image size exists |
| 132 | * |
| 133 | * @return boolean true if the image size has a optimized alternative format |
| 134 | */ |
| 135 | public function converted_image_exists() { |
| 136 | if ( ! $this->converted() ) { |
| 137 | return false; |
| 138 | } |
| 139 | return file_exists( $this->meta['convert']['path'] ); |
| 140 | } |
| 141 | |
| 142 | public function conversion_text() { |
| 143 | if ( ! $this->converted() ) { |
| 144 | return esc_html__( 'Not converted', 'tiny-compress-images' ); |
| 145 | } |
| 146 | $conversion_text = $this->meta['convert']['type'] . ' (' . |
| 147 | size_format( $this->meta['convert']['size'], 1 ) . ')'; |
| 148 | return $conversion_text; |
| 149 | } |
| 150 | |
| 151 | public function compressed() { |
| 152 | return $this->still_exists() && $this->same_size(); |
| 153 | } |
| 154 | |
| 155 | public function modified() { |
| 156 | return $this->still_exists() && ! $this->same_size(); |
| 157 | } |
| 158 | |
| 159 | public function uncompressed() { |
| 160 | return $this->exists() && |
| 161 | ! $this->is_duplicate() && |
| 162 | ! ( isset( $this->meta['output'] ) && $this->same_size() ); |
| 163 | } |
| 164 | |
| 165 | public function in_progress() { |
| 166 | return $this->recently_started() && ! isset( $this->meta['output'] ); |
| 167 | } |
| 168 | |
| 169 | public function resized() { |
| 170 | return ( |
| 171 | isset( $this->meta['output'] ) && |
| 172 | isset( $this->meta['output']['resized'] ) && |
| 173 | $this->meta['output']['resized'] |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | public function mark_duplicate( $duplicate_size_name ) { |
| 178 | $this->_duplicate = true; |
| 179 | $this->_duplicate_of_size = $duplicate_size_name; |
| 180 | } |
| 181 | |
| 182 | public function is_duplicate() { |
| 183 | return $this->_duplicate; |
| 184 | } |
| 185 | |
| 186 | public function duplicate_of_size() { |
| 187 | return $this->_duplicate_of_size; |
| 188 | } |
| 189 | |
| 190 | public function delete_converted_image_size() { |
| 191 | if ( $this->converted_image_exists() ) { |
| 192 | unlink( $this->meta['convert']['path'] ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private function recently_started() { |
| 197 | $thirty_minutes_ago = date( 'U' ) - ( 60 * 30 ); |
| 198 | return ( |
| 199 | isset( $this->meta['start'] ) && |
| 200 | $this->meta['start'] > $thirty_minutes_ago |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 |