Skip to content

Commit

Permalink
增加qrcode等依赖 #2028
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 20, 2016
1 parent abc520c commit ae7d93d
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/Controller/Qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function showAction($req)
$enc = \QRencode::factory($level, $size, 0);
$tab = $enc->encode($text);
$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)));
$image = \QRimage::image($tab, min($size, $maxSize), 0);
$image = $this->image($tab, min($size, $maxSize), 0);

// TODO 支持远程图片
// 将LOGO放置到图片中间
Expand Down Expand Up @@ -102,4 +102,61 @@ public function showAction($req)

return $content;
}

/**
* @see QRimage::image
*
* @param $frame
* @param int $pixelPerPoint
* @param int $outerFrame
* @param int $back_color
* @param int $fore_color
* @return resource
*/
protected 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 format (red = 255 0 0, green = 0 255 0, blue = 0 0 255)
$r1 = round((($fore_color & 0xFF0000) >> 16), 5);
$g1 = round((($fore_color & 0x00FF00) >> 8), 5);
$b1 = round(($fore_color & 0x0000FF), 5);

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


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

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;
}
}

0 comments on commit ae7d93d

Please sign in to comment.