PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.8.0
TinyPNG – JPEG, PNG & WebP image compression v3.8.0
3.8.0 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-compress.php
tiny-compress-images / src Last commit date
compatibility 2 months ago config 2 months ago css 7 months ago data 4 years ago images 4 years ago js 1 week ago vendor 6 months ago views 1 week ago class-tiny-apache-rewrite.php 2 months ago class-tiny-bulk-optimization.php 2 months ago class-tiny-cli.php 2 months ago class-tiny-compress-client.php 1 week ago class-tiny-compress-fopen.php 2 months ago class-tiny-compress.php 1 week ago class-tiny-conversion.php 4 months ago class-tiny-diagnostics.php 7 months ago class-tiny-exception.php 7 months ago class-tiny-helpers.php 2 months ago class-tiny-image-size.php 1 week ago class-tiny-image.php 1 week ago class-tiny-logger.php 2 months ago class-tiny-migrate.php 2 months ago class-tiny-notices.php 2 months ago class-tiny-php.php 2 months ago class-tiny-picture.php 2 months ago class-tiny-plugin.php 1 week ago class-tiny-settings.php 1 week ago class-tiny-source-base.php 1 week ago class-tiny-source-image.php 7 months ago class-tiny-source-picture.php 7 months ago class-tiny-wp-base.php 2 months ago
class-tiny-compress.php
224 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 abstract class Tiny_Compress {
22
23 const KEY_MISSING = 'Register an account or provide an API key first';
24 const FILE_MISSING = 'File does not exist';
25 const WRITE_ERROR = 'No permission to write to file';
26
27 protected $after_compress_callback;
28
29 public static function create( $api_key, $after_compress_callback = null ) {
30 if ( Tiny_PHP::client_supported() ) {
31 $class = 'Tiny_Compress_Client';
32 } elseif ( Tiny_PHP::fopen_available() ) {
33 $class = 'Tiny_Compress_Fopen';
34 } else {
35 throw new Tiny_Exception(
36 'No HTTP client is available (cURL or fopen)',
37 'NoHttpClient'
38 );
39 }
40 return new $class( $api_key, $after_compress_callback );
41 }
42
43 /* Based on pricing April 2016. */
44 public static function estimate_cost( $compressions, $compressions_used ) {
45 return round(
46 self::compression_cost( $compressions + $compressions_used ) -
47 self::compression_cost( $compressions_used ),
48 2
49 );
50 }
51
52 protected function __construct( $after_compress_callback ) {
53 $this->after_compress_callback = $after_compress_callback;
54 }
55
56 abstract public function can_create_key();
57 abstract public function get_compression_count();
58 abstract public function get_remaining_credits();
59 abstract public function get_paying_state();
60 abstract public function get_email_address();
61 abstract public function get_key();
62
63 public function limit_reached() {
64 return $this->get_remaining_credits() === 0;
65 }
66
67 public function get_status() {
68 if ( $this->get_key() == null ) {
69 return (object) array(
70 'ok' => false,
71 'message' => self::KEY_MISSING,
72 );
73 }
74
75 $result = false;
76 $message = null;
77
78 try {
79 $result = $this->validate();
80 } catch ( Tiny_Exception $err ) {
81 if ( $err->get_status() == 404 ) {
82 $message = 'The key that you have entered is not valid';
83 } else {
84 list($message) = explode( ' (HTTP', $err->getMessage(), 2 );
85 }
86 }
87
88 $this->call_after_compress_callback();
89
90 return (object) array(
91 'ok' => $result,
92 'message' => $message,
93 );
94 }
95
96 /**
97 * Compresses a single file
98 *
99 * @param string $file
100 * @param array $resize_opts
101 * @param array $preserve_opts
102 * @param array{ string } conversion options
103 * @return void
104 */
105 public function compress_file(
106 $file,
107 $resize_opts = array(),
108 $preserve_opts = array(),
109 $convert_to = array()
110 ) {
111 if ( $this->get_key() == null ) {
112 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- not used in output
113 throw new Tiny_Exception( self::KEY_MISSING, 'KeyError' );
114 }
115
116 if ( ! file_exists( $file ) ) {
117 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- not used in output
118 throw new Tiny_Exception( self::FILE_MISSING, 'FileError' );
119 }
120
121 if ( ! is_writable( $file ) ) {
122 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- not used in output
123 throw new Tiny_Exception( self::WRITE_ERROR, 'FileError' );
124 }
125
126 if ( ! $this->needs_resize( $file, $resize_opts ) ) {
127 $resize_opts = false;
128 }
129
130 try {
131 $file_data = file_get_contents( $file );
132
133 list($output, $details, $convert_output ) = $this->compress(
134 $file_data,
135 $resize_opts,
136 $preserve_opts,
137 $convert_to
138 );
139 } catch ( Tiny_Exception $err ) {
140 $this->call_after_compress_callback();
141 throw $err;
142 }
143
144 try {
145 file_put_contents( $file, $output );
146 } catch ( Exception $e ) {
147 throw new Tiny_Exception( esc_html( $e->getMessage() ), 'FileError' );
148 }
149
150 if ( $convert_output ) {
151 $converted_filepath = Tiny_Helpers::replace_file_extension(
152 $details['convert']['type'],
153 $file
154 );
155
156 try {
157 file_put_contents( $converted_filepath, $convert_output );
158 } catch ( Exception $e ) {
159 throw new Tiny_Exception( esc_html( $e->getMessage() ), 'FileError' );
160 }
161 $details['convert']['path'] = $converted_filepath;
162 }
163
164 if ( $resize_opts ) {
165 $details['output']['resized'] = true;
166 }
167
168 $this->call_after_compress_callback();
169
170 return $details;
171 }
172
173 abstract protected function validate();
174 abstract protected function compress(
175 $input,
176 $resize_options,
177 $preserve_options,
178 $convert_to
179 );
180
181 protected static function identifier() {
182 return 'WordPress/' . Tiny_Plugin::wp_version() . ' Plugin/' . Tiny_Plugin::version();
183 }
184
185 private function call_after_compress_callback() {
186 if ( $this->after_compress_callback ) {
187 call_user_func( $this->after_compress_callback, $this );
188 }
189 }
190
191 private static function needs_resize( $file, $resize_options ) {
192 if ( ! $resize_options ) {
193 return false;
194 }
195
196 list($width, $height) = getimagesize( $file );
197
198 $should_resize_width = isset( $resize_options['width'] ) &&
199 $width > $resize_options['width'];
200 $should_resize_height = isset( $resize_options['height'] ) &&
201 $height > $resize_options['height'];
202
203 return $should_resize_width || $should_resize_height;
204 }
205
206 private static function compression_cost( $total ) {
207 $cost = 0;
208
209 if ( $total > 10000 ) {
210 $compressions = $total - 10000;
211 $cost += $compressions * 0.002;
212 $total -= $compressions;
213 }
214
215 if ( $total > 500 ) {
216 $compressions = $total - 500;
217 $cost += $compressions * 0.009;
218 $total -= $compressions;
219 }
220
221 return $cost;
222 }
223 }
224