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

如果有文件上传的需求,可以修改一下最大文件上传的尺寸:

upload_max_filesize=20M

步骤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配置:

client_max_body_size 50M;

步骤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扩展的实现,部署及应用相关推荐

  1. python在windows上的扩展名_python – 如何克服 – 在windows上使用文件名或扩展名失败的pip install ansible...

    好吧,这似乎是一个已知的错误: In Ansible 2.4.0,the module copy use symbolic links in the tests suite: 07001 circle ...

  2. 在windows上搭建DZ(Discuz)论坛-部署完成

    部署过程 用到的工具软件 phpstudy https://www.xp.cn/ discuz安装包 https://gitee.com/3dming/DiscuzL/attach_files 参考详 ...

  3. Elasticsearch-03 CentOS7 / Windows上部署Elasticsearch5.6.16集群模式

    文章目录 概述 CentOS上部署ES集群 集群组成 关键配置信息 Master节点搭建 Slave1节点搭建 Slave2节点搭建 Windows 部署 ES集群 elasticsearch.yml ...

  4. 在 Windows 上部署 gitblit

    在 Windows 上部署 gitblit 在 Windows 上部署 gitblit 缘起 gitblit 是什么 安装JDK 部署 gitblit 下载 gitblit 并解压 配置 登录 注册为 ...

  5. Zabbix agent 在windows上安装部署

    Zabbix agent 在windows上安装部署 1.下载与解压 地址: http://www.zabbix.com/downloads/2.4.4/zabbix_agents_2.4.4.win ...

  6. polybase配置 sql_在 Windows 上配置 PolyBase 横向扩展组

    在 Windows 上配置 PolyBase 横向扩展组Configure PolyBase scale-out groups on Windows 04/23/2019 本文内容 适用于:Appli ...

  7. 物联网平台thingsboard在Windows上的安装和部署

    物联网平台thingsboard在Windows上的安装和部署 前言 安装环境 部署和安装 测试 http测试 测试MQTT 前言 thingsboard是一个完全开源的物联网应用平台,个人感觉如果不 ...

  8. 在windows上部署IIS web服务

    在windows上部署IIS web服务 在windows上部署IIS web服务安装IIS相关环境并利用IIS服务器发布靶站源代码(注意应用程序池使用.net 4.0并开启.NET服务) 1.1程序 ...

  9. windows上配置TensorRT yolov5 -6.0部署 tensorrtx视频流推理

    目录 前言 前置环境 一,TensorRT下载安装 二,tensorrtx正常windows部署,图片推理 2.1 VS配置(这一步骤二和三通用) 2.2 导出wts模型 2.3 生成engine引擎 ...

最新文章

  1. 口碑扑街光环不在,2018将是苹果手机最难熬的年头!
  2. 周围剃光头顶留长发型_为什么很多秃头的人,宁愿周围留一圈头发,也不愿剃成光头?...
  3. outlook 发送邮件!
  4. Game On Serverless:SAE 助力广州小迈提升微服务研发效能
  5. 【深度学习】超级赞!N个神经网络可视化利器
  6. android:persistent (非系统app失效)
  7. MySQL 排名函数.md
  8. 别不信!servlet获取到的参数值,也许完全出乎你的意料!
  9. 3个问题,1套非技术人员的AI方法论 | 哈佛商业评论最新热文
  10. [转载] Python 3 集合方法 remove( )
  11. 前景检测算法(十)--SOBS算法
  12. 【一天一个C++小知识】009.C++面向对象
  13. html怎么改变图片整体大小,html怎么改变图片大小
  14. 云计算实验(二)Hadoop 练习
  15. 迅捷网络路由器服务器无响应怎么办,迅捷(FAST)300M无线路由器设置后不能上网怎么办?...
  16. html中加大p的距离,html中P标签段落与CSS段落间距距离调整
  17. win7无法安装gpt、mbr问题
  18. Spring+SpringMVC+Jsp实现校园二手交易系统
  19. 芯片封装技术——Wire Bond与Flip Chip
  20. Scylladb 高可用测试结果

热门文章

  1. c++11-std::functionbind
  2. 服务器php 不能运行框架,经验总结 PHP框架常见错误
  3. 学习使用 Manifest
  4. hibernate merge saveorupdate save lock
  5. (读书笔记).NET大局观-.NET框架类库概观
  6. 洛谷.4897.[模板]最小割树(Dinic)
  7. 【转】Linux系统安装Redis详细过程
  8. 【洛谷】【动态规划+单调队列】P1725 琪露诺
  9. php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数...
  10. variable `xxx' has initializer but incomplete type