美文网首页
php-base64 图片互转

php-base64 图片互转

作者: 若初_终点 | 来源:发表于2020-04-04 10:53 被阅读0次

    ............................................................................................................................................................................................................................................

    //        $base64_image_content = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXcAAAGQCAYAAABVgSDEAAANzklEQVR4Xu3UwW0UURRE0WeJPGB+WJAMycCSYAjgj0nEQmxBQrZltdq3z6xnprtOPdXD+BAgQIBATuAhl0ggAgQIEBjj7ggIECAQFDDuwVJFIkCAgHF3AwQIEAgKGPdgqSIRIEDAuLsBAgQIBAWMe7BUkQgQIGDc3QABAgSCAsY9WKpIBAgQMO5ugAABAkEB4x4sVSQCBAgYdzdAgACBoIBxD5YqEgECBIy7GyBAgEBQwLgHSxWJAAECxt0NECBAIChg3IOlikSAAAHj7gYIECAQFDDuwVJFIkCAgHF3AwQIEAgKGPdgqSIRIEDAuLsBAgQIBAWMe7BUkQgQIGDc3QABAgSCAsY9WKpIBAgQMO5ugAABAkEB4x4sVSQCBAgYdzdAgACBoIBxD5YqEgECBIy7GyBAgEBQwLgHSxWJAAECxt0NECBAIChg3IOlikSAAAHj7gYIECAQFDDuwVJFIkCAgHF3AwQIEAgKGPdgqSIRIEDAuLsBAgQIBAWMe7BUkQgQIGDc3QABAgSCAsY9WKpIBAgQMO5ugAABAkEB4x4sVSQCBAgYdzdAgACBoIBxD5YqEgECBIy7GyBAgEBQwLgHSxWJAAECxt0NECBAIChg3IOlikSAAAHj7gYIECAQFDDuwVJFIkCAgHF3AwQIEAgKGPdgqSIRIEDAuLsBAgQIBAWMe7BUkQgQIGDc3QABAgSCAsY9WKpIBAgQMO5ugAABAkEB4x4sVSQCBAgYdzdAgACBoIBxD5YqEgECBIy7GyBAgEBQwLgHSxWJAAECxt0NECBAIChg3IOlikSAAAHj7gYIECAQFDDuwVJFIkCAgHF3AwQIEAgKGPdgqSIRIEDAuLsBAgQIBAWMe7BUkQgQIGDc3QABAgSCAsY9WKpIBAgQMO5ugAABAkEB4x4sVSQCBA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QICAcXcDBAgQCAoY92CpIhEgQMC4uwECBAgEBYx7sFSRCBAgYNzdAAECBIICxj1YqkgECBAw7m6AAAECQQHjHixVJAIECBh3N0CAAIGggHEPlioSAQIEjLsbIECAQFDAuAdLFYkAAQLG3Q0QIEAgKGDcg6WKRIAAAePuBggQIBAUMO7BUkUiQICAcXcDBAgQCAoY92CpIhEgQMC4uwECBAgEBYx7sFSRCBAgYNzdAAECBIICxj1YqkgECBAw7m6AAAECQQHjHixVJAIECBh3N0CAAIGggHEPlioSAQIEjLsbIECAQFDAuAdLFYkAAQLG3Q0QIEAgKGDcg6WKRIAAAePuBggQIBAUMO7BUkUiQICAcXcDBAgQCAoY92CpIhEgQMC4uwECBAgEBYx7sFSRCBAgYNzdAAECBIICxj1YqkgECBAw7m6AAAECQQHjHixVJAIECBh3N0CAAIGggHEPlioSAQIEjLsbIECAQFDAuAdLFYkAAQK/AcFlN5EVWmXrAAAAAElFTkSuQmCC';
    
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $url)){
                $imgtype = $url[2];
                list($s1, $s2) = explode(' ', microtime());
                $date = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
                // 移动到框架应用根目录/public/upload 目录下
    //            $new_file = "upload/active/img/".date('Ymd',time())."/";
    //            if(!file_exists($new_file))
    //            {
    //                //检查是否有该文件夹,如果没有就创建,并给予最高权限
    //                mkdir($new_file, 0700);
    //            }
                $new_file = $new_file.time().".{$imgtype}";
                $new_file = 'public/upload/'.$date.".{$imgtype}";
                if (file_put_contents($new_file, base64_decode(str_replace($url[1], '', $base64_image_content)))){
                    echo '新文件保存成功:', $new_file;
                }else{
                    echo '新文件保存失败';
                }
    
    
    
    
    
    
    
    
    
    
    
    1.解析base64数据成图片
    
    $url = explode(',' $base64);
    
    $a = file_put_contents('./test.png', base64_decode($url[1]));//返回的是字节数
    
    print_r($a);
    
    2.图片转成base64数据
    
    $str = file_get_contents('./test.png');
    
    echo base64_encode($str);
    
    function imgToBase64($img_url) {
        $imageInfo = getimagesize($img_url);
        $bae64 = chunk_split(base64_encode(file_get_contents($img_url)));
        $encode = 'data:'.$imageInfo['mime'] .';base64,' . $bae64;
        return $encode;
    }
    
    

    ............................................................................................................................................................................................................................................

    相关文章

      网友评论

          本文标题:php-base64 图片互转

          本文链接:https://www.haomeiwen.com/subject/vnpdphtx.html