PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web-utils / tenweb-image-optimizer / src / TenWebIO / Compress.php
tenweb-speed-optimizer / vendor / 10web-utils / tenweb-image-optimizer / src / TenWebIO Last commit date
Exceptions 4 years ago Queue 2 years ago Views 2 years ago Api.php 2 years ago Attachments.php 2 years ago Backup.php 2 years ago CLI.php 3 years ago Compress.php 2 years ago CompressDataService.php 3 years ago CompressService.php 3 years ago Config.php 2 years ago Init.php 2 years ago LastCompress.php 3 years ago Logs.php 2 years ago PreInit.php 3 years ago Rest.php 2 years ago Settings.php 3 years ago Utils.php 1 year ago
Compress.php
212 lines
1 <?php
2
3 namespace TenWebIO;
4
5 use TenWebIO\Exceptions\IOException;
6 use TenWebWpTransients\OptimizerTransients;
7
8 class Compress
9 {
10 private $queue_type;
11 private $url;
12 private $post_id;
13 private $thumb_size;
14
15 private $destination;
16 private $absolute_path;
17
18 private $compress_response = array();
19 private $compress_settings;
20
21 /**
22 * @param $queue_name
23 * @param $url
24 * @param int $post_id
25 * @param string $thumb_size
26 */
27 public function __construct($queue_name, $url, $post_id = 0, $thumb_size = 'full')
28 {
29 $this->url = $url;
30 $this->post_id = (int)$post_id;
31 $this->thumb_size = $thumb_size;
32
33 $data = Utils::getAttachmentData($url);
34 $this->destination = $data['destination'];
35 $this->absolute_path = $data['absolute_path'];
36
37 $this->compress_settings = new Settings(false);
38
39 $this->queue_type = Utils::getQueueTypeByName($queue_name);
40 update_site_option(TENWEBIO_PREFIX . '_compress_images_counter_' . $this->queue_type, get_site_option(TENWEBIO_PREFIX . '_compress_images_counter_' . $this->queue_type, 0) + 1);
41 }
42
43 /**
44 *
45 * @return void
46 * @throws IOException
47 */
48 public function compress()
49 {
50 $settings = $this->compress_settings->getSettings(false, 1, 1);
51 Logs::setLog("compress:settings:log", $settings);
52 if (!$this->checkImage()) {
53 throw new IOException('Error during compress request. Wrong mime type for ' . $this->url);
54 }
55 $this->compressRequest();
56 if (!$this->compress_response) {
57 throw new IOException('Error during compress request');
58 }
59 $compress_response = $this->compress_response;
60 if (!empty($compress_response['aws_url'])) {
61 if (!empty($settings['keep_originals'])) {
62 $this->keepOriginals();
63 }
64 }
65 $convert_webp = !empty($settings) ? (int)$settings['convert_webp'] : 1;
66 if (!$this->download($convert_webp)) {
67 throw new IOException('Error during download from s3');
68 }
69 if (!$this->checkHashes()) {
70 throw new IOException('Not matching image hashes.');
71 }
72 $this->saveLastCompress();
73 }
74
75 /**
76 * @return void
77 * @throws IOException
78 */
79 public function compressRequest()
80 {
81 $action = strpos($this->queue_type, 'custom_') !== false ? Api::API_COMPRESS_CUSTOM_ACTION : Api::API_COMPRESS_ACTION;
82 $page_id = Utils::getCustomQueueIdByType($this->queue_type);
83 $api_instance = new Api($action);
84 $origin = get_site_option(TENWEBIO_PREFIX . '_custom_compress_origin_' . $page_id);
85 $this->compress_response = $api_instance->apiRequest('POST', array(
86 "wp_options" => array(
87 "attachment_id" => $this->post_id,
88 "thumb_size" => $this->thumb_size,
89 "page_id" => $page_id
90 ),
91 "url" => $this->url,
92 "total" => get_site_option(TENWEBIO_PREFIX . '_compress_images_count_' . $this->queue_type, 0),
93 "counter" => get_site_option(TENWEBIO_PREFIX . '_compress_images_counter_' . $this->queue_type, 0),
94 "only_convert_webp" => get_site_option(TENWEBIO_PREFIX . '_custom_compress_only_convert_webp_' . $page_id),
95 "origin" => $origin ? $origin : OptimizerTransients::get(TENWEBIO_PREFIX . '_origin')
96 ));
97 if ($api_instance->getResponseStatusCode() === 400 ||
98 $api_instance->getResponseStatusCode() === 401 ||
99 $api_instance->getResponseStatusCode() === 403 ||
100 $api_instance->getResponseStatusCode() === 503) {
101 throw new IOException('finish_queue');
102 }
103 }
104
105 /**
106 * @return void
107 */
108 public function keepOriginals()
109 {
110 $backup = new Backup();
111 $backup->backupBeforeReplace($this->absolute_path, $this->destination);
112 Logs::setLog("compress:backup:" . $this->url . ":log", 'finished');
113 }
114
115 /**
116 *
117 * @param int $with_webp
118 *
119 * @return bool
120 * @throws IOException
121 */
122 public function download($with_webp = 1)
123 {
124 $compress_response = $this->compress_response;
125 if ((int)$compress_response['final_size'] > (int)$compress_response['orig_size']) {
126 return true;
127 }
128
129 return $this->downloadAssignedUrl($with_webp);
130 }
131
132 /**
133 *
134 * @return void
135 */
136 public function saveLastCompress()
137 {
138 $compress_images_counter = get_site_option(TENWEBIO_PREFIX . '_compress_images_counter_' . $this->queue_type);
139 $last_force = $compress_images_counter == 1;
140 $last_compress = new LastCompress($last_force);
141 $response = $this->compress_response;
142 $orig_size = !empty($response['orig_size']) ? $response['orig_size'] : 0;
143 $size = !empty($response['final_size']) ? $response['final_size'] : 0;
144 $last_compress->update($size, $orig_size);
145
146 Logs::setLog("compress:last:" . $this->url . ":log", array('orig_size' => $orig_size, 'final_size' => $size));
147 }
148
149
150 /**
151 * @param $with_webp
152 *
153 * @return bool
154 */
155 private function downloadAssignedUrl($with_webp = 1)
156 {
157 $aws_webp = $aws = false;
158 $compress_response = $this->compress_response;
159 if (!empty($compress_response['aws_signed_webp_url']) && $with_webp) {
160 $aws_webp = Utils::downloadFile($compress_response['aws_signed_webp_url'], $this->destination . '/' . basename($this->url) . '.webp');
161 Utils::storeWebPLog($this->url . '.webp');
162 }
163 if (!empty($compress_response['aws_signed_url'])) {
164 $url = parse_url($compress_response['aws_signed_url'], PHP_URL_PATH);
165 $aws = Utils::downloadFile($compress_response['aws_signed_url'], $this->destination . '/' . basename($url));
166 }
167
168 return ($aws_webp || $aws);
169
170 }
171
172 /**
173 * @return bool
174 */
175 private function checkHashes()
176 {
177 $checked = false;
178 $compress_response = $this->compress_response;
179 $url = parse_url($compress_response['aws_signed_url'], PHP_URL_PATH);
180 $downloaded_file = $this->destination . '/' . basename($url);
181 if (is_file($downloaded_file)) {
182 $downloaded_file_hash = hash_file("sha256", $downloaded_file);
183 if ($compress_response['image_hash'] !== $downloaded_file_hash) {
184 unlink($downloaded_file);
185 } else {
186 $checked = true;
187 rename($downloaded_file, $this->destination . '/' . basename($this->url));
188 }
189 }
190
191 return $checked;
192 }
193
194 /**
195 * @return bool
196 */
197 private function checkImage()
198 {
199 $data = getimagesize($this->absolute_path);
200 if (!$data) {
201 return false;
202 }
203 $allowed_mime_types = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
204 if (!empty($data['mime']) && in_array(strtolower($data['mime']), $allowed_mime_types)) {
205 return true;
206 }
207
208 return false;
209 }
210
211 }
212