PHP implements a two-dimensional code with custom images in the middle

Source: Internet
Author: User
Tags transparent color
  1. Class qrcode{
  2. public $w;
  3. Public $h;
  4. Public $s;
  5. function __construct ($w 1, $h 1, $s 1) {
  6. $this->w = $w 1;
  7. $this->h = $h 1;
  8. $this->s = $s 1;
  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[' CHL ') = $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");
Copy Code

2, and then, through a PHP file will be the QR code and your purpose picture to draw together.

  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. $FP 2= @fopen ($filename, "a");
  15. Fwrite ($fp 2, $img);
  16. Fclose ($fp 2);
  17. return $filename;
  18. }
  19. $source = Grabimage ("http://localhost/QRCode/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. Get the original 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); Calculating the zoom ratio
  49. if ($scale >= 1) {
  50. More than the original size is no longer abbreviated
  51. $width = $srcWidth;
  52. $height = $srcHeight;
  53. } else {
  54. Thumbnail size
  55. $width = (int) ($srcWidth * $scale);
  56. $height = (int) ($srcHeight * $scale);
  57. }
  58. Load in original
  59. $createFun = ' Imagecreatefrom '. ($type = = ' jpg '? ' JPEG ': $type);
  60. $SRCIMG = $createFun ($image);
  61. Create a thumbnail image
  62. if ($type! = ' gif ' && function_exists (' Imagecreatetruecolor '))
  63. $THUMBIMG = Imagecreatetruecolor ($width, $height);
  64. Else
  65. $THUMBIMG = Imagecreate ($width, $height);
  66. Copy Picture
  67. if (function_exists ("imagecopyresampled"))
  68. Imagecopyresampled ($THUMBIMG, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  69. Else
  70. Imagecopyresized ($THUMBIMG, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  71. if (' gif ' = = $type | | ' png ' = = $type) {
  72. Imagealphablending ($THUMBIMG, false);//Cancel the default process color mode
  73. Imagesavealpha ($THUMBIMG, true);//set to save full alpha channel information
  74. $background _color = imagecolorallocate ($thumbImg, 0, 255, 0); Assign a Green
  75. Imagecolortransparent ($THUMBIMG, $background _color); Set to Transparent color, if the line is commented out the output of the green graph
  76. }
  77. Set up interlaced scanning for JPEG graphics
  78. if (' jpg ' = = $type | | ' jpeg ' = = $type)
  79. Imageinterlace ($THUMBIMG, $interlace);

  80. Create a picture

  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 if the file exists
  91. if (!file_exists ($source) | |!file_exists ($THUMB))
  92. return false;
  93. Picture 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 picture is smaller than the watermark picture, the picture is not 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. To set the color blending mode of an image
  106. Imagealphablending ($wImage, true);
  107. Image position, default to right bottom right corner
  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. Generating mixed images
  113. Imagecopymerge ($sImage, $wImage, $posX, $posY, 0, 0, $wInfo [' width '], $wInfo [' height '], $alpha);
  114. Output image
  115. $ImageFun = ' Image '. $sInfo [' type '];
  116. If the save file name is not given, the original image name is the default
  117. if (! $savename) {
  118. $savename = $source;
  119. @unlink ($source);
  120. }
  121. Save Image
  122. $ImageFun ($sImage, $savename);
  123. Imagedestroy ($sImage);
  124. }
  125. Water ($source, $water);

Copy Code

In the code above, 3 functions Grabimage () function is to convert a file that generates a QR code into a picture the next function is to manipulate the image's zoom to add the destination image to the two-bit.

3, in a portal file index.html code is as follows:

  1. A two-dimensional code generator that can define its own image in the middle _bbs.it-home.org
  2. Note the URL of the submission"method=" POST ">
  3. A two-dimensional code generator that can define a picture in the middle

  4. Two-dimensional code to be raw content:
  5. Want to add your own image address:
Copy Code
  • Related Article

    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.