如何从PHP中的字符串生成QR代码?

时间:2015-05-30 15:15:01

标签: php codeigniter

我一直在玩,试图从字符串生成QR码而没有任何运气。我正在使用CodeIgniter。我尝试了Packagist,bacon / bacon-qr-code和endroid / qrcode两个不同的软件包。下面是培根控制器中的代码:

$renderer = new \BaconQrCode\Renderer\Image\Png();
        $renderer->setHeight(256);
        $renderer->setWidth(256);
        $writer = new \BaconQrCode\Writer($renderer);
        $writer->writeFile('Hello World!', 'qrcode.png');

当我运行此代码时,我收到错误'未找到phpass类文件'。 所以我然后通过spark安装了phpass,我仍然得到同样的错误。谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:1)

第一个也在运作(也可能是第二个)。 你需要以这种方式使用它(至少):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); use \BaconQrCode\Renderer\Image\Png; use \BaconQrCode\Writer; class Qrcode { public function test() { $renderer = new Png(); $renderer->setHeight(256); $renderer->setWidth(256); $writer = new Writer($renderer); $writer->writeFile('Hello World!', 'qrcode.png'); //var_dump($writer); } }

APPPATH . 'controllers/Test.php'

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Test extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { $this->qrcode(); } public function qrcode() { $this->load->library('qrcode'); $this->qrcode->test(); } }

FCPATH . 'qrcode.png'

图像将在from visual import * import string import random class Cube: def __init__(self): self.surfaces = { 'r': (color.red, (1, 0, 0)), 'o': (color.orange, (-1, 0, 0)), 'y': (color.yellow, (0, 1, 0)), 'b': (color.blue, (0, -1, 0)), 'w': (color.white, (0, 0, 1)), 'g': (color.green, (0, 0, -1))} self.fps = 30 self.wholeSurfaces = [] self.initialPosition = [] self.commandsList = [] def createCube(self): for colour, axis in self.surfaces.itervalues(): for x in (-1, 0, 1): for y in (-1, 0, 1): # Start with all powierzchniaBoczna on the top face, then rotate them "down" # to the appropriate face. powBoczna = box(color=colour, pos=(x, y, 1.5), length=0.98, height=0.98, width=0.05) cos_kat = dot((0, 0, 1), axis) if cos_kat == 0: #alfa = 90 + kPI obliczonaOsObrotu = cross((0, 0, 1), axis) #iloczyn wektorowy else: obliczonaOsObrotu=(1, 0, 0) powBoczna.rotate(angle=acos(cos_kat), axis=obliczonaOsObrotu, origin=(0, 0, 0)) self.wholeSurfaces.append(powBoczna) #remember initial position v = (float(powBoczna.pos.x), float(powBoczna.pos.y), float(powBoczna.pos.z)) self.initialPosition.append(v) def solveCube(self): print self.commandsList self.commandsList.reverse() print self.commandsList for i in self.commandsList: self.rotateCube(self.reverseCommand(i), 10000) self.commandsList = [] def reverseCommand(self, key): if (key.islower()): return key.upper() else: return key.lower() def rotateCube(self, key, refreshRate): colour, axis = self.surfaces[key.lower()] if (key.isupper()): kat = (pi / 2.0) else: kat = -pi/2.0 for r in arange(0, kat, kat / self.fps): rate(refreshRate) for surface in self.wholeSurfaces: if dot(surface.pos, axis) > 0.5: surface.rotate(angle=kat / self.fps, axis=axis, origin=(0, 0, 0)) def beginLoop(self): while True: key = scene.kb.getkey() if (key.lower() in self.surfaces): self.commandsList.append(key) self.rotateCube(key, self.fps) elif key == "k": self.solveCube() elif key == "a": i = 0 print "=================" for surface in self.wholeSurfaces: print "%s\n(%s,%s,%s)" % (self.initialPosition[i], surface.pos.x, surface.pos.y, surface.pos.z) if self.initialPosition[i][0] == float(surface.pos.x) and self.initialPosition[i][1] == float(surface.pos.y) and self.initialPosition[i][2] == float(surface.pos.z): print "equal" else: print "not equal" print "" i+=1 if __name__ == "__main__": myCube = Cube() myCube.createCube() myCube.beginLoop() 文件中生成。

答案 1 :(得分:0)

您可以使用Google QR Codes API.

生成字符串的QR代码
https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=Hello+world&choe=UTF-8
  • 300x300 是您的图片尺寸。
  • chl - 用于将其转换为qr代码的网址编码字符串。