| 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 |  |  |  * Checker 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 Converter | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * Convert image to base64 | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $file image's path | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @see https://stackoverflow.com/a/13758760/2901396 | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @return bool|string | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public static function imageToBase64($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $type = pathinfo($file, PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $data = file_get_contents($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return $base64; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Convert base64 to image | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $base64    Base64 string | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $dest      destination path | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @return array|string | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public static function base64ToImage(string $base64, string $dest = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($dest == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $dest = getcwd() . '/src/tmp'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $base64 = Utils::clearString($base64); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $resp = Utils::getExtFromBase64($base64); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $filename = md5(time().uniqid()) . '.' . $resp['ext']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $base64 = Utils::clearString($base64); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $resp['path'] = $dest . '/' . $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             file_put_contents($resp['path'], base64_decode($base64)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             unlink($resp['path']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return $resp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return "{$e->getMessage()}: ({$e->getCode()})"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Convert base64 to image | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $base64    Base64 string | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $ext       File's extension | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $dest      destination path | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @return array|string | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public static function base64ToFile(string $base64, string $ext = '', string $dest = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             if ($dest == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $dest = getcwd() . '/src/tmp'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $base64 = Utils::clearString($base64); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $resp = Utils::getExtFromBase64($base64); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $resp['ext'] = !empty($ext) ? $ext : $resp['ext']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $filename = md5(time().uniqid()) . '.' . $resp['ext']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $resp['path'] = $dest . '/' . $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             file_put_contents($resp['path'], base64_decode($base64)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             unlink($resp['path']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return $resp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return "{$e->getMessage()}: ({$e->getCode()})"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | } | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.