| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Image Helper |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Helper_E2pdf_Image { |
| 17 |
|
| 18 |
private $helper; |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get Base64 Encoded Image |
| 26 |
* |
| 27 |
* @param string $image - Image path |
| 28 |
* |
| 29 |
* @return mixed - Base64 encoded image OR FALSE |
| 30 |
*/ |
| 31 |
public function get_image($image) { |
| 32 |
|
| 33 |
$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)); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (!$source) { |
| 45 |
if ($body = $this->get_by_url($image)) { |
| 46 |
$source = base64_encode($body); |
| 47 |
} elseif ($tmp_image = base64_decode($image, true)) { |
| 48 |
if ($this->get_image_extension($tmp_image)) { |
| 49 |
$source = $image; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
return $source; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get image by Url |
| 59 |
* |
| 60 |
* @param string $url - Url to image |
| 61 |
* |
| 62 |
* @return array(); |
| 63 |
*/ |
| 64 |
public function get_by_url($url) { |
| 65 |
|
| 66 |
$response = wp_remote_get($url, array( |
| 67 |
'timeout' => 30, |
| 68 |
'sslverify' => false |
| 69 |
)); |
| 70 |
|
| 71 |
if (wp_remote_retrieve_response_code($response) === 200) { |
| 72 |
return wp_remote_retrieve_body($response); |
| 73 |
} else { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function get_image_extension($image = false) { |
| 79 |
|
| 80 |
if (!$image) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$mime = false; |
| 85 |
if (function_exists('finfo_open') && function_exists('finfo_buffer')) { |
| 86 |
$f = finfo_open(); |
| 87 |
$mime = finfo_buffer($f, $image, FILEINFO_MIME_TYPE); |
| 88 |
} elseif (function_exists('image_type_to_mime_type') && function_exists('getimagesizefromstring')) { |
| 89 |
$size = getimagesizefromstring($image); |
| 90 |
if (isset($size['mime'])) { |
| 91 |
$mime = $size['mime']; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
switch ($mime) { |
| 96 |
case 'image/jpeg': |
| 97 |
$ext = "jpg"; |
| 98 |
break; |
| 99 |
|
| 100 |
case 'image/png': |
| 101 |
$ext = "png"; |
| 102 |
break; |
| 103 |
|
| 104 |
default: |
| 105 |
$ext = false; |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
return $ext; |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|