Windows上PHP扩展的实现,部署及应用

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

软件下载

步骤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 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:做测试:

18151545_qbYM.png

18151547_x0SB.png

源码

https://github.com/dynamsoftsamples/php-barcode-reader


转载于:https://my.oschina.net/yushulx/blog/547333

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值