PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-image.php

e2pdf-image.php in E2Pdf – Export Pdf Tool for WordPress 1.32.49, at classes/helper/e2pdf-image.php

263 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2Pdf Image Helper
5 * @copyright Copyright 2017 https://e2pdf.com
6 * @license GPLv3
7 * @version 1
8 * @link https://e2pdf.com
9 * @since 0.00.01
10 */
11 if (!defined('ABSPATH')) {
12 die('Access denied.');
13 }
14
15 class Helper_E2pdf_Image {
16
17 private $helper;
18
19 public function __construct() {
20 $this->helper = Helper_E2pdf_Helper::instance();
21 }
22
23 /**
24 * Get Base64 Encoded Image
25 * @param string $value - Image path
26 * @return mixed - Base64 encoded image OR FALSE
27 */
28 public function get_image($value, $extension = false, $field = array()) {
29 $source = false;
30 $protected = false;
31 if ($value) {
32 preg_match('/src=(?:"|\')([^"\']*)(?:"|\')/', $value, $matches);
33 if (isset($matches[1])) {
34 $value = $matches[1];
35 }
36 $value = trim($value);
37 $site_url = site_url('/');
38 $https = str_replace('http://', 'https://', $site_url);
39 $http = str_replace('https://', 'http://', $site_url);
40 if (!get_option('e2pdf_images_remote_request', '0')) {
41 if (0 === strpos($value, $https)) {
42 $tmp_value = ABSPATH . substr($value, strlen($https));
43 if (@file_exists($tmp_value)) {
44 $value = $tmp_value;
45 }
46 } elseif (0 === strpos($value, $http)) {
47 $tmp_value = ABSPATH . substr($value, strlen($http));
48 if (@file_exists($tmp_value)) {
49 $value = $tmp_value;
50 }
51 }
52 }
53 if (!$source) {
54 if ((0 === strpos($value, ABSPATH) || 0 === strpos($value, '/')) && @file_exists($value) && $this->get_extension($value)) {
55 if ($extension == 'formidable' && class_exists('FrmProFileField') && !@is_readable($value)) {
56 FrmProFileField::chmod($value, apply_filters('frm_protected_file_readonly_permission', 0400));
57 if (@!is_readable($value)) {
58 @chmod($value, apply_filters('frm_protected_file_readonly_permission', 0400));
59 }
60 if (@is_readable($value)) {
61 $protected = true;
62 }
63 }
64 $contents = $this->get_optimized_image($value, $field);
65 if (!$contents) {
66 $contents = @file_get_contents($value);
67 }
68 if ($contents) {
69 $source = base64_encode($contents);
70 }
71 if ($protected) {
72 FrmProFileField::chmod($value, 0200);
73 }
74 } elseif (@file_exists(ABSPATH . $value) && $this->get_extension(ABSPATH . $value)) {
75 if ($extension == 'formidable' && class_exists('FrmProFileField') && !@is_readable(ABSPATH . $value)) {
76 FrmProFileField::chmod(ABSPATH . $value, apply_filters('frm_protected_file_readonly_permission', 0400));
77 if (@!is_readable(ABSPATH . $value)) {
78 @chmod(ABSPATH . $value, apply_filters('frm_protected_file_readonly_permission', 0400));
79 }
80 if (@is_readable(ABSPATH . $value)) {
81 $protected = true;
82 }
83 }
84 $contents = $this->get_optimized_image(ABSPATH . $value, $field);
85 if (!$contents) {
86 $contents = @file_get_contents(ABSPATH . $value);
87 }
88 if ($contents) {
89 $source = base64_encode($contents);
90 }
91 if ($protected) {
92 FrmProFileField::chmod(ABSPATH . $value, 0200);
93 }
94 } elseif (0 === strpos($value, 'data:image/')) {
95 $value = preg_replace('/^data:image\/[^;]+;base64,/', '', $value);
96 $tmp_file = base64_decode($value, true);
97 if ($this->get_extension($tmp_file)) {
98 $source = $value;
99 }
100 } elseif (0 === strpos($value, '<svg') || 0 === strpos($value, '<?xml') || (false !== strpos($value, '<svg') && false !== strpos($value, '</svg>'))) {
101 if (false === strpos($value, 'xmlns')) {
102 $value = str_replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"', $value);
103 }
104 if (0 !== strpos($value, '<svg') && 0 !== strpos($value, '<?xml')) {
105 $position = strpos($value, '<svg');
106 $value = substr($value, $position);
107 }
108 $source = base64_encode($value);
109 } elseif ($tmp_file = base64_decode($value, true)) {
110 if ($this->get_extension($tmp_file)) {
111 $source = $value;
112 }
113 } elseif ($body = $this->get_by_url($value)) {
114 $source = base64_encode($body);
115 }
116 }
117 }
118 return $source;
119 }
120
121 public function get_optimized_image($file, $field = array()) {
122 if (!empty($field['properties']['quality']) && $field['properties']['quality'] != '-1' && isset($field['width']) && isset($field['height'])) {
123 $quality = (int) $field['properties']['quality'];
124 $editor = wp_get_image_editor($file);
125 if (is_wp_error($editor) || !$editor->get_size() || !class_exists('ReflectionProperty')) {
126 return '';
127 }
128 $image_size = $editor->get_size();
129 if (($image_size['width'] > ((int) $field['width'] * $quality)) || ($image_size['height'] > ((int) $field['height'] * $quality))) {
130 $width = (int) $field['width'] * $quality;
131 $height = (int) $field['height'] * $quality;
132 } else {
133 return '';
134 }
135 if (is_wp_error($editor->resize($width, $height, false))) {
136 return '';
137 }
138 $editor->set_quality(100);
139 $reflection = new ReflectionProperty(get_class($editor), 'mime_type');
140 $reflection->setAccessible(true);
141 $reflection->getValue($editor);
142 $mime_type = $reflection->getValue($editor);
143
144 $reflection = new ReflectionProperty(get_class($editor), 'image');
145 $reflection->setAccessible(true);
146 $image = $reflection->getValue($editor);
147
148 if ($image && $editor instanceof WP_Image_Editor_GD) {
149 ob_start();
150 switch ($mime_type) {
151 case 'image/png':
152 imagepng($image);
153 break;
154 case 'image/gif':
155 imagegif($image);
156 break;
157 case 'image/webp':
158 if (function_exists('imagewebp')) {
159 imagewebp($image, null, 100);
160 }
161 break;
162 default:
163 imagejpeg($image, null, 100);
164 break;
165 }
166 $contents = ob_get_contents();
167 ob_end_clean();
168 return $contents;
169 } elseif ($image && $editor instanceof WP_Image_Editor_Imagick) {
170 $contents = $image->getImagesBlob();
171 return $contents;
172 }
173 }
174 return '';
175 }
176
177 public function get_base64_image($url) {
178 if ($ext = $this->get_extension($url)) {
179 $type = array_search($ext, $this->get_allowed_extensions());
180 if ($type !== false && $body = $this->get_by_url($url)) {
181 return 'data:' . $type . ';base64,' . base64_encode($body);
182 }
183 }
184 return $url;
185 }
186
187 /**
188 * Get image by Url
189 * @param string $url - Url to image
190 * @return array();
191 */
192 public function get_by_url($url) {
193 $response = wp_remote_get(
194 $url,
195 array(
196 'timeout' => get_option('e2pdf_images_timeout', '30'),
197 'sslverify' => false,
198 )
199 );
200 if (wp_remote_retrieve_response_code($response) === 200) {
201 return wp_remote_retrieve_body($response);
202 } else {
203 return '';
204 }
205 }
206
207 public function get_allowed_extensions() {
208 return array(
209 'image/jpeg' => 'jpg',
210 'image/jpeg2' => 'jpeg',
211 'image/png' => 'png',
212 'image/gif' => 'gif',
213 'image/svg' => 'svg',
214 'image/svg+xml' => 'svg',
215 'image/webp' => 'webp',
216 'image/x-ms-bmp' => 'bmp',
217 'image/bmp' => 'bmp',
218 'image/tif' => 'tiff',
219 'image/tiff' => 'tiff',
220 );
221 }
222
223 public function get_extension($value = false) {
224
225 $value = trim($value);
226 if (!$value) {
227 return false;
228 }
229
230 $extensions = $this->get_allowed_extensions();
231 $extension = false;
232 $mime = false;
233
234 if (@file_exists($value)) {
235 $file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION));
236 if (in_array($file_extension, $extensions)) {
237 return $file_extension;
238 } elseif (function_exists('finfo_open') && function_exists('finfo_file')) {
239 $f = finfo_open();
240 $mime = @finfo_file($f, $value, FILEINFO_MIME_TYPE);
241 }
242 } else {
243 $file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION));
244 if (in_array($file_extension, $extensions)) {
245 return $file_extension;
246 } elseif (function_exists('finfo_open') && function_exists('finfo_buffer')) {
247 $f = finfo_open();
248 $mime = finfo_buffer($f, $value, FILEINFO_MIME_TYPE);
249 } elseif (function_exists('image_type_to_mime_type') && function_exists('getimagesizefromstring')) {
250 $size = getimagesizefromstring($value);
251 if (isset($size['mime'])) {
252 $mime = $size['mime'];
253 }
254 }
255 }
256
257 if ($mime && isset($extensions[$mime])) {
258 $extension = $extensions[$mime];
259 }
260 return $extension;
261 }
262 }
263