Edit QR code online and send to thermal printer for printing

Source: Internet
Author: User
Online editing

JS Plugin to use:

Qrcodesvg

Draw two-dimensional code plug-in colorpicker based on information

For color selection, JS binding event Change the color of the QR Code (SVG) CANVG

Used to convert the SVG format QR code into a HTML5 canvas, and then use the Todataurl method to generate the base64 encoded data of a two-dimensional code image, and send the QR code to the backend thermal printer via Ajax

Picture to be converted into BMP format. To convert a picture to a BMP class:

  1. /**
  2. * Class jpg, GIF, PNG =========> BMP
  3. *
  4. * {Description:-
  5. * Class that resize and convert jpg, GIF or PNG to BMP
  6. * }
  7. * for + info contact with me (mahabub1212@yahoo.com)
  8. * You can modify or use or redistribute this class.
  9. */
  10. Class tobmp{
  11. New Image width
  12. var $new _width;
  13. New Image Height
  14. var $new _height;
  15. Image Resources
  16. var $image _resource;
  17. function Image_info ($source _image) {
  18. $img _info = getimagesize ($source _image);
  19. Switch ($img _info[' mime ') {
  20. Case "Image/jpeg": {$this->image_resource = imagecreatefromjpeg ($source _image);
  21. Case "Image/gif": {$this->image_resource = imagecreatefromgif ($source _image);
  22. Case "Image/png": {$this->image_resource = imagecreatefrompng ($source _image);
  23. Default: {die ("Picture error");}
  24. }
  25. }
  26. Public Function imagebmp ($file _path = ") {
  27. if (! $this->image_resource) die ("Picture error");
  28. $picture _width = imagesx ($this->image_resource);
  29. $picture _height = Imagesy ($this->image_resource);
  30. if (!imageistruecolor ($this->image_resource)) {
  31. $tmp _img_reource = Imagecreatetruecolor ($picture _width, $picture _height);
  32. Imagecopy ($tmp _img_reource, $this->image_resource, 0, 0, 0, 0, $picture _width, $picture _height);
  33. Imagedestroy ($this->image_resource);
  34. $this->image_resource = $tmp _img_reource;
  35. }
  36. if ((int) $this->new_width >0 && (int) $this->new_height > 0) {
  37. $image _resized = Imagecreatetruecolor ($this->new_width, $this->new_height);
  38. Imagecopyresampled ($image _resized, $this->image_resource,0,0,0,0, $this->new_width, $this->new_height,$ Picture_width, $picture _height);
  39. Imagedestroy ($this->image_resource);
  40. $this->image_resource = $image _resized;
  41. }
  42. $result = ";
  43. $biBPLine = ((int) $this->new_width >0 && (int) $this->new_height > 0)? $this->new_width * 3: $picture _width * 3;
  44. $biStride = ($biBPLine + 3) & ~;
  45. $biSizeImage = ((int) $this->new_width >0 && (int) $this->new_height > 0)? $biStride * $this->new_height: $biStride * $picture _height;
  46. $bfOffBits = 54;
  47. $bfSize = $bfOffBits + $biSizeImage;
  48. $result. = substr (' BM ', 0, 2);
  49. $result. = Pack (' VvvV ', $bfSize, 0, 0, $bfOffBits);
  50. $result. = ((int) $this->new_width >0 && (int) $this->new_height > 0)? Pack (' VVVVVVVVVVV ', Max, $this->new_width, $this->new_height, 1, 0, $biSizeImage, 0, 0, 0, 0): Pack (' VVVVVVVVV VV ', _width, $picture, $picture _height, 1, 0, $biSizeImage, 0, 0, 0, 0);
  51. $numpad = $biStride-$biBPLine;
  52. $h = ((int) $this->new_width >0 && (int) $this->new_height > 0)? $this->new_height: $picture _height;
  53. $w = ((int) $this->new_width >0 && (int) $this->new_height > 0)? $this->new_width: $picture _width;
  54. for ($y = $h-1; $y >= 0;--$y) {
  55. for ($x = 0; $x < $w; + + $x) {
  56. $col = Imagecolorat ($this->image_resource, $x, $y);
  57. $result. = substr (Pack (' V ', $col), 0, 3);
  58. }
  59. for ($i = 0; $i < $numpad; + + $i) {
  60. $result. = Pack (' C ', 0);
  61. }
  62. }
  63. if ($file _path = = ") {
  64. Header ("Content-type:image/bmp");
  65. echo $result;
  66. } else {
  67. $fp = fopen ($file _path, "WB");
  68. Fwrite ($fp, $result);
  69. Fclose ($FP);
  70. //=============
  71. }
  72. return;
  73. }
  74. }
Copy Code

How to use

    1. $ToBMP = new Tobmp ();
    2. $ToBMP->image_info ($path _to_img);
    3. $ToBMP->new_width = 255;
    4. $ToBMP->new_height = 255;
    5. $output _path = Realpath (path. ' Test.bmp ');
    6. $ToBMP->imagebmp ($output _path);
Copy Code

BMP format According to file header information different data composition structure is also different

My is 24-bit BMP, after removing the header file 54 bytes, each three bytes (RGB) represents a point.

Merge RGB (three bytes merged into one byte) after two to value the pixel point (get 1bit, that is, the point is black or white. The reason for the binary is because my thermal printer printing black and white does not print color.

Each 8bit is stitched into 1 bytes and is represented by the ' \xx ' 16 binary form. The data is sent to the printer according to the interface that the printer sends to print.

Special attention:

Use of 1.unpack

    1. $content = file_get_contents ($path _to_img);
    2. $content = Unpack ("h*", $content); Get 16 binary representations of picture data
Copy Code

2.

' \xfe ' represents 4 characters

"\xfe" represents 1 characters (that is, the ASCII character corresponding to the 16 binary digits)

Single quotes can be converted using Chr (' 0xFE ')

Printer printing
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.