Windows上PHP扩展的实现,部署及应用
2019獨角獸企業重金招聘Python工程師標準>>>
PHP對擴展的編寫要求非常嚴格。如果沒有按照官方文檔,使用對應的PHP版本,PHP源碼版本,以及Visual Studio版本,即使能夠在Windows上成功編譯DLL,也會因為版本不匹配報錯,從而無法運行。之前只寫了如何編寫擴展,這里會分享下如何使用Nginx+PHP+DBR(Dynamsoft Barcode Reader)來搭建一個簡單的Web條形碼應用。
參考原文:How to Create a Web Barcode Reader App with PHP and Nginx
作者:Xiao Ling
翻譯:yushulx
軟件下載
Nginx
PHP
Dynamsoft Barcode Reader SDK
步驟1:PHP Barcode擴展實現
使用Dynamsoft Barcode SDK來快速創建一個PHP擴展php_dbr.dll。具體步驟可以參考:使用C/C++編寫PHP Extension。
步驟2:PHP擴展部署和環境配置
把生成的php_dbr.dll拷貝到%PHP%\ext中。
把DynamsoftBarcodeReaderx86.dll拷貝到%PHP%根目錄下。
打開%php%\php.ini文件添加擴展描述:
extension=php_dbr.dll如果有文件上傳的需求,可以修改一下最大文件上傳的尺寸:
步驟3:如何在Nginx中配置PHP
為了讓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配置:
步驟4:Web條形碼應用
創建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?extensionvar?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?filesTiff.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);}elsefileReader.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?imageunlink($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:做測試:
源碼
https://github.com/dynamsoftsamples/php-barcode-reader
轉載于:https://my.oschina.net/yushulx/blog/547333
總結
以上是生活随笔為你收集整理的Windows上PHP扩展的实现,部署及应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Angular - - $cacheF
- 下一篇: PHP性能如何实现全面优化?