I need to convert the image URL to base64_encode data using PHP. I am explaining my code below.
<?php
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
?>
Here I am getting the blank output. Please help me to resolve this issue.
your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true
add this line and check for error message
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
Here's code to convert image to base64 format
$path='http:\/\/abcd.com\/pub\/media\/catalog\/product\/\/m\/i\/mi-l32m5-ai.jpeg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo $base64;
Thanks
Please use the unescaped url in $path variable,
<?php
$path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
Try this code. It will help you.
According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .
$path = 'your-file-path';
$file = 'your-file';
$image_with_path = $path.'/'.$file;
if(file_exists($image_with_path)){
$base64 = base64_encode($image_with_path );
}
Related
I want to get the image file from my public folder which I stored all of my images, then convert to base64. I will use the base64 image to view it on PDF using pdfmake in dataTables, because my dataTable cell have an Image called avatar, and as I've searched it needs to convert image to base64 to able to view in PDF.
Now I use file_get_contents to retrieve my image, but my page is too much load I guess it's around 5mins, and after throws and error Maximum execution time of 60 seconds exceeded.
{{ file_get_contents( asset('files/user_avatar.png') ) }}
There is difference between svg and (jpg-png-jpeg) when you encoding to base64. You can use basically png extension when you working with png image. But you cant use svg basically. You need svg+xml when you working with svg.
function img_enc_base64 ($filepath){ // img_enc_base64() is manual function you can change the name what you want.
if (file_exists($filepath)){
$filetype = pathinfo($filepath, PATHINFO_EXTENSION);
if ($filetype==='svg'){
$filetype .= '+xml';
}
$get_img = file_get_contents($filepath);
return 'data:image/' . $filetype . ';base64,' . base64_encode($get_img );
}
}
so now
echo img_enc_base64('file_path'); // is your base64 code of image
<img src="<?php echo img_enc_base64('file_path');?>" alt=""> // is your image
file_path example: pics/my_book.png
I'm not friendly with Laraval but I have a tested answer written in PHP
<?php
$path = "files/user_avatar.png";
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
# print to make sure that it is working or not
echo $base64."<br>";
# Or, show it as a clean image
echo '<img scr="'.$base64.'" height="150" width="150">';
?>
Edited:
According to Jin,
Above snippet not working just because of file_get_contents function.
Solution:
Use curl_get_contents() instead of file_get_contents
curl_get_contents()
function curl_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
After replacing file_get_contents into curl_get_contents
$path = "files/user_avatar.png";
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = curl_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
# print to make sure that it is working or not
echo $base64."<br>";
# Or, show it as a clean image
echo '<img scr="'.$base64.'" height="150" width="150">';
Still taking too much time to load?
Try to check your file size or try
to check Server-Configuration
Hope it will helpful for you 😊
I think that it should be:
$path = 'files/user_avatar.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit.
Basically image/ document in jpf / png / pdf format needs to be converted into base64Binary and passed as json request.
Have seen a lot of code on the web but all are in Java or ASP. There is nothing I could find that would help with reading file and outputting base64binary value in PHP.
You could use the following code. Assuming you can use file_get_contents function,
$file_type = pathinfo($path, PATHINFO_EXTENSION);
$contents = file_get_contents($file_type);
$encoded = 'data:image/' . $file_type . ';base64,' . base64_encode($contents);
or you can use base64_encode function. Base64-encoded data takes about 33% more space than the original data.
$data = file_get_contents($contents);
$encoded = base64_encode($data);
echo $encoded;
When I echo the following:
$im_dec = base64_decode($row['image']);
I obtain the desired URL:
https://www.ft.com/__origami/service/image/v2/images/raw/http%3A%2F%2Fprod-upp-image-read.ft.com%2F1263ad72-2d9a-11e7-9555-23ef563ecf9a?source=next&fit=scale-down&compression=best&width=210
Then I use this URL to be the src of my img:
$newImage = $dom->createElement('img');
$newImage->setAttribute("src", $im_dec);
$newImage->setAttribute("class", "articleImage");
$newTitle->appendChild($newImage);
And when I check the src attribute in my html document, I get a modified url where
& is replaced by & for example and many more weird stuff..
Some characters were modified and I don't know how to avoid it. I tried many things but I thought base64 encoding would work...
Help please!
You can convert an image to base64 encoding with the following example:
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
I'm stuck with a little problem and can't find a solution. I'm outputting my images to the website as base64 data URI. If someone opens the image directly in the browser the url to the image would be the data URI which works find but the name that shows in the browser tab is something random and i want to change that. I know that i could add /new+name++that+i+want at the end of the data URI to change the title that shows in the browser but sometimes there is already a name/title at the end of the data URI and in this case it would just create an error if I add something to the end. Is there any save way to set a title/name rather than adding something at the end and hoping it doesn't create an error?
Thanks for reading and have a nice day.
My Code:
$img = "uploads/images/smile.png";
$filetype = mime_content_type($img);
$imgdata = base64_encode(file_get_contents($img));
$src = "data:" . $filetype . ";base64," . $imgdata;
Use pathinfo for file type $filetype = pathinfo($path, PATHINFO_EXTENSION);
$img = "uploads/images/smile.png";
$filetype = pathinfo($img, PATHINFO_EXTENSION);
$imgdata = base64_encode(file_get_contents($img));
$src = "data:" . $filetype . ";base64," . $imgdata;
I'm trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..
I got this code:
define('UPLOAD_DIR', '../image/');
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$uniqueNumber = uniqid();
$namefile = $uniqueNumber.'.png';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
$namefile = $uniqueNumber.'.pdf';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!
EDIT:
I'm trying this code and getting the same issue.
$data = base64_decode ($img);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$data);
//close output file
fclose ($pdf);
echo 'Done';
Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.
EDIT 2:
FOUND A SOLUTION.
http://www.fpdf.org/en/script/script45.php
I followed these steps and i can get that, thank you all!
Check out DOMPDF: https://github.com/dompdf/dompdf
You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.
<?php
require_once("dompdf_config.inc.php");
$img = $_POST['image'];
$html =
'<html><body>'.
'<img src="'.$img.
'"></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>