-
Notifications
You must be signed in to change notification settings - Fork 684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compress QR code #102
Comments
Not that anyone cares but I got it even more compressed...
|
In case someone cares... public function createSVG($title='', $size=2, $color='black/white') {
$size = max(2, $size);
// $color usage: fgc OR fgc/bgc OR fgc/bgc/brdc
list($fgc, $bgc, $brdc) = array_pad(explode('/',$color), 3, '');
$margin = ($brdc ? $size+1 : 0); // if border(-color) add margin
$n = $this->getModuleCount();
$svgSize = $n * $size + 2 * $margin; // square; default: viewBox 0 0 $svgSize $svgSize
$svgOut = "<svg width=$svgSize height=$svgSize xmlns='http://www.w3.org/2000/svg'>";
if ($title) $svgOut .= "<title>$title</title>";
// no bgc = 'transparent' background, no brdc = no border
if ($bgc || $brdc) { // background or border color
if (!$bgc) $bgc = 'transparent';
$attr = "fill='$bgc'" . ($brdc ? " stroke='$brdc' stroke-width=1" : '');
$svgOut .= "<rect x=0 y=0 width=$svgSize height=$svgSize $attr />";
}
// use path to draw hor.line sections; default: stroke-linecap="butt"
$svgOut .= "<path fill='none' stroke='$fgc' stroke-width=$size d='";
for ($r = 0; $r < $n; $r++) {
$y = (int)floor(($r + 0.5) * $size) + $margin; // half-way rows!
$svgOut .= "M$margin $y"; // newline (abs.move)
$currX = $lastX = $margin;
for ($c = 0; $c < $n; $c++) {
$new = $this->isDark($r,$c);
if ($c==0) $curr = $new; // first cell
elseif ($new!==$curr) { // dark <-> notDark
$x = $currX - $lastX; // relative distance
if ($curr) $svgOut .= "h$x"; // draw horz.
else $svgOut .= "m$x 0"; // move horz.
$curr = $new;
$lastX = $currX;
}
$currX += $size;
}
if ($curr) { // last cell
$x = $currX - $lastX;
$svgOut .= "h$x"; // draw until eol
}
}
$svgOut .= "'/></svg>";
return $svgOut;
} NB. a 2-color PNG is still the smallest QRcode to publish |
Damn! You guys did a great work. @maxpelic @RIMhosting |
The QR code generator function does not compress the data at all, changing it to something like
makes the output data waaaaay smaller while still printing the same thing (it has a transparent background but that could easily be changed by just adding a white rect first).
The text was updated successfully, but these errors were encountered: