| @@ -1,11 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * E2pdf Image Helper | |
| 5 | - * | |
| 4 | + * E2Pdf Image Helper | |
| 6 | 5 | * @copyright Copyright 2017 https://e2pdf.com |
| 7 | - * @license GPL v2 | |
| 6 | + * @license GPLv3 | |
| 8 | 7 | * @version 1 |
| 9 | 8 | * @link https://e2pdf.com |
| 10 | 9 | * @since 0.00.01 |
| 11 | 10 | */ |
| @@ -22,34 +21,98 @@ | ||
| 22 | 21 | } |
| 23 | 22 | |
| 24 | 23 | /** |
| 25 | 24 | * Get Base64 Encoded Image |
| 26 | - * | |
| 27 | - * @param string $image - Image path | |
| 28 | - * | |
| 25 | + * @param string $value - Image path | |
| 29 | 26 | * @return mixed - Base64 encoded image OR FALSE |
| 30 | 27 | */ |
| 31 | - public function get_image($image) { | |
| 32 | - | |
| 28 | + public function get_image($value, $extension = false, $field = array()) { | |
| 33 | 29 | $source = false; |
| 34 | - | |
| 35 | - if ($image) { | |
| 36 | - | |
| 37 | - if (0 === strpos($image, site_url())) { | |
| 38 | - $image_path = $this->helper->get('plugin_dir') . '../../../' . substr($image, strlen(site_url())); | |
| 39 | - if (file_exists($image_path)) { | |
| 40 | - $source = base64_encode(file_get_contents($image_path)); | |
| 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 | + } | |
| 41 | 51 | } |
| 42 | 52 | } |
| 43 | - | |
| 44 | 53 | if (!$source) { |
| 45 | - $response = $this->get_url($image); | |
| 46 | - if (array_key_exists('content', $response) && $response['content'] !== FALSE) { | |
| 47 | - $source = base64_encode($response['content']); | |
| 48 | - } elseif ($tmp_image = base64_decode($image, true)) { | |
| 49 | - if ($this->get_image_extension($tmp_image)) { | |
| 50 | - $source = $image; | |
| 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 | + } | |
| 51 | 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); | |
| 52 | 115 | } |
| 53 | 116 | } |
| 54 | 117 | } |
| 55 | 118 | return $source; |
| @@ -54,54 +117,146 @@ | ||
| 54 | 117 | } |
| 55 | 118 | return $source; |
| 56 | 119 | } |
| 57 | 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 | + | |
| 58 | 187 | /** |
| 59 | 188 | * Get image by Url |
| 60 | - * | |
| 61 | 189 | * @param string $url - Url to image |
| 62 | - * | |
| 63 | 190 | * @return array(); |
| 64 | 191 | */ |
| 65 | - public function get_url($url) { | |
| 66 | - $content = @file_get_contents($url); | |
| 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() { | |
| 67 | 208 | return array( |
| 68 | - 'headers' => $http_response_header, | |
| 69 | - 'content' => $content | |
| 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', | |
| 70 | 220 | ); |
| 71 | 221 | } |
| 72 | 222 | |
| 73 | - public function get_image_extension($image = false) { | |
| 223 | + public function get_extension($value = false) { | |
| 74 | 224 | |
| 75 | - if (!$image) { | |
| 225 | + $value = trim($value); | |
| 226 | + if (!$value) { | |
| 76 | 227 | return false; |
| 77 | 228 | } |
| 78 | 229 | |
| 230 | + $extensions = $this->get_allowed_extensions(); | |
| 231 | + $extension = false; | |
| 79 | 232 | $mime = false; |
| 80 | - if (function_exists('finfo_open') && function_exists('finfo_buffer')) { | |
| 81 | - $f = finfo_open(); | |
| 82 | - $mime = finfo_buffer($f, $image, FILEINFO_MIME_TYPE); | |
| 83 | - } elseif (function_exists('image_type_to_mime_type') && function_exists('getimagesizefromstring')) { | |
| 84 | - $size = getimagesizefromstring($image); | |
| 85 | - if (isset($size['mime'])) { | |
| 86 | - $mime = $size['mime']; | |
| 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); | |
| 87 | 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 | + } | |
| 88 | 255 | } |
| 89 | 256 | |
| 90 | - switch ($mime) { | |
| 91 | - case 'image/jpeg': | |
| 92 | - $ext = "jpg"; | |
| 93 | - break; | |
| 94 | - | |
| 95 | - case 'image/png': | |
| 96 | - $ext = "png"; | |
| 97 | - break; | |
| 98 | - | |
| 99 | - default: | |
| 100 | - $ext = false; | |
| 101 | - break; | |
| 257 | + if ($mime && isset($extensions[$mime])) { | |
| 258 | + $extension = $extensions[$mime]; | |
| 102 | 259 | } |
| 103 | - | |
| 104 | - return $ext; | |
| 260 | + return $extension; | |
| 105 | 261 | } |
| 106 | - | |
| 107 | 262 | } |