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-compress.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-compress.php
168 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 abstract class Tiny_Compress {
22 const KEY_MISSING = 'Register an account or provide an API key first';
23 const FILE_MISSING = 'File does not exist';
24 const WRITE_ERROR = 'No permission to write to file';
25
26 protected $after_compress_callback;
27
28 public static function create( $api_key, $after_compress_callback = null ) {
29 if ( Tiny_PHP::client_supported() ) {
30 $class = 'Tiny_Compress_Client';
31 } elseif ( Tiny_PHP::fopen_available() ) {
32 $class = 'Tiny_Compress_Fopen';
33 } else {
34 throw new Tiny_Exception(
35 'No HTTP client is available (cURL or fopen)',
36 'NoHttpClient'
37 );
38 }
39 return new $class($api_key, $after_compress_callback);
40 }
41
42 /* Based on pricing April 2016. */
43 public static function estimate_cost( $compressions, $usage ) {
44 return round(
45 self::compression_cost( $compressions + $usage ) -
46 self::compression_cost( $usage ),
47 2
48 );
49 }
50
51 protected function __construct( $after_compress_callback ) {
52 $this->after_compress_callback = $after_compress_callback;
53 }
54
55 public abstract function can_create_key();
56 public abstract function get_compression_count();
57 public abstract function get_key();
58 public abstract function limit_reached();
59
60 public function get_status() {
61 if ( $this->get_key() == null ) {
62 return (object) array(
63 'ok' => false,
64 'message' => self::KEY_MISSING,
65 );
66 }
67
68 $result = false;
69 $message = null;
70
71 try {
72 $result = $this->validate();
73 } catch ( Tiny_Exception $err ) {
74 if ( $err->get_status() == 401 ) {
75 $message = 'The key that you have entered is not valid';
76 } else {
77 list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 );
78 }
79 }
80
81 $this->call_after_compress_callback();
82
83 return (object) array(
84 'ok' => $result,
85 'message' => $message,
86 );
87 }
88
89 public function compress_file( $file, $resize_opts = array(), $preserve_opts = array() ) {
90 if ( $this->get_key() == null ) {
91 throw new Tiny_Exception( self::KEY_MISSING, 'KeyError' );
92 }
93
94 if ( ! file_exists( $file ) ) {
95 throw new Tiny_Exception( self::FILE_MISSING, 'FileError' );
96 }
97
98 if ( ! is_writable( $file ) ) {
99 throw new Tiny_Exception( self::WRITE_ERROR, 'FileError' );
100 }
101
102 if ( ! $this->needs_resize( $file, $resize_opts ) ) {
103 $resize_opts = false;
104 }
105
106 try {
107 list( $output, $details ) = $this->compress(
108 file_get_contents( $file ),
109 $resize_opts,
110 $preserve_opts
111 );
112 } catch ( Tiny_Exception $err ) {
113 $this->call_after_compress_callback();
114 throw $err;
115 }
116
117 $this->call_after_compress_callback();
118 file_put_contents( $file, $output );
119
120 if ( $resize_opts ) {
121 $details['output']['resized'] = true;
122 }
123
124 return $details;
125 }
126
127 protected abstract function validate();
128 protected abstract function compress( $input, $resize_options, $preserve_options );
129
130 protected static function identifier() {
131 return 'WordPress/' . Tiny_Plugin::wp_version() . ' Plugin/' . Tiny_Plugin::version();
132 }
133
134 private function call_after_compress_callback() {
135 if ( $this->after_compress_callback ) {
136 call_user_func( $this->after_compress_callback, $this );
137 }
138 }
139
140 private static function needs_resize( $file, $resize_options ) {
141 if ( ! $resize_options ) {
142 return false;
143 }
144
145 list($width, $height) = getimagesize( $file );
146
147 return ( $width > $resize_options['width'] || $height > $resize_options['height'] );
148 }
149
150 private static function compression_cost( $total ) {
151 $cost = 0;
152
153 if ( $total > 10000 ) {
154 $compressions = $total - 10000;
155 $cost += $compressions * 0.002;
156 $total -= $compressions;
157 }
158
159 if ( $total > 500 ) {
160 $compressions = $total - 500;
161 $cost += $compressions * 0.009;
162 $total -= $compressions;
163 }
164
165 return $cost;
166 }
167 }
168