GoogleOCRController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Google\Cloud\Vision\V1\ImageAnnotatorClient;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Str;
  7. class GoogleOCRController extends Controller
  8. {
  9. /**
  10. * open the view.
  11. *
  12. * @param
  13. * @return void
  14. */
  15. public function index()
  16. {
  17. return view('googleOcr');
  18. }
  19. /**
  20. * handle the image
  21. *
  22. * @param
  23. * @return void
  24. */
  25. public function submit(Request $request)
  26. {
  27. $file = $request->file('file');
  28. if ($file) {
  29. if (Str::lower($file->extension()) === 'pdf') {
  30. $baseName = $file->getBasename();
  31. $outputPath = "/ocr-images/$baseName";
  32. $this->convertPdfToImages($file, $outputPath);
  33. $text = $this->extractTextFromImages($outputPath);
  34. } else {
  35. $text = $this->detectText($file);
  36. }
  37. dd($text);
  38. }
  39. }
  40. private function detectText($file)
  41. {
  42. $imageAnnotatorClient = new ImageAnnotatorClient([
  43. 'credentials' => storage_path('/app/client_secret.json')
  44. ]);
  45. $text = '';
  46. $image = file_get_contents($file);
  47. $response = $imageAnnotatorClient->textDetection($image);
  48. $annotations = $response->getTextAnnotations();
  49. if ($annotations) {
  50. $text .= $annotations[0]->getDescription() . "\n";
  51. }
  52. return $text;
  53. }
  54. private function convertPdfToImages($pdfPath, $outputPath) {
  55. $imagick = new \Imagick();
  56. $imagick->setResolution(300, 300);
  57. $imagick->readImage($pdfPath);
  58. if (! Storage::disk()->exists($outputPath)) {
  59. Storage::disk()->makeDirectory($outputPath);
  60. }
  61. for ($i = 0; $i < $imagick->getNumberImages(); $i++) {
  62. $imagick->setIteratorIndex($i);
  63. $imagick->setImageFormat('png');
  64. $imagick->writeImage(Storage::path($outputPath) . "/page_" . $i . ".png");
  65. }
  66. }
  67. private function extractTextFromImages($imagesPath) {
  68. $imageAnnotatorClient = new ImageAnnotatorClient([
  69. 'credentials' => storage_path('/app/client_secret.json')
  70. ]);
  71. $text = '';
  72. $files = glob(Storage::path($imagesPath) . '/*.png');
  73. foreach ($files as $file) {
  74. $image = file_get_contents($file);
  75. $response = $imageAnnotatorClient->textDetection($image);
  76. $annotations = $response->getTextAnnotations();
  77. if ($annotations) {
  78. $text .= $annotations[0]->getDescription() . "\n";
  79. }
  80. }
  81. Storage::deleteDirectory($imagesPath);
  82. $imageAnnotatorClient->close();
  83. return $text;
  84. }
  85. }