0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| <?php
class ImgToBase64 { static private $instance;
private function __construct() { }
static public function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance; }
function getClientArgs() { global $argv; array_shift($argv); $args = array(); array_walk($argv, function ($v, $k) use (&$args) { @list($key, $value) = @explode('=', $v); $args[$key] = $value; }); return $args; }
function imgToBase64($img_path = '') { $img = isset($img_path['path']) ? $img_path['path'] : ''; if (!$img) { return '图片不存在或图片路径不正确'; }
if ($fp = fopen($img, "rb", 0)) { $image = fread($fp, filesize($img)); fclose($fp);
$base64 = chunk_split(base64_encode($image)); $encode = 'data:image/jpg/png/gif;base64,' . $base64; echo $encode; } }
private function __clone() { } }
$img_path = ImgToBase64::getInstance()->getClientArgs(); ImgToBase64::getInstance()->imgToBase64($img_path);
|