Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
fixed qrcode orientation bug plus added support for colored QRcode
Browse files Browse the repository at this point in the history
  • Loading branch information
t0k4rt committed Feb 13, 2012
1 parent 40b7371 commit d7d79e9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 129 deletions.
9 changes: 8 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ Usage : QRcode::eps('arguments');
Added support for SVG export
Usage : QRcode::svg('arguments');

Added support for color export :
example :
$back_color = 0xFFFF00;
$fore_color = 0xFF00FF;
QRcode::png('some othertext 1234', false, 'h', 20, 1, false, $back_color, $fore_color);


Copyright (C) 2012 by Alexandre Assouad

== LICENSING ==
Expand Down Expand Up @@ -51,4 +58,4 @@ countries.

Reed-Solomon code encoder is written by Phil Karn, KA9Q.
Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q


24 changes: 14 additions & 10 deletions qrencode.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ public function encodeString($string, $version, $level, $hint, $casesensitive)
}

//----------------------------------------------------------------------
public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
{
$enc = QRencode::factory($level, $size, $margin);
$enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}

Expand All @@ -294,16 +294,16 @@ public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $siz
}

//----------------------------------------------------------------------
public static function eps($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
public static function eps($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
{
$enc = QRencode::factory($level, $size, $margin);
$enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
return $enc->encodeEPS($text, $outfile, $saveandprint=false);
}

//----------------------------------------------------------------------
public static function svg($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
public static function svg($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
{
$enc = QRencode::factory($level, $size, $margin);
$enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
return $enc->encodeSVG($text, $outfile, $saveandprint=false);
}

Expand Down Expand Up @@ -415,18 +415,22 @@ class QRencode {
public $version = 0;
public $size = 3;
public $margin = 4;
public $back_color = 0xFFFFFF;
public $fore_color = 0x000000;

public $structured = 0; // not supported yet

public $level = QR_ECLEVEL_L;
public $hint = QR_MODE_8;

//----------------------------------------------------------------------
public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4)
public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
{
$enc = new QRencode();
$enc->size = $size;
$enc->margin = $margin;
$enc->fore_color = $fore_color;
$enc->back_color = $back_color;

switch ($level.'') {
case '0':
Expand Down Expand Up @@ -505,7 +509,7 @@ public function encodePNG($intext, $outfile = false,$saveandprint=false)

$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));

QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);

} catch (Exception $e) {

Expand All @@ -529,7 +533,7 @@ public function encodeEPS($intext, $outfile = false,$saveandprint=false)

$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));

return QRvect::eps($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
return QRvect::eps($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);

} catch (Exception $e) {

Expand All @@ -553,7 +557,7 @@ public function encodeSVG($intext, $outfile = false,$saveandprint=false)

$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));

return QRvect::svg($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
return QRvect::svg($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);

} catch (Exception $e) {

Expand Down
200 changes: 106 additions & 94 deletions qrimage.php
Original file line number Diff line number Diff line change
@@ -1,95 +1,107 @@
<?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);

if ($filename === false) {
Header("Content-type: image/png");
ImagePng($image);
} else {
if($saveandprint===TRUE){
ImagePng($image, $filename);
header("Content-type: image/png");
ImagePng($image);
}else{
ImagePng($image, $filename);
}
}

ImageDestroy($image);
}

//----------------------------------------------------------------------
public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame);

if ($filename === false) {
Header("Content-type: image/jpeg");
ImageJpeg($image, null, $q);
} else {
ImageJpeg($image, $filename, $q);
}

ImageDestroy($image);
}

//----------------------------------------------------------------------
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
{
$h = count($frame);
$w = strlen($frame[0]);

$imgW = $w + 2*$outerFrame;
$imgH = $h + 2*$outerFrame;

$base_image =ImageCreate($imgW, $imgH);

$col[0] = ImageColorAllocate($base_image,255,255,255);
$col[1] = ImageColorAllocate($base_image,0,0,0);

imagefill($base_image, 0, 0, $col[0]);

for($y=0; $y<$h; $y++) {
for($x=0; $x<$w; $x++) {
if ($frame[$y][$x] == '1') {
ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
}
}
}

$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
ImageDestroy($base_image);

return $target_image;
}
<?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, $back_color, $fore_color)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);

if ($filename === false) {
Header("Content-type: image/png");
ImagePng($image);
} else {
if($saveandprint===TRUE){
ImagePng($image, $filename);
header("Content-type: image/png");
ImagePng($image);
}else{
ImagePng($image, $filename);
}
}

ImageDestroy($image);
}

//----------------------------------------------------------------------
public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);

if ($filename === false) {
Header("Content-type: image/jpeg");
ImageJpeg($image, null, $q);
} else {
ImageJpeg($image, $filename, $q);
}

ImageDestroy($image);
}

//----------------------------------------------------------------------
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
{
$h = count($frame);
$w = strlen($frame[0]);

$imgW = $w + 2*$outerFrame;
$imgH = $h + 2*$outerFrame;

$base_image =ImageCreate($imgW, $imgH);

// convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
$r1 = round((($fore_color & 0xFF0000) >> 16), 5);
$b1 = round((($fore_color & 0x00FF00) >> 8), 5);
$g1 = round(($fore_color & 0x0000FF), 5);

// convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
$r2 = round((($back_color & 0xFF0000) >> 16), 5);
$b2 = round((($back_color & 0x00FF00) >> 8), 5);
$g2 = round(($back_color & 0x0000FF), 5);



$col[0] = ImageColorAllocate($base_image,$r2,$b2,$g2);
$col[1] = ImageColorAllocate($base_image,$r1,$b1,$g1);

imagefill($base_image, 0, 0, $col[0]);

for($y=0; $y<$h; $y++) {
for($x=0; $x<$w; $x++) {
if ($frame[$y][$x] == '1') {
ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
}
}
}

$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
ImageDestroy($base_image);

return $target_image;
}
}
Loading

0 comments on commit d7d79e9

Please sign in to comment.