How to convert an image to base64 encoding?

前端 未结 9 1223
刺人心
刺人心 2020-11-22 06:01

Can you please guide me how can I convert an image from a URL to base64 encoding?

相关标签:
9条回答
  • 2020-11-22 06:27

    Very simple and to be commonly used:

    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="">';
    

    Note: The Mime-Type of the file will be added automatically (taking help from this PHP documentation).

    0 讨论(0)
  • 2020-11-22 06:28

    Use also this way to represent image in base64 encode format... find PHP function file_get_content and next to use function base64_encode

    and get result to prepare str as data:" . file_mime_type . " base64_encoded string. Use it in img src attribute. see following code can I help for you.

    // 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.'">';
    
    0 讨论(0)
  • 2020-11-22 06:29

    Just in case you are (for whatever reason) unable to use curl nor file_get_contents, you can work around:

    $img = imagecreatefrompng('...');
    ob_start();
    imagepng($img);
    $bin = ob_get_clean();
    $b64 = base64_encode($bin);
    
    0 讨论(0)
  • 2020-11-22 06:34

    Here is the code for upload to encode and save it to the 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 . "')");
        }
    }
    

    For showing the image use this

    echo "<img src='data:image/jpeg;base64, $image' width=300>";
    
    0 讨论(0)
  • 2020-11-22 06:35

    I think that it should be:

    $path = 'myfolder/myimage.png';
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    
    0 讨论(0)
  • 2020-11-22 06:41

    Easy:

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

    bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit.

    0 讨论(0)
提交回复
热议问题