Skip to content

Instantly share code, notes, and snippets.

@adamyi
Last active January 23, 2017 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamyi/2300df41cb900638b5f8ec7464840390 to your computer and use it in GitHub Desktop.
Save adamyi/2300df41cb900638b5f8ec7464840390 to your computer and use it in GitHub Desktop.
Metzli PngRenderer rewritten with Imagick in PHP to generate Aztec Code
<?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