PHP對擴展的編寫要求很是嚴格。若是沒有按照官方文檔,使用對應的PHP版本,PHP源碼版本,以及Visual Studio版本,即便可以在Windows上成功編譯DLL,也會由於版本不匹配報錯,從而沒法運行。以前只寫瞭如何編寫擴展,這裏會分享下如何使用Nginx+PHP+DBR(Dynamsoft Barcode Reader)來搭建一個簡單的Web條形碼應用。php
參考原文:How to Create a Web Barcode Reader App with PHP and Nginxcss
做者:Xiao Linghtml
翻譯:yushulxjquery
使用Dynamsoft Barcode SDK來快速建立一個PHP擴展php_dbr.dll。具體步驟能夠參考:使用C/C++編寫PHP Extension。web
把生成的php_dbr.dll拷貝到%PHP%\ext中。
app
把DynamsoftBarcodeReaderx86.dll拷貝到%PHP%根目錄下。
post
打開%php%\php.ini文件添加擴展描述:
extension=php_dbr.dll
若是有文件上傳的需求,能夠修改一下最大文件上傳的尺寸:
upload_max_filesize=20M
爲了讓Nginx支持PHP,打開%nginx%\conf\nginx.conf添加:
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME <Your Web App Folder>/$fastcgi_script_name; include fastcgi_params; }
若是上傳文件尺寸太大,會出現下面的錯誤:
nginx 413 Request Entity Too Large
這個時候須要修改Nginx配置:
client_max_body_size 50M;
建立index.php:
<!DOCTYPE html> <html> <head> <title>Dynamsoft PHP Barcode Reader</title> <script src="jquery-1.11.3.min.js"></script> <script src="tiff.min.js"></script> </head> <body> <H1>Dynamsoft PHP Barcode Reader</H1> <form action="dbr.php" method="post" enctype="multipart/form-data"> Select barcode image: <input type="file" name="readBarcode" id="readBarcode" accept="image/*"><br> <input type="submit" value="Read Barcode" name="submit"> </form> <div id="tiff"></div> <div id='image'></div> <script> function reset() { $("#image").empty(); $("#tiff").empty(); } var input = document.querySelector('input[type=file]'); input.onchange = function() { reset(); var file = input.files[0]; var fileReader = new FileReader(); // get file extension var extension = file.name.split('.').pop().toLowerCase(); var isTiff = false; if (extension == "tif" || extension == "tiff") { isTiff = true; } fileReader.onload = function(e) { if (isTiff) { console.debug("Parsing TIFF image..."); //initialize with 100MB for large files Tiff.initialize({ TOTAL_MEMORY: 100000000 }); var tiff = new Tiff({ buffer: e.target.result }); var tiffCanvas = tiff.toCanvas(); $(tiffCanvas).css({ "max-width": "800px", "width": "100%", "height": "auto", "display": "block", "padding-top": "10px" }).addClass("TiffPreview"); $("#tiff").append(tiffCanvas); } else { var dataURL = e.target.result, img = new Image(); img.src = dataURL; $("#image").append(img); } } if (isTiff) { fileReader.readAsArrayBuffer(file); } else fileReader.readAsDataURL(file); } </script> </body> </html>
爲了支持tiff文件的加載顯示,咱們能夠使用tiff js library.。
建立dbr.php用於接收上傳文件,而且調用PHP條形碼擴展接口來解碼:
<?php // create absolute file path function file_build_path(...$segments) { return join(DIRECTORY_SEPARATOR, $segments); } $file = basename($_FILES["readBarcode"]["name"]); echo "<p>$file</p>"; if ($file != NULL && $file != "") { // get current working directory $root = getcwd(); // tmp dir for receiving uploaded barcode images $tmpDir = "uploads/"; if (!file_exists($tmpDir)) { mkdir($tmpDir); } $target_file = $tmpDir . basename($_FILES["readBarcode"]["name"]); $isSuccessful = true; $fileType = pathinfo($target_file,PATHINFO_EXTENSION); if (!$isSuccessful) { echo "Fail to read barcode"; } else { if (move_uploaded_file($_FILES["readBarcode"]["tmp_name"], $target_file)) { // dynamsoft barcode reader $path = file_build_path($root, $target_file); /* * Description: * array DecodeBarcodeFile( string $filename , bool $isNativeOutput [, bool $isLogOn ] ) * * Return Values: * If barcode detected, $array[0] is an array. */ $resultArray = DecodeBarcodeFile($path, false); if (is_array($resultArray[0])) { $resultCount = count($resultArray); echo "Total count: $resultCount\n"; for($i = 0; $i < $resultCount ; $i++) { $result = $resultArray[$i]; echo "<p>Barcode format: $result[0], value: $result[1]</p>"; } } else { echo "<p>$resultArray[0]</p>"; } // delete the uploaded barcode image unlink($path); } else { echo "Fail to read barcode."; } } } ?>
運行php-cgi:
%php%\php-cgi.exe -b 127.0.0.1:9000 -c %php%\php.ini
運行Nginx:
%nginx%\nginx.exe
打開localhost:8080/index.php
:
作測試: