compatibility
5 months ago
config
5 months ago
css
5 months ago
data
3 years ago
images
3 years ago
js
5 months ago
vendor
4 months ago
views
2 months ago
class-tiny-apache-rewrite.php
2 months ago
class-tiny-bulk-optimization.php
5 months ago
class-tiny-cli.php
5 months ago
class-tiny-compress-client.php
5 months ago
class-tiny-compress-fopen.php
5 months ago
class-tiny-compress.php
5 months ago
class-tiny-conversion.php
2 months ago
class-tiny-diagnostics.php
5 months ago
class-tiny-exception.php
5 months ago
class-tiny-helpers.php
5 months ago
class-tiny-image-size.php
5 months ago
class-tiny-image.php
5 months ago
class-tiny-logger.php
2 months ago
class-tiny-notices.php
2 months ago
class-tiny-php.php
5 months ago
class-tiny-picture.php
4 months ago
class-tiny-plugin.php
2 months ago
class-tiny-settings.php
2 months ago
class-tiny-source-base.php
2 months ago
class-tiny-source-image.php
5 months ago
class-tiny-source-picture.php
5 months ago
class-tiny-wp-base.php
5 months ago
class-tiny-image-size.php
257 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 $_mime_type; |
| 29 | private $_duplicate = false; |
| 30 | private $_duplicate_of_size = ''; |
| 31 | |
| 32 | public function __construct( $filename = null ) { |
| 33 | $this->filename = $filename; |
| 34 | } |
| 35 | |
| 36 | public function end_time() { |
| 37 | if ( isset( $this->meta['end'] ) ) { |
| 38 | return $this->meta['end']; |
| 39 | } elseif ( isset( $this->meta['timestamp'] ) ) { |
| 40 | return $this->meta['timestamp']; |
| 41 | } else { |
| 42 | return null; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public function add_tiny_meta_start() { |
| 47 | $this->meta = array( |
| 48 | 'start' => time(), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | public function add_tiny_meta( $response ) { |
| 53 | if ( isset( $this->meta['start'] ) ) { |
| 54 | $this->meta = $response; |
| 55 | $this->meta['end'] = time(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function add_tiny_meta_error( $exception ) { |
| 60 | if ( isset( $this->meta['start'] ) ) { |
| 61 | $this->meta = array( |
| 62 | 'error' => $exception->get_type(), |
| 63 | 'message' => $exception->get_message(), |
| 64 | 'timestamp' => time(), |
| 65 | ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Marks the image size as compressed without actually processing it. |
| 71 | * |
| 72 | * This method simulates the compression process by creating metadata that |
| 73 | * indicates the image has been processed, while keeping the original file |
| 74 | * size and format unchanged. Useful for marking images as compressed when |
| 75 | * they don't need actual compression or have been processed externally. |
| 76 | * |
| 77 | * @since 3.0.0 |
| 78 | * |
| 79 | * @param bool $include_conversion Optional. Whether to include conversion metadata. |
| 80 | * When true, adds conversion data with current |
| 81 | * file information. Default false. |
| 82 | * @return void |
| 83 | */ |
| 84 | public function mark_as_compressed( $include_conversion = false ) { |
| 85 | |
| 86 | if ( ! $this->has_been_compressed() ) { |
| 87 | $this->add_tiny_meta_start(); |
| 88 | $tiny_image_size_meta = array( |
| 89 | 'input' => array( |
| 90 | 'size' => $this->filesize(), |
| 91 | ), |
| 92 | 'output' => array( |
| 93 | 'size' => $this->filesize(), |
| 94 | 'type' => $this->mimetype(), |
| 95 | ), |
| 96 | ); |
| 97 | $this->add_tiny_meta( $tiny_image_size_meta ); |
| 98 | } |
| 99 | |
| 100 | if ( ! $this->has_been_converted() && $include_conversion ) { |
| 101 | $this->meta['convert'] = array( |
| 102 | 'size' => $this->filesize(), |
| 103 | 'type' => $this->mimetype(), |
| 104 | 'path' => $this->filename, |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public function has_been_compressed() { |
| 110 | return isset( $this->meta['output'] ); |
| 111 | } |
| 112 | |
| 113 | public function has_been_converted() { |
| 114 | return isset( $this->meta['convert'] ); |
| 115 | } |
| 116 | |
| 117 | public function never_compressed() { |
| 118 | return ! $this->has_been_compressed(); |
| 119 | } |
| 120 | |
| 121 | public function filesize() { |
| 122 | if ( is_null( $this->_file_size ) ) { |
| 123 | if ( $this->exists() ) { |
| 124 | $this->_file_size = filesize( $this->filename ); |
| 125 | } else { |
| 126 | $this->_file_size = 0; |
| 127 | } |
| 128 | } |
| 129 | return $this->_file_size; |
| 130 | } |
| 131 | |
| 132 | public function mimetype() { |
| 133 | if ( is_null( $this->_mime_type ) ) { |
| 134 | if ( $this->exists() ) { |
| 135 | $file = file_get_contents( $this->filename ); |
| 136 | $this->_mime_type = Tiny_Helpers::get_mimetype( $file ); |
| 137 | } else { |
| 138 | $this->_mime_type = 'application/octet-stream'; |
| 139 | } |
| 140 | } |
| 141 | return $this->_mime_type; |
| 142 | } |
| 143 | |
| 144 | public function exists() { |
| 145 | if ( is_null( $this->_exists ) ) { |
| 146 | $this->_exists = $this->filename && file_exists( $this->filename ); |
| 147 | } |
| 148 | return $this->_exists; |
| 149 | } |
| 150 | |
| 151 | private function same_size() { |
| 152 | return ( $this->filesize() == $this->meta['output']['size'] ); |
| 153 | } |
| 154 | |
| 155 | public function still_exists() { |
| 156 | return $this->has_been_compressed() && $this->exists(); |
| 157 | } |
| 158 | |
| 159 | public function missing() { |
| 160 | return $this->has_been_compressed() && ! $this->exists(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checks wether the image has been processed for conversion. |
| 165 | * Will still return true if conversion was not needed. |
| 166 | * |
| 167 | * @return bool true if image is processed for conversion |
| 168 | */ |
| 169 | public function converted() { |
| 170 | return isset( $this->meta['convert'] ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Checks wether the image is applicable for conversion and has |
| 175 | * not been converted yet. |
| 176 | * |
| 177 | * @return bool true if image can be converted and has not been converted |
| 178 | */ |
| 179 | public function unconverted() { |
| 180 | return ! $this->converted() && $this->exists(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Checks if the converted image size exists |
| 185 | * |
| 186 | * @return boolean true if the image size has a optimized alternative format |
| 187 | */ |
| 188 | public function converted_image_exists() { |
| 189 | if ( ! $this->converted() ) { |
| 190 | return false; |
| 191 | } |
| 192 | return file_exists( $this->meta['convert']['path'] ); |
| 193 | } |
| 194 | |
| 195 | public function conversion_text() { |
| 196 | if ( ! $this->converted() ) { |
| 197 | return esc_html__( 'Not converted', 'tiny-compress-images' ); |
| 198 | } |
| 199 | $conversion_text = $this->meta['convert']['type'] . ' (' . |
| 200 | size_format( $this->meta['convert']['size'], 1 ) . ')'; |
| 201 | return $conversion_text; |
| 202 | } |
| 203 | |
| 204 | public function compressed() { |
| 205 | return $this->still_exists() && $this->same_size(); |
| 206 | } |
| 207 | |
| 208 | public function modified() { |
| 209 | return $this->still_exists() && ! $this->same_size(); |
| 210 | } |
| 211 | |
| 212 | public function uncompressed() { |
| 213 | return $this->exists() && |
| 214 | ! $this->is_duplicate() && |
| 215 | ! ( isset( $this->meta['output'] ) && $this->same_size() ); |
| 216 | } |
| 217 | |
| 218 | public function in_progress() { |
| 219 | return $this->recently_started() && ! isset( $this->meta['output'] ); |
| 220 | } |
| 221 | |
| 222 | public function resized() { |
| 223 | return ( |
| 224 | isset( $this->meta['output'] ) && |
| 225 | isset( $this->meta['output']['resized'] ) && |
| 226 | $this->meta['output']['resized'] |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | public function mark_duplicate( $duplicate_size_name ) { |
| 231 | $this->_duplicate = true; |
| 232 | $this->_duplicate_of_size = $duplicate_size_name; |
| 233 | } |
| 234 | |
| 235 | public function is_duplicate() { |
| 236 | return $this->_duplicate; |
| 237 | } |
| 238 | |
| 239 | public function duplicate_of_size() { |
| 240 | return $this->_duplicate_of_size; |
| 241 | } |
| 242 | |
| 243 | public function delete_converted_image_size() { |
| 244 | if ( $this->converted_image_exists() ) { |
| 245 | unlink( $this->meta['convert']['path'] ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private function recently_started() { |
| 250 | $thirty_minutes_ago = gmdate( 'U' ) - ( 60 * 30 ); |
| 251 | return ( |
| 252 | isset( $this->meta['start'] ) && |
| 253 | $this->meta['start'] > $thirty_minutes_ago |
| 254 | ); |
| 255 | } |
| 256 | } |
| 257 |