Skip to content

Commit 898f8dd

Browse files
committed
Added webp and other misc helper function, improved CURL api calls
1 parent b61790b commit 898f8dd

6 files changed

Lines changed: 102 additions & 11 deletions

File tree

src/Http/BaseClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public static function download( $url, $save_filepath ) {
141141
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
142142
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, 1 );
143143
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
144+
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "Accept" => "application/json" ) );
145+
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
144146
$response = curl_exec( $ch );
145147
if ( false === $response ) {
146148
$curl_error = curl_error( $ch );
@@ -184,6 +186,7 @@ public static function get_user_agent() {
184186

185187
/**
186188
* Set the user agent
189+
*
187190
* @param $user_agent
188191
*/
189192
public static function set_user_agent( $user_agent ) {

src/Http/Client.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ public static function _get( $url, $api_key = null ) {
7979
curl_setopt( $ch, CURLOPT_FAILONERROR, 0 );
8080
curl_setopt( $ch, CURLOPT_TIMEOUT, self::TIMEOUT );
8181
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
82+
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
83+
84+
// Setup headers
85+
$headers = array( 'Accept: application/json' );
8286
if ( ! is_null( $api_key ) ) {
83-
curl_setopt(
84-
$ch, CURLOPT_HTTPHEADER, array(
85-
self::AUTH_HEADER . ': ' . $api_key
86-
)
87-
);
87+
array_push( $headers, self::AUTH_HEADER . ': ' . $api_key );
8888
}
89+
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
8990

9091
$response = curl_exec( $ch );
9192
curl_close( $ch );
@@ -127,13 +128,15 @@ public static function _post( $url, $data, $files = array(), $api_key = null ) {
127128
curl_setopt( $ch, CURLOPT_FAILONERROR, 0 );
128129
curl_setopt( $ch, CURLOPT_TIMEOUT, self::TIMEOUT );
129130
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
131+
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
132+
133+
// Setup headers
134+
$headers = array( 'Accept: application/json' );
130135
if ( ! is_null( $api_key ) ) {
131-
curl_setopt(
132-
$ch, CURLOPT_HTTPHEADER, array(
133-
self::AUTH_HEADER . ': ' . $api_key
134-
)
135-
);
136+
array_push( $headers, self::AUTH_HEADER . ': ' . $api_key );
136137
}
138+
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
139+
137140
$response = curl_exec( $ch );
138141
if ( false === $response ) {
139142
$curl_error = curl_error( $ch );

src/Optimizer.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Optimizer {
3939
const RESOURCE_FILES = 'files';
4040
const MAX_ALLOWED_RESOURCES = 5;
4141

42-
const VERSION = '1.0.3';
42+
const VERSION = '1.0.4';
4343
/**
4444
* @var string
4545
*/
@@ -305,4 +305,19 @@ public function get_user_info() {
305305
return $this->service->get_user_info();
306306
}
307307

308+
309+
/**
310+
* Is valid compression level?
311+
*
312+
* @param $level
313+
*
314+
* @return bool
315+
*/
316+
public static function valid_compression_level($level) {
317+
return in_array($level, array(
318+
self::COMPRESSION_LOSSLESS,
319+
self::COMPRESSION_INTELLIGENT,
320+
self::COMPRESSION_ULTRA,
321+
));
322+
}
308323
}

src/Responses/Response.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,19 @@ public function getRawResponse() {
230230
public function getProcessId() {
231231
return $this->process_id;
232232
}
233+
234+
/**
235+
* Returns the result by file name
236+
* @param $file_name
237+
*
238+
* @return Result|mixed|null
239+
*/
240+
public function getResultByFileName($file_name) {
241+
foreach($this->result as $result) {
242+
if($result->getFileName() === $file_name) {
243+
return $result;
244+
}
245+
}
246+
return null;
247+
}
233248
}

src/Responses/Result.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class Result implements IFile {
6262
*/
6363
private $webp;
6464

65+
/**
66+
* If image optimization saved percent is equal or less than 5% it wont count so this will be 0.
67+
* @var int
68+
*/
69+
private $success;
70+
6571
/**
6672
* The local file
6773
* If this is not null it means a file by path was optimized and this is it's local path.
@@ -97,6 +103,9 @@ public function __construct( $result ) {
97103
if ( isset( $result->url ) ) {
98104
$this->url = $result->url;
99105
}
106+
if ( isset( $result->success ) ) {
107+
$this->success = intval($result->success);
108+
}
100109
if ( isset( $result->webp ) ) {
101110
$this->webp = new ResultWebP($result);
102111
}
@@ -168,6 +177,21 @@ public function getWebP() {
168177
return $this->webp;
169178
}
170179

180+
/**
181+
* Check if the attachment was optimized.
182+
* @return bool
183+
*/
184+
public function isSuccessfullyOptimized() {
185+
return $this->success == 1;
186+
}
187+
188+
/**
189+
* Check if the attachment was already optimized.
190+
* @return bool
191+
*/
192+
public function isAlreadyOptimized() {
193+
return $this->success == 0;
194+
}
171195

172196
/**
173197
* Overwrite the local file with the optimized file

src/Responses/ResultWebP.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
namespace MegaOptim\Responses;
2222

23+
use MegaOptim\Tools\FileSystem;
24+
use MegaOptim\Http\Client;
25+
2326
class ResultWebP {
2427
public $url;
2528
public $optimized_size;
@@ -34,4 +37,32 @@ public function __construct($response) {
3437
$this->saved_percent = $response->webp->saved_percent;
3538
}
3639
}
40+
41+
/**
42+
* Save the optimized file to the full path
43+
*
44+
* @param $path
45+
*
46+
* @return mixed
47+
* @throws \Exception
48+
*/
49+
public function saveAsFile( $path ) {
50+
if(is_null($this->url)) {
51+
return false;
52+
}
53+
FileSystem::maybe_prepare_output_dir( $path );
54+
if ( ! Client::download( $this->url, $path ) ) {
55+
throw new \Exception( 'Unable to overwrite the local file.' );
56+
}
57+
return $path;
58+
59+
}
60+
61+
/**
62+
* Return the saved bytes
63+
* @return mixed
64+
*/
65+
public function getSavedBytes() {
66+
return $this->saved_bytes;
67+
}
3768
}

0 commit comments

Comments
 (0)