使用php imagick库创建QR码图像

jan*_*mon 4 php qr-code imagick

在此输入图像描述

嗨,

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

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

纯粹用PHP实现,除GD2外没有外部依赖

是否有任何使用imagick创建QR码的 php片段或

iND*_*iND 8

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

下面是我写的替换qrimage.php的可能的imagick输出文件.但是,我无法测试此代码,因为我在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;
                }
            }
        }    
    }
Run Code Online (Sandbox Code Playgroud)

有一个名为phpqrcode.php的合并文件,其中包含整个qrimage.php,因此您要么必须重新合并该文件,要么替换相关部分.

如果对上面的代码使用不同的文件名,则必须更改文件qrlib.phpmerge.php中的引用.