
You know we can add image into the web page without adding the link of the external image file. Let’s make it simple, the DATA URIs is the concept were we can just add the data of the image into the page and it works.
HTML Syntax to add image using DATA URI:
1 |
<img src="data:image/;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAIAAACyr5FlAAAACXBIWXMAABYlAAAWJQFJUiTwA"> |
CSS Syntax to add image using DATA URI:
1 2 3 4 5 6 |
div{ height: 400px; width: 400px; background: url(data:image/;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAIAAACyr5FlAAAACXBIWXMAABYlAAAWJQFJUiTwA) no-repeat; } |
PHP code to Convert Image to DATA URI
1 2 3 4 5 6 7 8 9 |
<?php $imagePath = "test.png"; // external image path $ImageType = pathinfo($imagePath, PATHINFO_EXTENSION); // get the image extension $data = file_get_contents($imagePath); $dataURI = 'data:image/' . $ImageType . ';base64,' . base64_encode($data); echo $dataURI; ?> |
index.php
along with the user interface.
Step 1: Create index.php
file and add following code:
Make sure to add required external CSS and JS files, such as jQuery and Bootstrap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Live Demo : Convert Image to DATA URI Using PHP and jQuery</title> <!-- Bootstrap CSS --> <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="container"> <h3> iTech Empires - Live Demo </h3> <div class="row jumbotron"> <div class="col-md-12"> <h2>Asynchronous Image to DATA URI Conversion using PHP and jQuery</h2> <p> </p> <form enctype="multipart/form-data"> <div class="form-group"> <label for="">Select Image File to Upload</label> <input type="file" name="file_to_upload" id="file_to_upload"> </div> </form> <div class="form-group"> <button class="btn btn-danger" onclick="ConvertImage()">Upload Image</button> </div> </div> </div> <div class="row"> <div class="col-md-6"> <h3>DATA URI</h3> <div id="dataURI"></div> </div> <div class="col-md-6"> <h3>IMAGE</h3> <div id="image"></div> </div> </div> </div> <script src="js/jquery.min.js"></script> <script src="js/script.js"></script> </body> </html> |
You can read more about uploading Asynchronous file from here: PHP and Ajax Asynchronous File Upload using jQuery
Step 2: Create script.js
file and add following function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// upload file on server to convert function ConvertImage() { if($("#file_to_upload").val() != "") { var file_data = $('#file_to_upload').prop('files')[0]; var form_data = new FormData(); form_data.append('file', file_data); $.ajax({ url: 'convertImage.php', dataType: 'text', cache: false, contentType: false, processData: false, data: form_data, type: 'post', success: function(data){ // get responce = DATA URI $("#dataURI").html(data); $("#image").html("<img src='"+data+"' />"); // clear file field $("#file_to_upload").val(""); } }); } else { alert("Please select file to upload!"); } } |
Step 3: Image to data uris conversion:
This is the file where we are performing the image conversion operation asynchronously.
create convertImage.php
file and use following code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php if ( 0 < $_FILES['file']['error'] ) { echo 'Error: ' . $_FILES['file']['error'] . '<br>'; } else { $imagePath = $_FILES['file']['tmp_name']; $ImageType = pathinfo($imagePath, PATHINFO_EXTENSION); $data = file_get_contents($imagePath); $dataURI = 'data:image/' . $ImageType . ';base64,' . base64_encode($data); echo $dataURI; } ?> |
Step 4: Create style.css
file and add following code:
1 2 3 4 5 6 7 |
#dataURI{ width: 100%; max-height: 400px; overflow-wrap: break-word; background-color: #EEE; overflow-y: scroll; } |
Thanks, if you get this tutorial helpful please provide your feedback using comments section.
Thank you for this informative read, I have sharedd it on Twitter.