Skip to content

Instantly share code, notes, and snippets.

@nbriz
Created April 8, 2016 15:54
Show Gist options
  • Save nbriz/05f62c2e44994dad4b72c03ee38bba9e to your computer and use it in GitHub Desktop.
Save nbriz/05f62c2e44994dad4b72c03ee38bba9e to your computer and use it in GitHub Desktop.
convert image into base64 encoded string
<!DOCTYPE html>
<html>
<head>
<title> img to data</title>
<meta charset="utf-8">
<style>
textarea{ width: 50%; height: 200px; }
</style>
</head>
<body>
<?php
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$data = base64_encode(file_get_contents( $_FILES["fileToUpload"]["tmp_name"] ));
echo "copy + paste the data below, use it as a string in ur JavaScript Code<br><br>";
echo "<textarea id='data' style=''>data:".$check["mime"].";base64,".$data."</textarea>";
} else {
echo "File is not an image.";
}
}
?>
<script>document.getElementById('data').select();</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> img to data</title>
<meta charset="utf-8">
</head>
<body>
<form action="convert.php" method="post" enctype="multipart/form-data">
Select image to convert into base64 data:
<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="Convert Image to Data" name="submit">
</form>
</body>
</html>
@mbpating
Copy link

mbpating commented Mar 5, 2021

Freaking amazing. You save me a lot of work here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment