首页 文章

如何将图像转换为base64编码?

提问于
浏览
238

你能指导我如何将图像从URL转换为base64编码?

9 回答

  • 6

    我认为它应该是:

    $path = 'myfolder/myimage.png';
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    
  • 10

    简单:

    $imagedata = file_get_contents("/path/to/image.jpg");
                 // alternatively specify an URL, if PHP settings allow
    $base64 = base64_encode($imagedata);
    

    请记住,这会将数据扩大33%,而且对于大小超过 memory_limit 的文件,您会遇到问题 .

  • 10

    也可以这种方式用base64编码格式表示图像...找到PHP函数 file_get_content ,然后使用函数 base64_encode

    并获得结果以准备str为 data:" . file_mime_type . " base64_encoded string . 在img src属性中使用它 . 看下面的代码可以帮到你 .

    // A few settings
    $img_file = 'raju.jpg';
    
    // Read image path, convert to base64 encoding
    $imgData = base64_encode(file_get_contents($img_file));
    
    // Format the image SRC:  data:{mime};base64,{data};
    $src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;
    
    // Echo out a sample image
    echo '<img src="'.$src.'">';
    
  • 3
    <img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("IMAGE URL HERE")) ?>">
    

    我试图使用这个资源,但一直出错,我发现上面的代码完美无缺 .

    刚刚用图片的网址替换了图片网址 - http://www.website.com/image.jpg

  • 107

    如果您(无论出于何种原因)无法使用 curlfile_get_contents ,您可以解决以下问题:

    $img = imagecreatefrompng('...');
    ob_start();
    imagepng($img);
    $bin = ob_get_clean();
    $b64 = base64_encode($bin);
    
  • 0

    非常简单并且常用:

    function getDataURI($imagePath) {
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $type = $finfo->file($imagePath);
        return 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath));
    }
    
    //Use the above function like below:
    echo '<img src="'.getDataURI('./images/my-file.svg').'" alt="">';
    echo '<img src="'.getDataURI('./images/my-file.png').'" alt="">';
    

    注意:文件的Mime-Type将自动添加(从此PHP documentation获取帮助) .

  • 1

    以下是使用cURL调用的示例..这比 file_get_contents() 函数更好 . 当然,请使用 base64_encode()

    $url = "http://example.com";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    ?>
    
    <img src="data:image/png;base64,<?php echo base64_encode($output);?>">
    
  • 462

    这是上传编码并将其保存到MySQL的代码

    if (!isset($_GET["getfile"])) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
    
            $bin_string = file_get_contents($_FILES["file"]["name"]);
            $hex_string = base64_encode($bin_string);
            $mysqli = mysqli_init();
    
            if (!$mysqli->real_connect('localhost', 'root', '', 'arihant')) {
                die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
            }
    
            $mysqli->query("INSERT INTO upload(image) VALUES ('" . $hex_string . "')");
        }
    }
    

    为了显示图像使用此

    echo "<img src='data:image/jpeg;base64, $image' width=300>";
    
  • 30

    您也可以通过curl执行此操作,只需要一个图像文件的路径并将其传递给下面给出的函数 .

    public static function getImageDataFromUrl($url)
    {
        $urlParts = pathinfo($url);
        $extension = $urlParts['extension'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $response = curl_exec($ch);
        curl_close($ch);
        $base64 = 'data:image/' . $extension . ';base64,' . base64_encode($response);
        return $base64;
    
    }
    

相关问题