Create a QR code image using php imagick library

enter image description here

Hello,

We use imagick for various image manipulations and have a request to add QR watermarks at the end.

Now I could find a PHP QR Code library that uses the GD2 library:

Implemented exclusively in PHP, no external dependencies except GD2

Is there a php snippet or library that uses imagick to create QR codes ?

+4
source share
2 answers

Looking at the PHP QR Code library, there is only one file (I think) that accesses the GD library: qrimage.php . Therefore, change this file to output via imagick and use the rest of the PHP QR Code.

Below is a possible imagix output file that I wrote to replace qrimage.php . However, I cannot verify this code, as I am on Windows and cannot install imagick.

Can anyone debug it and edit this post with any corrections?

<?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; } } } } 

There is a combined file called phpqrcode.php that contains the whole qrimage.php , so you will either have to overwrite this file or replace the corresponding section,

If you use a different file name for the above code, you will have to change the link in the qrlib.php and merge.php file .

+7
source

I tested the above implementation and it worked. There is one mistake: you missed adding an external frame to the final image size.

 $image->scaleImage( ($imgW + 2*$outerFrame) * $pixelPerPoint, 0 ); 

Also, it looks like the GD library is much faster than ImageMagick in this case.

I explained the use of GD and imagick when creating the same 50 random qr codes. I highlighted part of the QR code, so actually using QRimage :: png.

I am testing only png. These are my results:

Gd:

  • min time: 0.0148401260 s
  • maximum time: 0.0211210251 s
  • average: 0.0167747593 s

ImageMagick:

  • min time: 0.0799968243 s
  • Maximum time: 0.1147611141 s
  • average: 0.0918840790 s

In the final code, this is slightly different. The other part of the code takes about 0.15 seconds to run, and on a large number of codes it matters (I downloaded QRcode :: png with the result, for example: 0.17 s on qrcode with GD and 0.24 on one code with imagemagick) .

0
source

Source: https://habr.com/ru/post/1388474/


All Articles