---
title: PHP
slug: cisdk-php
docTags: 
createdAt: 2023-03-12T03:13:21.000Z
---

## Step 1:   Install ClearImage SDK

Download and install the [ClearImage SDK.](https://barcode-reader.inliteresearch.com/get-cisdk)

## Step 2:   Configure ClearImage

PHP code uses <font color="#eb144c">`ClearImage.dll`</font> through **COM API**.  ClearImage DLL is registered as a COM object during installation. To register manually, run the following command **as Administrator**.

:::BlockQuote
"C:\Program Files (x86)\Inlite\ClearImageSDK\register.bat"
:::

Add this section to <font color="#eb144c">`php.ini`</font>if it has not been set yet.

:::BlockQuote
&#x20;<font color="#eb144c"> [PHP_COM_DOTNET]
  extension=php_com_dotnet.dll
  fastcgi.impersonate = 0</font>
:::

Error  `'Class ‘COM’ not found'`**&#x20;&#x20;**&#x20;indicates that the above is not configured.

ClearImage DLL API reports errors as an <font color="#eb144c">`Exception`</font> object.&#x20;

```php
<?php
 try {
   		// Create ClearImage COM Server
    $Server = new COM("ClearImage.ClearImage");
	//  Your PHP code
  }
  catch(Exception $e) {
    print "Exceptiom in line " . $e->getLine() . "\n" . $e->getMessage() . "\n";
  }
 ?>
```

## ClearImage project development

### Extend functionality

While developing your application, you can combine code examples on this page with methods described in  [ClearImage API documentation](http://www.inliteresearch.com/help/ci) in the <font color="#eb144c">`ClearImage COM API`</font> section.

## Barcode Reading

### Read 1D  barcodes from a page

This example reads code 39 and code 128 barcodes. Set barcode types used in your application. Setting <font color="#eb144c">`reader->AutoDetect1D=true;`</font> automatically finds the barcode type, but it is slower and is not recommended for production.  


```php
<?php
function ReadBarcode1D_page ($Server, $fileName, $page)
{
	$reader = $Server->CreateBarcodePro();
	$cibfCode39 = 2; $cibfCode128 = 4;
	$reader->Type = $cibfCode39 + $cibfCode128;
	$reader->Image->Open($fileName, $page); 
	$BCcount = $reader->Find(0);
	for ($i=1;$i<=$BCcount;$i++) {
		$Bc = $reader->BarCodes($i);
		print "Text: " . $Bc->Text . "\n";
		}
}
?>
```

### Read 2D Barcode from a page

This example reads PDF417, DataMatrix, and QR code.  &#x20;

```php
<?php
function ReadBarcode2D_page ($Server, $fileName, $page)
{
	$reader = $Server->CreateQR();
	// $reader = $Server->CreatePdf417();
	// $reader = $Server->CreateDataMatrix();
	$reader->Image->Open($fileName, $page); 
	$BCcount = $reader->Find(0);
	for ($i=1;$i<=$BCcount;$i++) {
		$Bc = $reader->BarCodes($i);
		print "Text: " . $Bc->Text . "\n";
		}
}
?>
```

2D barcodes might contain binary data in  <font color="#eb144c">`$Bc->Data`</font> property as an integer array.  To access all elements:

:::BlockQuote
foreach ($Bc->Data as $val) \{print "   " .  $val;}
:::

### Read Postal Barcodes from a page

This example reads Postal Barcodes from an image file page.  &#x20;

```php
<?php
function ReadBarcodePostal_page ($Server, $fileName, $page)
{
	$reader = $Server->CreateBarcodePro();
    $cibf4State = 0x2000000;
	$reader->Type = $cibf4State;
	$reader->Image->Open($fileName, $page); 
	$BCcount = $reader->Find(0);
	for ($i=1;$i<=$BCcount;$i++) {
		$Bc = $reader->BarCodes($i);
        $type1 =$Bc->Type;
        if ($type1 == 33) echo " US Intelligent Mail "; // cibUspsIntelligentMail = 33
        if ($type1 == 34) echo "UK Royal Mail "; // cibBpoPostcode = 34
        if ($type1 == 35) echo "Australian Mail "; // cibAustralianPost = 35 
        if ($type1 == 37) echo "Singapore Mail "; // cibSingaporePost = 37
		print "  Text: " . $Bc->Text . "\n";
		}
}
?>
```

### Read Driver's License barcode

This example reads the Driver's License barcode from an image file page.  &#x20;

```php
<?php
function ReadBarcodeDriverLic_page ($Server, $fileName, $page)
{
	$reader = $Server->CreatePdf417();
	$reader->Image->Open($fileName, $page); 
	$BCcount = $reader->Find(0);
	for ($i=1;$i<=$BCcount;$i++) {
		$Bc = $reader->BarCodes($i);
		print "Text: " . $Bc->Text . "\n";
		if ($Bc->Info("last") != "") 
		{
			 print "last:    " . $Bc->Info("last") . "\n";
			 print "first:   " . $Bc->Info("first") . "\n";
			 print "middle:  " . $Bc->Info("middle") . "\n";
			 print "suffix:  " . $Bc->Info("suffix") . "\n";
			 print "dob:     " . $Bc->Info("dob") . "\n";
			 print "eyes:    " . $Bc->Info("eyes") . "\n";
			 print "hair:    " . $Bc->Info("hair") . "\n";
			 print "sex:     " . $Bc->Info("sex") . "\n";
			 print "height:  " . $Bc->Info("height") . "\n";
			 print "weight:  " . $Bc->Info("weight") . "\n";
			 print "street:  " . $Bc->Info("street") . "\n";
			 print "city:    " . $Bc->Info("city") . "\n";
			 print "state:   " . $Bc->Info("state") . "\n";
			 print "postal:  " . $Bc->Info("postal") . "\n";
			 print "country: " . $Bc->Info("country") . "\n";
			 print "id:      " . $Bc->Info("id") . "\n";
			 print "issued:  " . $Bc->Info("issued") . "\n";
			 print "expires: " . $Bc->Info("expires") . "\n";
		}
	}
}
?>
```

### Read from  a multi-page image file

This example reads barcodes from a single-page file or all pages of a multi-page image file.  &#x20;

```php
<?php
function ReadBarcode1D_file ($Server, $fileName)
{
	$reader = $Server->CreateBarcodePro();
	$cibfCode39 = 2; $cibfCode128 = 4;
	$reader->Type = $cibfCode39 + $cibfCode128;
	$page=1;
	while(true)	{
	$reader->Image->Open($fileName, $page); 
	$BCcount = $reader->Find(0);
	// Process Results
	for ($i=1;$i<=$BCcount;$i++) {
		$Bc = $reader->BarCodes($i);
		$Bc = $reader->BarCodes($i);
		print "Page: " . $page .  "  Text: " . $Bc->Text . "\n";
		}
	$page++; 
	if ($page >  $reader->Image->PageCount)	
		break;
	}
}
?>
```

### Optimize barcode reading

This [page](docId\:SmK4-LGRtfEx0zacVU2LV) describes methods for improving the recognition rate and/or speed

## Image Processing

### Repair image page

This example uses some of the most popular image-processing functions. In a production application, the specific sequence of functions should be selected based on specific application needs. 

```php
<?php
function RepairPage ($repair)
{
	$repair->AutoDeskew();
	$repair->AutoRotate();
	$repair->AutoCrop(10, 10, 10, 10);   // Crop to 10 pixels on each side to bitonal
	$repair->AdvancedBinarize(0, 0 , 0);        // Convert to bitonal an image with complex background patterns
	$ciBexBorderDeskewCrop = 3;
	$ciBeaCleaner = 2;
	$repair->BorderExtract($ciBexBorderDeskewCrop, $ciBeaCleaner); // Deskew and crop based on black border
	$repair->RemovePunchHoles();
	$ciSmoothDarkenEdges = 1;
	$repair->SmoothCharacters($ciSmoothDarkenEdges);
	$repair->CleanNoise(3);               // Clean balck noise of 3 pixels
	// $ciCnxBlackNoise = 1; $ciCnxWhiteNoise = 2;
	// $repair->CleanNoiseExt($ciCnxBlackNoise + $ciCnxWhiteNoise, 3, 3, 10, 0); // Clean black and white noise
    $ciLineVertAndHorz = 3;
	$repair->ReconstructLines($ciLineVertAndHorz);
}
 
function Repair_page ($Server, $fileName, $page, $fileOut)
{
		$repair = $Server->CreateRepair();
		$repair->Image->Open($fileName, $page);
		RepairPage ($repair);
		$repair->Image->SaveAs($fileOut, 0);  // ciEXT
}
?>
```

### Repair all images in a multi-page file

This example repairs all images in a multi-page file and saves the result in the <font color="#eb144c">`fileOut`</font> file.

```php
<?php
function Repair_file ($Server, $fileName, $fileOut)
{
	$repair = $Server->CreateRepair();
	$page=1;
	while(true)
	{
		$repair->Image->Open($fileName, $page); 
		RepairPage ($repair);  // See above
		$repair->Image->Append($fileOut, 0);  // ciEXT
		$page++;
		if ($page >  $repair->Image->PageCount)
			break;
	}
}
?>
```







