diff --git a/examples/image.php b/examples/image.php index 97184fa8f..addbaf0e4 100644 --- a/examples/image.php +++ b/examples/image.php @@ -2,14 +2,18 @@ require_once '../vendor/autoload.php'; -use codemasher\QRCode\QRCode; -use codemasher\QRCode\QRConst; +use chillerlan\QRCode\Output\QRImage; +use chillerlan\QRCode\QRCode; +use chillerlan\QRCode\QRConst; +use chillerlan\QRCode\QROptions; -// google authenticator -$qr = new QRCode('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', QRConst::ERROR_CORRECT_LEVEL_L); +$qrOptions = new QROptions; +$qrOptions->output = new QRImage(); + +$qr = new QRCode('https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s', $qrOptions); header('Content-type: image/png'); -$im = $qr->createImage(5, 5); +$im = $qr->output(); imagepng($im); imagedestroy($im); diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php new file mode 100644 index 000000000..8a1a339b7 --- /dev/null +++ b/src/Output/QRImage.php @@ -0,0 +1,127 @@ + + * @copyright 2015 Smiley + * @license MIT + */ + +namespace chillerlan\QRCode\Output; +use chillerlan\QRCode\Util; + +/** + * toBase64() + */ +class QRImage extends QROutputBase implements QROutputInterface{ + + /** + * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions + */ + protected $options; + + /** + * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions + */ + public function __construct(QRImageOptions $outputOptions = null){ + $this->options = $outputOptions; + + if(!$this->options instanceof QRImageOptions){ + $this->options = new QRImageOptions; + } + + } + + + + /** + * added $fg (foreground), $bg (background), and $bgtrans (use transparent bg) parameters + * also added some simple error checking on parameters + * updated 2015.07.27 ~ DoktorJ + * + * @param int $size + * @param int $margin + * @param int $fg + * @param int $bg + * @param bool|false $bgtrans + * + * @return resource + */ + public function toImage($size = 2, $margin = 2, $fg = 0x000000, $bg = 0xFFFFFF, $bgtrans = false){ + + // size/margin EC + if(!is_numeric($size)){ + $size = 2; + } + if(!is_numeric($margin)){ + $margin = 2; + } + if($size < 1){ + $size = 1; + } + if($margin < 0){ + $margin = 0; + } + + $image_size = $this->pixelCount * $size + $margin * 2; + + $image = imagecreatetruecolor($image_size, $image_size); + + // fg/bg EC + if($fg < 0 || $fg > 0xFFFFFF){ + $fg = 0x0; + } + if($bg < 0 || $bg > 0xFFFFFF){ + $bg = 0xFFFFFF; + } + + // convert hexadecimal RGB to arrays for imagecolorallocate + $fgrgb = Util::hex2rgb($fg); + $bgrgb = Util::hex2rgb($bg); + + // replace $black and $white with $fgc and $bgc + $fgc = imagecolorallocate($image, $fgrgb['r'], $fgrgb['g'], $fgrgb['b']); + $bgc = imagecolorallocate($image, $bgrgb['r'], $bgrgb['g'], $bgrgb['b']); + if($bgtrans){ + imagecolortransparent($image, $bgc); + } + + // update $white to $bgc + imagefilledrectangle($image, 0, 0, $image_size, $image_size, $bgc); + + for($r = 0; $r < $this->pixelCount; $r++){ + for($c = 0; $c < $this->pixelCount; $c++){ + if($this->matrix[$r][$c]){ + + // update $black to $fgc + imagefilledrectangle($image, + $margin + $c * $size, + $margin + $r * $size, + $margin + ($c + 1) * $size - 1, + $margin + ($r + 1) * $size - 1, + $fgc); + } + } + } + + return $image; + } + + public function dump(){ + return $this->toImage(); + } + + protected function toPNG(){ + + } + + /** + * Actually, it's pronounced "DJIFF". *hides* + */ + protected function toGIF(){ + + } +} diff --git a/src/Output/QRImageOptions.php b/src/Output/QRImageOptions.php new file mode 100644 index 000000000..80d82dbf4 --- /dev/null +++ b/src/Output/QRImageOptions.php @@ -0,0 +1,19 @@ + + * @copyright 2015 Smiley + * @license MIT + */ + +namespace chillerlan\QRCode\Output; + +/** + * Class QRImageOptions + */ +class QRImageOptions{ + +}