Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active January 4, 2024 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/11dd9647c63865152696e481dc3bf124 to your computer and use it in GitHub Desktop.
Save code-boxx/11dd9647c63865152696e481dc3bf124 to your computer and use it in GitHub Desktop.
PHP Image To Text OCR

PHP IMAGE TO TEXT USING OCR

https://code-boxx.com/php-image-to-text-ocr/

TEST IMAGE

test

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) SETTINGS - CHANGE TO YOUR OWN!
$tes = "C:/Program Files/Tesseract-OCR/tesseract.exe"; // path to tesseract
$img = __DIR__ . DIRECTORY_SEPARATOR . "test.png"; // image to read
$lang = "eng"; // language
// (B) RUN TESSERACT IN TERMINAL
// https://github.com/tesseract-ocr/tesseract
$cmd = "\"$tes\" $img - -l $lang";
$result = shell_exec($cmd);
// (C) OPTIONAL - STRIP LINE BREAKS
$result = str_replace("\n", " ", $result);
echo $result;
<!DOCTYPE html>
<html>
<head>
<title>Tesseract JS</title>
<meta charset="utf-8">
</head>
<body>
<!-- (A) FILE SELECTOR -->
<input type="file" id="select" accept="image/png, image/gif, image/webp, image/jpeg">
<!-- (B) LOAD TESSERACT -->
<!-- https://cdnjs.com/libraries/tesseract.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.0.6/tesseract.min.js"></script>
<!-- (C) INIT -->
<script>
window.addEventListener("load", async () => {
// (C1) GET HTML FILE SELECTOR
const hSel = document.getElementById("select");
// (C2) CREATE ENGLISH WORKER
const worker = await Tesseract.createWorker();
await worker.loadLanguage("eng");
await worker.initialize("eng");
// (C3) ON FILE SELECT
hSel.onchange = async () => {
// (C3-1) IMAGE TO TEXT
const res = await worker.recognize(hSel.files[0]);
// (C3-2) UPLOAD TO SERVER
let data = new FormData();
data.append("text", res.data.text);
fetch("2b-save.php", { method:"post", body:data })
.then(res => res.text())
.then(txt => console.log(txt))
.catch(err => console.error(err));
};
});
</script>
</body>
</html>
<?php
print_r($_POST);
file_put_contents("demo.txt", $_POST["text"]);
https://cloud.google.com/vision
https://cloud.google.com/vision/docs/ocr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment