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

add transparency mode for png images #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/*
* PHP QR Code encoder
*
Expand All @@ -22,7 +22,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

echo "<h1>PHP QR Code</h1><hr/>";
echo "<h1>PHP QR Code</h1><hr/>";

//set it to writable location, a place for temp generated PNG files
$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
Expand All @@ -34,7 +34,7 @@

//ofcourse we need rights to create temp dir
if (!file_exists($PNG_TEMP_DIR))
mkdir($PNG_TEMP_DIR);
mkdir($PNG_TEMP_DIR);


$filename = $PNG_TEMP_DIR.'test.png';
Expand All @@ -49,27 +49,33 @@
if (isset($_REQUEST['size']))
$matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);


$backTransparent = false;
if (isset($_REQUEST['back_transparent']))
$backTransparent = (bool) $_REQUEST['back_transparent'];


if (isset($_REQUEST['data'])) {

//it's very important!
if (trim($_REQUEST['data']) == '')
die('data cannot be empty! <a href="?">back</a>');

// user data
$filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);

$filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2, false, 0xFFFFFF, 0x000000, $backTransparent);
} else {

//default data
echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';
QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);

}
//default data
echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';
QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2, false, 0xFFFFFF, 0x000000, $backTransparent);

//display generated file
echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';
}

//display generated file
echo '<div style="background-color: #c097e0; padding: 1em;">
<img src="'.$PNG_WEB_DIR.basename($filename).'" />
</div><hr/>';

//config form
echo '<form action="index.php" method="post">
Expand All @@ -86,9 +92,10 @@
echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';

echo '</select>&nbsp;
<input type="checkbox" name="back_transparent" value="backTransparent"'.(($backTransparent==true)?' checked':'').'> Transparent
<input type="submit" value="GENERATE"></form><hr/>';

// benchmark
QRtools::timeBenchmark();


// benchmark
QRtools::timeBenchmark();
9 changes: 5 additions & 4 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, $back_color = 0xFFFFFF, $fore_color = 0x000000)
public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000, $back_transparent = false)
{
$enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
$enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color, false, $back_transparent);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}

Expand Down Expand Up @@ -424,14 +424,15 @@ class QRencode {
public $hint = QR_MODE_8;

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

switch ($level.'') {
case '0':
Expand Down Expand Up @@ -510,7 +511,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, $this->back_color, $this->fore_color);
QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color, $this->back_transparent);

} catch (Exception $e) {

Expand Down
14 changes: 10 additions & 4 deletions qrimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
class QRimage {

//----------------------------------------------------------------------
public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color)
public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color, $back_transparent)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
$image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color, $back_transparent);

if ($filename === false) {
Header("Content-type: image/png");
Expand Down Expand Up @@ -63,7 +63,7 @@ public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outer
}

//----------------------------------------------------------------------
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $back_transparent = false)
{
$h = count($frame);
$w = strlen($frame[0]);
Expand Down Expand Up @@ -100,8 +100,14 @@ private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back

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

if ($back_transparent == true) {
$col[2] = imagecolorexact($target_image, $r2, $b2, $g2);
ImageColorTransparent($target_image, $col[2]);
}

ImageDestroy($base_image);

return $target_image;
}
}
}