Last active
January 23, 2017 04:14
-
-
Save adamyi/2300df41cb900638b5f8ec7464840390 to your computer and use it in GitHub Desktop.
Metzli PngRenderer rewritten with Imagick in PHP to generate Aztec Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// MetzzliRenderer | |
// Part of MUNPANEL System (https://www.munpanel.com/) | |
// Under Apache 2.0 License | |
// By Adam Yi | |
use \Imagick; | |
use \ImagickDraw; | |
use Metzli\Encoder\Encoder; | |
use Metzli\Encoder\AztecCode; | |
/** | |
* Render AztecCode Class to Imagick class | |
* @param $code An AztecCode class instance containing the matrix of the code | |
* @param $sizeX The width of the aztec code image | |
* @param $sizeY The height of the aztec code image | |
* @return An Imagick instance of the aztec code | |
*/ | |
private function render(AztecCode $code, $sizeX, $sizeY) // Rewritten according to Metzli\Renderer\PngRenderer, by Adam Yi | |
{ | |
$matrix = $code->getMatrix(); | |
$factorX = $sizeX / $matrix->getWidth(); | |
$factorY = $sizeY / $matrix->getHeight(); | |
$img = new Imagick(); | |
$img->newImage($sizeX, $sizeY, 'transparent'); | |
$img->setImageFormat("png"); | |
//$img->setImageColorspace (imagick::COLORSPACE_CMYK); | |
$draw = new ImagickDraw(); | |
$draw->setFillColor('#000000'); | |
for ($x = 0; $x < $matrix->getWidth(); $x++) { | |
for ($y = 0; $y < $matrix->getHeight(); $y++) { | |
if ($matrix->get($x, $y)) { | |
$draw->rectangle($x * $factorX, $y * $factorY, (($x + 1) * $factorX ), (($y + 1) * $factorY )); | |
// We don't know why Metzli minuses one here, but it seems great if we don't. | |
//imagefilledrectangle($im, $x * $f, $y * $f, (($x + 1) * $f - 1), (($y + 1) * $f - 1), $fg); | |
} | |
} | |
} | |
$img->drawImage($draw); | |
return $img; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment