使用php imagick库创建二维码图像

使用php imagick库创建二维码图像,php,qr-code,imagick,Php,Qr Code,Imagick,嗨 我们正在使用imagick进行不同的图像处理,并请求添加 QR水印在最后 现在我只能找到使用GD2库的: 纯用PHP实现,除了GD2之外没有外部依赖项 是否有任何php代码片段或库使用imagick创建二维码?查看php二维码库,只有一个文件(我认为)可以访问GD库:qrimage.php。因此,将该文件更改为通过imagick输出,并使用PHP二维码的其余部分 下面是我编写的一个可能的imagick输出文件,用于替换qrimage.php。但是,我无法测试此代码,因为我在Windows上,

我们正在使用imagick进行不同的图像处理,并请求添加 QR水印在最后

现在我只能找到使用GD2库的:

纯用PHP实现,除了GD2之外没有外部依赖项


是否有任何php代码片段或使用imagick创建二维码

查看php二维码库,只有一个文件(我认为)可以访问GD库:qrimage.php。因此,将该文件更改为通过imagick输出,并使用PHP二维码的其余部分

下面是我编写的一个可能的imagick输出文件,用于替换qrimage.php。但是,我无法测试此代码,因为我在Windows上,无法安装imagick

有人能调试一下,并修改一下这篇文章吗?

<?php
/*
 * PHP QR Code encoder
 *
 * Image output of code using GD2
 *
 * PHP QR Code is distributed under LGPL 3
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

    define('QR_IMAGE', true);

    class QRimage {

        //----------------------------------------------------------------------
        public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE) 
        {
            $image = self::image($frame, $pixelPerPoint, $outerFrame, "png", 85, $filename, $saveandprint);
        }

        //----------------------------------------------------------------------
        public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) 
        {
            $image = self::image($frame, $pixelPerPoint, $outerFrame, "jpeg", $q, $filename, $saveandprint);
        }

        //----------------------------------------------------------------------
        private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, 
            $format = "png", $quality = 85, $filename = FALSE, $saveandprint = FALSE) 
        {
            $imgH = count($frame);
            $imgW = strlen($frame[0]);

            $col[0] = new ImagickPixel("white");
            $col[1] = new ImagickPixel("black");

            $image = new Imagick();
            $image->newImage($imgW, $imgH, $col[0]);

            $image->setCompressionQuality($quality);
            $image->setImageFormat($format); 

            $draw = new ImagickDraw();
            $draw->setFillColor($col[1]);

            for($y=0; $y<$imgH; $y++) {
                for($x=0; $x<$imgW; $x++) {
                    if ($frame[$y][$x] == '1') {
                        $draw->point($x,$y); 
                    }
                }
            }

            $image->drawImage($draw);
            $image->borderImage($col[0],$outerFrame,$outerFrame);
            $image->scaleImage( $imgW * $pixelPerPoint, 0 );

            if ($filename === FALSE) {
                Header("Content-type: image/jpeg");
                echo $image;
            } else {
                if($saveandprint===TRUE){
                    $image->writeImages($filename, true);        
                    Header("Content-type: image/" . $format);
                    echo $image;
                } else {
                    echo $image;
                }
            }
        }    
    }

我测试了上面的实现,它成功了。
有一个错误:您没有在最终图像大小中添加外部帧

$image->scaleImage( ($imgW + 2*$outerFrame) * $pixelPerPoint, 0 );
此外,在这种情况下,GD库似乎比ImageMagick快得多

我将GD和imagick用于创建相同的50个随机二维码。 我隔离的部分都是QR码生成的,所以实际上使用的是QRimage::png

我只测试png一代。 以下是我的结果:

GD:

  • 最小时间:00148401260秒
  • 最长时间:0021210251秒
  • 平均值:00167747593秒
ImageMagick:

  • 最小时间:00799968243秒
  • 最长时间:01147611141秒
  • 平均值:00918840790秒

在最后的代码中,这会有一点不同。代码的另一部分需要大约0.15秒的时间来运行,在大量代码上它会产生不同(我将QRcode::png定为基准,结果是:GD时每个QRcode 0.17秒,imagemagick时每个代码0.24秒).

ImageMagick与其说是用于图像绘制,不如说是用于转换和转换。我的经验是,ImageMagick比GD有更多的功能,但php库几乎没有文档记录。只是看了一眼,它实际上是文档记录的。但我认为它不太常用,因为它不太普及,而且是最近才增加的。GD至少在PHP中出现过。您是对的,所有的命令都提到了,但是许多命令和常量没有得到很好的解释,比如不确定为什么没有更多的投票。也许这不是一个常见的问题。非常感谢-节省了大量的时间。你有这样的分叉回购协议吗?