How to convert image file to base64 in php?

How can I convert an image from a URL to Base64 encoding?

How to convert image file to base64 in php?

asked Oct 19, 2010 at 10:44

3

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);

kenorb

144k76 gold badges655 silver badges710 bronze badges

answered Dec 7, 2012 at 7:29

Ronny ShererRonny Sherer

7,9991 gold badge21 silver badges9 bronze badges

5

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.

How to convert image file to base64 in php?

answered Oct 19, 2010 at 10:45

How to convert image file to base64 in php?

PekkaPekka

434k137 gold badges965 silver badges1079 bronze badges

1

Also use this way to represent an image in Base64-encoded format...

Find the PHP function file_get_content and next use the function base64_encode.

And get the result to prepare str as data:" . file_mime_type . " base64_encoded string. Use it in the img src attribute. The following code ma be helpful:

// 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.'">';

How to convert image file to base64 in php?

answered Sep 11, 2013 at 11:20

Raju RamRaju Ram

9039 silver badges14 bronze badges

1

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);

answered May 22, 2016 at 5:12

yckartyckart

30.9k9 gold badges117 silver badges127 bronze badges

0

<img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("IMAGE URL HERE")) ?>">

I was trying to use this resource, but I kept getting an error. I found the code above worked perfectly.

I just replaced "IMAGE URL HERE" with the URL of your image - http://www.website.com/image.jpg

How to convert image file to base64 in php?

answered Jul 29, 2014 at 9:22

GoldenGonazGoldenGonaz

1,0362 gold badges16 silver badges38 bronze badges

1

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).

How to convert image file to base64 in php?

answered Dec 4, 2015 at 11:44

How to convert image file to base64 in php?

Reza MamunReza Mamun

5,7331 gold badge40 silver badges40 bronze badges

Here is the code for uploading to encode and save it to a MySQL database:

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

How to convert image file to base64 in php?

answered Jul 15, 2015 at 7:00

How to convert image file to base64 in php?

VivekVivek

1,40818 silver badges27 bronze badges

1

You can also do this via cURL. You just need a path to an image file and pass it to the function given below...

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;
}

How to convert image file to base64 in php?

answered Jan 6, 2016 at 6:49

How to convert image file to base64 in php?

Tayyab HussainTayyab Hussain

1,4981 gold badge17 silver badges20 bronze badges

Here is an example using a cURL call... This is better than the file_get_contents() function. Of course, use base64_encode().

<?php
    $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);?>">

How to convert image file to base64 in php?

answered Jul 28, 2015 at 21:59

JeanAlesiJeanAlesi

4183 silver badges15 bronze badges

1

How do I convert an image to base64?

To decode an image from the Base64 Encoded string, perform the following steps..
Open the Base64 to Image Converter..
Enter a Base64 Encoded string..
Select the Image type. ... .
After selecting the Image type, click on the "Generate Image" button..
The tool decodes the Base64 Encoded string and displays the image..

Can we convert image to base64?

The Image encoding tool supports loading the Image File to transform to Base64. Click on the Upload Image button and select File. Image to Base64 Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.

How do you base64 encode in PHP?

Syntax. base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.

How can I download base64 image in PHP?

“download base64 image php” Code Answer.
$path = 'myfolder/myimage.png';.
$type = pathinfo($path, PATHINFO_EXTENSION);.
$data = file_get_contents($path);.
$base64 = 'data:image/' . $ type . '; base64,' . base64_encode($data);.