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-client.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-client.php
235 lines
1 <?php
2 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
3 /*
4 * Tiny Compress Images - WordPress plugin.
5 * Copyright (C) 2015-2018 Tinify B.V.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc., 51
19 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 if ( ! defined( '\Tinify\VERSION' ) ) {
23 /* Load vendored client if it is not yet loaded. */
24 require_once __DIR__ . '/vendor/tinify/Tinify/Exception.php';
25 require_once __DIR__ . '/vendor/tinify/Tinify/ResultMeta.php';
26 require_once __DIR__ . '/vendor/tinify/Tinify/Result.php';
27 require_once __DIR__ . '/vendor/tinify/Tinify/Source.php';
28 require_once __DIR__ . '/vendor/tinify/Tinify/Client.php';
29 require_once __DIR__ . '/vendor/tinify/Tinify.php';
30 }
31
32 class Tiny_Compress_Client extends Tiny_Compress {
33
34 /**
35 * API request timeout in seconds.
36 * 2026-01-14 showed 99% was < 120s
37 *
38 * @since 3.6.8
39 * @var int
40 */
41 const API_TIMEOUT = 120;
42
43 /**
44 * Connection setup timeout
45 *
46 * @since 3.6.8
47 * @var int
48 */
49 const CONNECT_TIMEOUT = 8;
50
51 private $last_error_code = 0;
52 private $last_message = '';
53 private $proxy;
54
55 protected function __construct( $api_key, $after_compress_callback ) {
56 parent::__construct( $after_compress_callback );
57
58 $this->proxy = new WP_HTTP_Proxy();
59
60 \Tinify\setAppIdentifier( self::identifier() );
61 \Tinify\setKey( $api_key );
62 }
63
64 public function can_create_key() {
65 return true;
66 }
67
68 public function get_compression_count() {
69 return \Tinify\getCompressionCount();
70 }
71
72 public function get_remaining_credits() {
73 return \Tinify\remainingCredits();
74 }
75
76 public function get_paying_state() {
77 return \Tinify\payingState();
78 }
79
80 public function get_email_address() {
81 return \Tinify\emailAddress();
82 }
83
84 public function get_key() {
85 return \Tinify\getKey();
86 }
87
88 protected function validate() {
89 try {
90 $this->last_error_code = 0;
91 $this->set_request_options( \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS ) );
92 \Tinify\Tinify::getClient()->request( 'get', '/keys/' . $this->get_key() );
93 return true;
94 } catch ( \Tinify\Exception $err ) {
95 $this->last_error_code = $err->status;
96
97 if ( 429 == $err->status || 400 == $err->status ) {
98 return true;
99 }
100
101 throw new Tiny_Exception(
102 $err->getMessage(),
103 get_class( $err ),
104 $err->status
105 );
106 }
107 }
108
109 protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) {
110 try {
111 $this->last_error_code = 0;
112 $this->set_request_options( \Tinify\Tinify::getClient() );
113 $source = \Tinify\fromBuffer( $input );
114
115 if ( $resize_opts ) {
116 $source = $source->resize( $resize_opts );
117 }
118
119 if ( $preserve_opts ) {
120 $source = $source->preserve( $preserve_opts );
121 }
122
123 $compress_result = $source->result();
124 $meta = array(
125 'input' => array(
126 'size' => strlen( $input ),
127 'type' => Tiny_Helpers::get_mimetype( $input ),
128 ),
129 'output' => array(
130 'size' => $compress_result->size(),
131 'type' => $compress_result->mediaType(),
132 'width' => $compress_result->width(),
133 'height' => $compress_result->height(),
134 'ratio' => round( $compress_result->size() / strlen( $input ), 4 ),
135 ),
136 );
137
138 $buffer = $compress_result->toBuffer();
139 $result = array( $buffer, $meta, null );
140
141 if ( count( $convert_to ) > 0 ) {
142 $convert_source = $source->convert(
143 array(
144 'type' => $convert_to,
145 )
146 );
147 $convert_result = $convert_source->result();
148 $meta['convert'] = array(
149 'type' => $convert_result->mediaType(),
150 'size' => $convert_result->size(),
151 );
152 $convert_buffer = $convert_result->toBuffer();
153 $result = array( $buffer, $meta, $convert_buffer );
154 }
155
156 return $result;
157 } catch ( \Tinify\Exception $err ) {
158 $this->last_error_code = $err->status;
159
160 Tiny_Logger::error(
161 'client compress error',
162 array(
163 'error' => $err->getMessage(),
164 'status' => $err->status,
165 )
166 );
167
168 throw new Tiny_Exception(
169 $err->getMessage(),
170 get_class( $err ),
171 $err->status
172 );
173 } // End try().
174 }
175
176 public function create_key( $email, $options ) {
177 try {
178 $this->last_error_code = 0;
179 $this->set_request_options(
180 \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS )
181 );
182
183 \Tinify\createKey( $email, $options );
184 } catch ( \Tinify\Exception $err ) {
185 $this->last_error_code = $err->status;
186
187 throw new Tiny_Exception(
188 $err->getMessage(),
189 get_class( $err ),
190 $err->status
191 );
192 }
193 }
194
195 private function set_request_options( $client ) {
196 /*
197 The client does not let us override cURL properties yet, so we have
198 to use a reflection property. */
199 $property = new ReflectionProperty( $client, 'options' );
200 if ( PHP_VERSION_ID < 80100 ) {
201 /**
202 * No effect since PHP 8.1 and deprecated since PHP 8.5
203 * https://www.php.net/manual/en/reflectionproperty.setaccessible.php
204 */
205 $property->setAccessible( true );
206 }
207 $options = $property->getValue( $client );
208
209 // Set API request timeout to prevent indefinite hanging
210 $options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT;
211 $options[ CURLOPT_CONNECTTIMEOUT ] = self::CONNECT_TIMEOUT;
212
213 if ( TINY_DEBUG ) {
214 $file = fopen( __DIR__ . '/curl.log', 'w' );
215 if ( is_resource( $file ) ) {
216 $options[ CURLOPT_VERBOSE ] = true;
217 $options[ CURLOPT_STDERR ] = $file;
218 }
219 }
220
221 if ( $this->proxy->is_enabled() && $this->proxy->send_through_proxy( $url ) ) {
222 $options[ CURLOPT_PROXYTYPE ] = CURLPROXY_HTTP;
223 $options[ CURLOPT_PROXY ] = $this->proxy->host();
224 $options[ CURLOPT_PROXYPORT ] = $this->proxy->port();
225
226 if ( $this->proxy->use_authentication() ) {
227 $options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY;
228 $options[ CURLOPT_PROXYUSERPWD ] = $this->proxy->authentication();
229 }
230 }
231
232 $property->setValue( $client, $options );
233 }
234 }
235