/** * Encodes string into QR code image * @param $data * @return Image */ public static function encode($data) { $process = ProcessBuilder::create(['qrencode', '-l', 'L', '-s', '8', '-m', '0', '-o', '-', $data])->getProcess(); $process->run(); if ($process->isSuccessful()) { $image = Image::fromBinary($process->getOutput()); return $image; } }
/** * @param $png string binary content of image */ public static function fromImage(Image $image) { $bytesPerRow = $image->getWidth() / 8; $bits = ''; for ($y = 0; $y < $image->getHeight(); $y++) { for ($x = 0; $x < $image->getWidth(); $x++) { $bits .= $image->isFilledAt($x, $y) ? '1' : '0'; } } // split bits array to one-byte packs $bytes = implode('', array_map(function ($byte) { return str_pad(dechex(bindec($byte)), 2, '0', STR_PAD_LEFT); }, str_split($bits, 8))); return new self($bytes, $bytesPerRow, true); }