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

标签 php qr-code imagick

enter image description here

你好,

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

现在我只能找到PHP QR Code library它使用 GD2 库:

Implemented purely in PHP, no external dependencies except GD2

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

最佳答案

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

下面是我编写的用于替换 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;
                }
            }
        }    
    }

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

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

关于php - 使用 php imagick 库创建二维码图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669592/

相关文章:

php - Apache 的符号链接(symbolic link)错误 403

php - 如何在扫描后在Google QR码URL中包含%20空格?

qr-code - 是否可以生成不纠错的二维码?

javascript - 如何检索存储在数据库中的数据并将其显示为二维码?,

PHP imagick 将图像从 CMYK 转换为 RGB 反转图像

php - Laravel 在 init 时崩溃,错误 500(无附加信息)

插入前的 PHP 表单重复数据删除

qr-code - 是否可以解码不完整的二维码?

php - 仅使用 Imagick 创建第一页的在线 PDF 缩略图

php - 如何使用 PHP Imagick 根据文本框大小对齐文本?