Php implements a QR code with a custom image in the middle

Source: Internet
Author: User
Php implements a QR code with a custom image in the middle

  1. Class QRCode {
  2. Public $ w;
  3. Public $ h;
  4. Public $ s;
  5. Function _ construct ($ w1, $ h1, $ s1 ){
  6. $ This-> w = $ w1;
  7. $ This-> h = $ h1;
  8. $ This-> s = $ s1;
  9. $ This-> outimgase ();
  10. }
  11. Function qrcode (){
  12. $ Post_data = array ();
  13. $ Post_data ['cht'] = 'QR ';
  14. $ Post_data ['chs '] = $ this-> w. "x". $ this-> h;
  15. $ Post_data ['check'] = $ this-> s;
  16. $ Post_data ['choe'] = "UTF-8 ";
  17. $ Url = "http://chart.apis.google.com/chart ";
  18. $ Data_Array = array ();
  19. Foreach ($ post_data as $ key => $ value)
  20. {
  21. $ Data_Array [] = $ key. '='. $ value;
  22. }
  23. $ Data = implode ("&", $ data_Array );
  24. $ Ch = curl_init ();
  25. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  26. Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
  27. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  28. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
  29. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  30. $ Result = curl_exec ($ ch );
  31. Curl_close ($ ch );
  32. Return $ result;
  33. }
  34. Function outimgase (){
  35. Echo $ this-> qrcode ();
  36. }
  37. }
  38. Header ("Content-type: image/png ");
  39. $ T = new QRCode (300,300, "tianxin ");

2. then, draw the QR code with your target image through a php file.

  1. $ Surl = $ _ POST ["url"];

  2. Function GrabImage ($ url, $ filename = ""){
  3. If ($ url = ""): return false; endif;
  4. If ($ filename = ""){
  5. $ Ext = strrchr ($ url ,".");
  6. If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
  7. $ Filename = date ("dMYHis"). $ ext;
  8. }
  9. Ob_start ();
  10. Readfile ($ url );
  11. $ Img = ob_get_contents ();
  12. Ob_end_clean ();
  13. $ Size = strlen ($ img );
  14. $ Fp2 = @ fopen ($ filename, "");
  15. Fwrite ($ fp2, $ img );
  16. Fclose ($ fp2 );
  17. Return $ filename;
  18. }
  19. $ Source = GrabImage ("http: // localhost/QRCode. php", "Myqrcode.png ");
  20. $ Water = GrabImage ($ surl, "t.png ");
  21. Function getImageInfo ($ img ){
  22. $ ImageInfo = getimagesize ($ img );
  23. If ($ imageInfo! = False ){
  24. $ ImageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1 ));
  25. $ ImageSize = filesize ($ img );
  26. $ Info = array (
  27. "Width" => $ imageInfo [0],
  28. "Height" => $ imageInfo [1],
  29. "Type" => $ imageType,
  30. "Size" => $ imageSize,
  31. "Mime" => $ imageInfo ['Mime ']
  32. );
  33. Return $ info;
  34. } Else {
  35. Return false;
  36. }
  37. }
  38. Function thumb ($ image, $ thumbname, $ type = '', $ maxWidth = 200, $ maxHeight = 50, $ interlace = true ){
  39. // Obtain the source image information
  40. $ Info = getImageInfo ($ image );
  41. If ($ info! = False ){
  42. $ SrcWidth = $ info ['width'];
  43. $ SrcHeight = $ info ['height'];
  44. $ Type = empty ($ type )? $ Info ['type']: $ type;
  45. $ Type = strtolower ($ type );
  46. $ Interlace = $ interlace? 1: 0;
  47. Unset ($ info );
  48. $ Scale = min ($ maxWidth/$ srcWidth, $ maxHeight/$ srcHeight); // calculates the scaling ratio.
  49. If ($ scale> = 1 ){
  50. // No longer scale out when the source image size is exceeded
  51. $ Width = $ srcWidth;
  52. $ Height = $ srcHeight;
  53. } Else {
  54. // Thumbnail size
  55. $ Width = (int) ($ srcWidth * $ scale );
  56. $ Height = (int) ($ srcHeight * $ scale );
  57. }
  58. // Load the source image
  59. $ CreateFun = 'imagecreatefrom '. ($ type = 'jpg '? 'Jpeg ': $ type );
  60. $ SrcImg = $ createFun ($ image );
  61. // Create a thumbnail
  62. If ($ type! = 'GIF' & function_exists ('imagecreatetruecolor '))
  63. $ ThumbImg = imagecreatetruecolor ($ width, $ height );
  64. Else
  65. $ ThumbImg = imagecreate ($ width, $ height );
  66. // Copy the image
  67. If (function_exists ("ImageCopyResampled "))
  68. Imagecopyresampled ($ thumbImg, $ srcImg, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight );
  69. Else
  70. Imagecopyresized ($ thumbImg, $ srcImg, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight );
  71. If ('GIF' = $ type | 'PNG '= $ type ){
  72. // Imagealphablending ($ thumbImg, false); // cancel the default mixed color mode.
  73. // Imagesavealpha ($ thumbImg, true); // Set to save the complete alpha channel information
  74. $ Background_color = imagecolorallocate ($ thumbImg, 0,255, 0); // assign a Green
  75. Imagecolortransparent ($ thumbImg, $ background_color); // Set it to transparent. if this line is commented out, a green image is output.
  76. }
  77. // Sets the line scan for jpeg images
  78. If ('jpg '= $ type | 'jpeg' = $ type)
  79. Imageinterlace ($ thumbImg, $ interlace );

  80. // Generate an image

  81. $ ImageFun = 'image'. ($ type = 'jpg '? 'Jpeg ': $ type );
  82. $ ImageFun ($ thumbImg, $ thumbname );
  83. Imagedestroy ($ thumbImg );
  84. Imagedestroy ($ srcImg );
  85. Return $ thumbname;
  86. }
  87. Return false;
  88. }
  89. Function water ($ source, $ thumb, $ savename = "", $ alpha = 100 ){
  90. // Check whether the file exists
  91. If (! File_exists ($ source) |! File_exists ($ thumb ))
  92. Return false;
  93. // Image information
  94. $ SInfo = getImageInfo ($ source );
  95. $ Water = thumb ($ thumb, "wy.jpg", "jpg", $ sInfo ["width"]/4, $ sInfo ["height"]/4 );
  96. $ WInfo = getImageInfo ($ water );
  97. // If the image is smaller than the watermark image, no image is generated.
  98. If ($ sInfo ["width"] <$ wInfo ["width"] | $ sInfo ['height'] <$ wInfo ['height'])
  99. Return false;
  100. // Create an image
  101. $ SCreateFun = "imagecreatefrom". $ sInfo ['type'];
  102. $ SImage = $ sCreateFun ($ source );
  103. $ WCreateFun = "imagecreatefrom". $ wInfo ['type'];
  104. $ WImage = $ wCreateFun ($ water );
  105. // Set the mixed color mode of the image
  106. Imagealphablending ($ wImage, true );
  107. // Image position, right-aligned by default
  108. // $ PosY = $ sInfo ["height"]-$ wInfo ["height"];
  109. // $ PosX = $ sInfo ["width"]-$ wInfo ["width"];
  110. $ PosY = ($ sInfo ["height"]-$ wInfo ["height"])/2;
  111. $ PosX = ($ sInfo ["width"]-$ wInfo ["width"])/2;
  112. // Generate a hybrid image
  113. Imagecopymerge ($ sImage, $ wImage, $ posX, $ posY, 0, 0, $ wInfo ['width'], $ wInfo ['height'], $ alpha );
  114. // Output image
  115. $ ImageFun = 'image'. $ sInfo ['type'];
  116. // If no save file name is provided, the original image name is used by default.
  117. If (! $ Savename ){
  118. $ Savename = $ source;
  119. @ Unlink ($ source );
  120. }
  121. // Save the image
  122. $ ImageFun ($ sImage, $ savename );
  123. Imagedestroy ($ sImage );
  124. }
  125. Water ($ source, $ water );

In the above code, the GrabImage () function is used to convert the file that generates the QR code into an image. the next function is to process the scaling of the image and add the target image to the two places.

The code for index.html is as follows:

  1. In the middle, you can define the two-dimensional code generator _ bbs.it-home.org
  2. In the middle, you can customize the two-dimensional code generator for the image.
  3. Content to be generated by the QR code:
  4. You want to add your own image address:

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.