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-client.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-client.php
175 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 if ( ! defined( '\Tinify\VERSION' ) ) {
22 /* Load vendored client if it is not yet loaded. */
23 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Exception.php';
24 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/ResultMeta.php';
25 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Result.php';
26 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Source.php';
27 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Client.php';
28 require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify.php';
29 }
30
31 class Tiny_Compress_Client extends Tiny_Compress {
32 private $last_error_code = 0;
33 private $last_message = '';
34 private $proxy;
35
36 protected function __construct( $api_key, $after_compress_callback ) {
37 parent::__construct( $after_compress_callback );
38
39 $this->proxy = new WP_HTTP_Proxy();
40
41 \Tinify\setAppIdentifier( self::identifier() );
42 \Tinify\setKey( $api_key );
43 }
44
45 public function can_create_key() {
46 return true;
47 }
48
49 public function get_compression_count() {
50 return \Tinify\getCompressionCount();
51 }
52
53 public function get_key() {
54 return \Tinify\getKey();
55 }
56
57 public function limit_reached() {
58 return 429 == $this->last_error_code;
59 }
60
61 protected function validate() {
62 try {
63 $this->last_error_code = 0;
64 $this->set_request_options( \Tinify\Tinify::getClient() );
65
66 \Tinify\Tinify::getClient()->request( 'post', '/shrink' );
67 return true;
68
69 } catch ( \Tinify\Exception $err ) {
70 $this->last_error_code = $err->status;
71
72 if ( 429 == $err->status || 400 == $err->status ) {
73 return true;
74 }
75
76 throw new Tiny_Exception(
77 $err->getMessage(),
78 get_class( $err ),
79 $err->status
80 );
81 }
82 }
83
84 protected function compress( $input, $resize_opts, $preserve_opts ) {
85 try {
86 $this->last_error_code = 0;
87 $this->set_request_options( \Tinify\Tinify::getClient() );
88
89 $source = \Tinify\fromBuffer( $input );
90
91 if ( $resize_opts ) {
92 $source = $source->resize( $resize_opts );
93 }
94
95 if ( $preserve_opts ) {
96 $source = $source->preserve( $preserve_opts );
97 }
98
99 $result = $source->result();
100
101 $meta = array(
102 'input' => array(
103 'size' => strlen( $input ),
104 'type' => $result->mediaType(),
105 ),
106 'output' => array(
107 'size' => $result->size(),
108 'type' => $result->mediaType(),
109 'width' => $result->width(),
110 'height' => $result->height(),
111 'ratio' => round( $result->size() / strlen( $input ), 4 ),
112 ),
113 );
114
115 $buffer = $result->toBuffer();
116 return array( $buffer, $meta );
117
118 } catch ( \Tinify\Exception $err ) {
119 $this->last_error_code = $err->status;
120
121 throw new Tiny_Exception(
122 $err->getMessage(),
123 get_class( $err ),
124 $err->status
125 );
126 }// End try().
127 }
128
129 public function create_key( $email, $options ) {
130 try {
131 $this->last_error_code = 0;
132 $this->set_request_options(
133 \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS )
134 );
135
136 \Tinify\createKey( $email, $options );
137 } catch ( \Tinify\Exception $err ) {
138 $this->last_error_code = $err->status;
139
140 throw new Tiny_Exception(
141 $err->getMessage(),
142 get_class( $err ),
143 $err->status
144 );
145 }
146 }
147
148 private function set_request_options( $client ) {
149 /* The client does not let us override cURL properties yet, so we have
150 to use a reflection property. */
151 $property = new ReflectionProperty( $client, 'options' );
152 $property->setAccessible( true );
153 $options = $property->getValue( $client );
154
155 if ( TINY_DEBUG ) {
156 $file = fopen( dirname( __FILE__ ) . '/curl.log', 'w' );
157 if ( is_resource( $file ) ) {
158 $options[ CURLOPT_VERBOSE ] = true;
159 $options[ CURLOPT_STDERR ] = $file;
160 }
161 }
162
163 if ( $this->proxy->is_enabled() && $this->proxy->send_through_proxy( $url ) ) {
164 $options[ CURLOPT_PROXYTYPE ] = CURLPROXY_HTTP;
165 $options[ CURLOPT_PROXY ] = $this->proxy->host();
166 $options[ CURLOPT_PROXYPORT ] = $this->proxy->port();
167
168 if ( $this->proxy->use_authentication() ) {
169 $options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY;
170 $options[ CURLOPT_PROXYUSERPWD ] = $this->proxy->authentication();
171 }
172 }
173 }
174 }
175