PHP
Step 1: Install ClearImage SDK
Download and install the ClearImage SDK.
Step 2: Configure ClearImage
PHP code uses ClearImage.dll through COM API. ClearImage DLL is registered as a COM object during installation. To register manually, run the following command as Administrator.
Add this section to php.iniif it has not been set yet.
Error 'Class ‘COM’ not found' indicates that the above is not configured.
ClearImage DLL API reports errors as an Exception object.
<?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 in the ClearImage COM API 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 reader->AutoDetect1D=true; automatically finds the barcode type, but it is slower and is not recommended for production.
<?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.
<?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 $Bc->Data property as an integer array. To access all elements:
Read Postal Barcodes from a page
This example reads Postal Barcodes from an image file page.
<?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.
<?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.
<?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;
}
}
?>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
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 fileOut file.
<?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;
}
}
?>