QRcode for Zend Framework
I’ve been using QRcodes for quiet some time now on different projects, and so far I’ve always based my code generator on the PHP script written by Y.Swetake. It does the job pretty well, but there is definitely room for improvement:
- the code is very messy
- it’s almost impossible to figure out the meaning of each piece of code
- it’s not object oriented
- instead of using algorithms, arrays of predefined values are used for certain calculations
So I decided to write a QR matrixcode generator class for Zend Framework from scratch.
As there is not that much information to find on the internet about how a QR code is actually constructed, or what algorithms are used, I had to digg through all the relevant programming code I could find. One of the most valuable resources was a C++ library written by K. Fukuchi. The code was much better documented than the PHP version, and both combined, together with online documentation, I could start to figure it out piece by piece.
I must say it didn’t progress as fast as I hoped. I hadn’t been analyzing code like that for quite some time. Line by line, in different programming languages, jumping around from one function to the other trying to keep track. But you’ve gotta love that kind of challenges.
So once I had a much better idea of how a QR code is built, I started writing my own classes. I first checked if there was already a Zend Framework component that was related to QR codes or matrixcodes in general, but the only component that came close was Zend_Image_Barcode which is still a prototype and thus not part of the official library distribution.
I hoped I could base my own component on the structure of Zend_Image_Barcode, but there was something I didn’t like about it. A barcode is not necessarily an image. So to me it didn’t make much sense to extend the Zend_Image class. I did however plan to use the same methods like ‘render’ and ‘draw’ etc.
A QR code is in fact a matrixcode, just like Quick Code and Data Matrix. I was only going to implement the former for now, but it seemed to be a good idea to create a Matrixcode component that could hold all the different types of two dimentional codes. My QR code generator class is one of them, and an instance is created by the factory method of the Matrixcode class.
Because I didn’t want to limit the output format to png, gif and jpg, I decided to create an abstract rendering class that could be extended for each type of output rendering. In this specific case, I wanted png, gif, jpg, svg, and pdf (vectorial) to begin with.
So after a weekend of intense programming I managed to get a decent looking component. Since now I actually understand every part of it, I added some extra functionality like transparent backgrounds, adding a watermark, rendering in vector format, creating a border, etc.
There is still some work to do though. I would like to create an abstract matrixcode class, but I don’t really know yet in how far I could go with it. Well, I suppose I could keep it very, eehm… abstract. There is also one more algorithm to implement, some exception handling to do, etc.
But anyhow, if you’d be interested to check out the component like it is now, just leave me a comment.
UPDATE: Meanwhile the code has been released.
Comments are closed.