|
1
|
|
|
<?php |
|
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Base64 Handler library. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* PHP Version 7 |
|
10
|
|
|
* |
|
11
|
|
|
* LICENSE: This source file is subject to the MIT license that is available |
|
12
|
|
|
* through the world-wide-web at the following URI: |
|
13
|
|
|
* http://opensource.org/licenses/mit-license.php |
|
14
|
|
|
* |
|
15
|
|
|
* @category Src |
|
16
|
|
|
* @package Normeno\Base64Hanlder |
|
17
|
|
|
* @author Nicolas Ormeno <[email protected]> |
|
18
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
19
|
|
|
* @link https://github.com/normeno/base64_handler |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Normeno\Base64Handler; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Base64Handler Class |
|
25
|
|
|
* |
|
26
|
|
|
* @category src |
|
27
|
|
|
* @package Normeno\Base64Handler |
|
28
|
|
|
* @author Nicolas Ormeno <[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
30
|
|
|
* @link https://github.com/normeno/base64_handler |
|
31
|
|
|
*/ |
|
32
|
|
|
class Base64Handler { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Convert file to base64 |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $path file path |
|
38
|
|
|
* |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function toBase64(string $path) |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
|
|
$isUrl = filter_var($path, FILTER_VALIDATE_URL); |
|
46
|
|
|
|
|
47
|
|
|
if (!$isUrl && !Utils::isValidPath($path)) { |
|
48
|
|
|
throw new \Exception('Invalid Path', 404); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$file = file_get_contents($path); |
|
52
|
|
|
|
|
53
|
|
|
$resp = Utils::getExtFromFile($path); |
|
54
|
|
|
$resp['base64'] = base64_encode($file); |
|
55
|
|
|
|
|
56
|
|
|
return $resp; |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
return "{$e->getMessage()} ({$e->getCode()})"; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Convert Base64 to File |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $base64 Base64 string |
|
66
|
|
|
* @param string $ext File's extension |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function toFile(string $base64, string $ext = '') |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
|
|
if (!Checker::isBase64($base64)) { |
|
74
|
|
|
throw new \Exception('Invalid Base64', 409); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$resp = null; |
|
78
|
|
|
|
|
79
|
|
|
if (Checker::isBase64Image($base64)) { |
|
80
|
|
|
$resp = Converter::base64ToImage($base64); |
|
81
|
|
|
} else { |
|
82
|
|
|
$resp = Converter::base64ToFile($base64, $ext); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $resp; |
|
86
|
|
|
} catch (\Exception $e) { |
|
87
|
|
|
return "{$e->getMessage()} ({$e->getCode()})"; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return base64 type |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $base64 Base64 string |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getBase64Type(string $base64) |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
if (!Checker::isBase64($base64)) { |
|
102
|
|
|
throw new \Exception('Invalid Base64', 409); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (Checker::isBase64Image($base64)) { |
|
106
|
|
|
return 'image'; |
|
107
|
|
|
} else { |
|
108
|
|
|
return 'unknown type'; |
|
109
|
|
|
} |
|
110
|
|
|
} catch (\Exception $e) { |
|
111
|
|
|
return "({$e->getCode()}) {$e->getMessage()}"; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |