How to Scan Documents in PHP Applications

Sep 05, 2021
How to Scan Documents in PHP Applications

How to Interact with TWAIN Scanners in a PHP Application

PHP is a popular server-side language, but it cannot directly interact with document scanners attached to client computers. However, with the continued growth of digitization, scanning capabilities have become increasingly important in PHP environments.

To implement document scanning in your PHP web applications, you can use an HTML5/JavaScript scanning SDK like Dynamic Web TWAIN to interact with TWAIN, SANE, eSCL, WIA, and other scanners attached to the client machine.

Image Scanning and Uploading in PHP Application

Here are the code snippets for using Dynamic Web TWAIN to scan documents and upload the images to Web server in PHP.

Download Sample Code

JavaScript code – scan and upload images

function AcquireImage(){
  var DWObject = Dynamsoft.DWT.GetWebTwain('dwtcontrolContainer');
  DWObject.IfShowUI = false;
  DWObject.SelectSource();
  DWObject.OpenSource();
  DWObject.AcquireImage();
}

function btnUpload_onclick(){
  DWObject.HTTPUploadThroughPostEx(
  strHTTPServer,
  DWObject.CurrentImageIndexInBuffer,
  strActionPage,
  uploadfilename,
  trImageType );
}

PHP code – action page for uploading the images to web server

<?php   
  $fileTempName = $_FILES['RemoteFile']['tmp_name'];    
  $fileSize = $_FILES['RemoteFile']['size'];
  $fileName = "UploadedImages\\".$_FILES['RemoteFile']['name'];
  
  if (file_exists($fileName)) 
    $fWriteHandle = fopen($fileName, 'w');
  else
    $fWriteHandle = fopen($fileName, 'w');
  $fReadHandle = fopen($fileTempName, 'rb');
  $fileContent = fread($fReadHandle, $fileSize);
  fwrite($fWriteHandle, $fileContent);
  fclose($fWriteHandle);
  echo "DWTBarcodeUploadSuccess:".$_FILES['RemoteFile']['name'];
?>

Regarding Dynamic Web TWAIN

Dynamic Web TWAIN is a browser-based document scanning component that can interact with TWAIN & SANE scanners from all popular web browsers. It is highly optimized for 32-bit/64-bit IE, Edge, Firefox, Chrome, Safari, and Opera on Windows, Linux, and macOS.

Regarding server-side technology, Dynamic Web TWAIN works with all programming languages, including C#, VB.NET, ASP, JSP, and PHP. It can be deployed on web servers like IIS, Apache, or Tomcat.

The server-side code can be written in your preferred language. The “Action Page” refers to the page that receives and processes the images uploaded by Dynamic Web TWAIN. These images can be saved into a file system, database, SharePoint, or other formats.

To embed a web scanning module into your PHP application, you can download and install the 30-day free trial of Dynamic Web TWAIN.

If you have any questions or comments about writing a PHP application to interact with scanners, please let us know.