Organizational Research By

Surprising Reserch Topic

how to convert image to base64 encoding


how to convert image to base64 encoding  using -'php,image-processing'

asked Sep 9, 2015 by rajesh
0 votes
14 views



Related Hot Questions

6 Answers

0 votes

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);
answered Sep 9, 2015 by rajesh
0 votes

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 '';
answered Sep 9, 2015 by rajesh
0 votes

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.

answered Sep 9, 2015 by rajesh
0 votes
">

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

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

answered Sep 9, 2015 by rajesh
0 votes

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"] . "
"; }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 "";
answered Sep 9, 2015 by rajesh
0 votes

Here is an example using a cURL call.. This is better than the file_get_contents() function. Of course, use 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);
?>

  
answered Sep 9, 2015 by rajesh

...