PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.6.13
TinyPNG – JPEG, PNG & WebP image compression v3.6.13
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 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-compress.php
221 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 [type] $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 throw new Tiny_Exception( self::KEY_MISSING, 'KeyError' );
113 }
114
115 if ( ! file_exists( $file ) ) {
116 throw new Tiny_Exception( self::FILE_MISSING, 'FileError' );
117 }
118
119 if ( ! is_writable( $file ) ) {
120 throw new Tiny_Exception( self::WRITE_ERROR, 'FileError' );
121 }
122
123 if ( ! $this->needs_resize( $file, $resize_opts ) ) {
124 $resize_opts = false;
125 }
126
127 try {
128 $file_data = file_get_contents( $file );
129
130 list($output, $details, $convert_output ) = $this->compress(
131 $file_data,
132 $resize_opts,
133 $preserve_opts,
134 $convert_to
135 );
136 } catch ( Tiny_Exception $err ) {
137 $this->call_after_compress_callback();
138 throw $err;
139 }
140
141 try {
142 file_put_contents( $file, $output );
143 } catch ( Exception $e ) {
144 throw new Tiny_Exception( $e->getMessage(), 'FileError' );
145 }
146
147 if ( $convert_output ) {
148 $converted_filepath = Tiny_Helpers::replace_file_extension(
149 $details['convert']['type'],
150 $file
151 );
152
153 try {
154 file_put_contents( $converted_filepath, $convert_output );
155 } catch ( Exception $e ) {
156 throw new Tiny_Exception( $e->getMessage(), 'FileError' );
157 }
158 $details['convert']['path'] = $converted_filepath;
159 }
160
161 if ( $resize_opts ) {
162 $details['output']['resized'] = true;
163 }
164
165 $this->call_after_compress_callback();
166
167 return $details;
168 }
169
170 abstract protected function validate();
171 abstract protected function compress(
172 $input,
173 $resize_options,
174 $preserve_options,
175 $convert_to
176 );
177
178 protected static function identifier() {
179 return 'WordPress/' . Tiny_Plugin::wp_version() . ' Plugin/' . Tiny_Plugin::version();
180 }
181
182 private function call_after_compress_callback() {
183 if ( $this->after_compress_callback ) {
184 call_user_func( $this->after_compress_callback, $this );
185 }
186 }
187
188 private static function needs_resize( $file, $resize_options ) {
189 if ( ! $resize_options ) {
190 return false;
191 }
192
193 list($width, $height) = getimagesize( $file );
194
195 $should_resize_width = isset( $resize_options['width'] ) &&
196 $width > $resize_options['width'];
197 $should_resize_height = isset( $resize_options['height'] ) &&
198 $height > $resize_options['height'];
199
200 return $should_resize_width || $should_resize_height;
201 }
202
203 private static function compression_cost( $total ) {
204 $cost = 0;
205
206 if ( $total > 10000 ) {
207 $compressions = $total - 10000;
208 $cost += $compressions * 0.002;
209 $total -= $compressions;
210 }
211
212 if ( $total > 500 ) {
213 $compressions = $total - 500;
214 $cost += $compressions * 0.009;
215 $total -= $compressions;
216 }
217
218 return $cost;
219 }
220 }
221