Skip to content

Instantly share code, notes, and snippets.

@mssyogi
Forked from nbriz/convert.php
Created December 20, 2017 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mssyogi/e149e5bbeae26d2185ab51505f1e7683 to your computer and use it in GitHub Desktop.
Save mssyogi/e149e5bbeae26d2185ab51505f1e7683 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment