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 and it cannot interact with document scanners attached to client computers directly. With the continued growth of digitization of documents, scanning capabilities are 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 scanners attached to the client machine.

Download Sample Code

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.

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. It 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.

As far as Dynamic Web TWAIN is concerned, it doesn’t make much of a difference whether you are using C#, VB.NET, ASP, JSP or PHP on the server side. And, it can be deployed on web servers like IIS, Apache or Tomcat.

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

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

Let us know if you have any questions or comments on writing a PHP application to interact with scanners.