Home  >  Article  >  Backend Development  >  PHP implements QR code with custom picture in the middle

PHP implements QR code with custom picture in the middle

WBOY
WBOYOriginal
2016-07-25 08:54:301325browse
  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['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");
复制代码

2. Then, draw the QR code and your target image together 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, "a");
  15. fwrite($fp2,$img);
  16. fclose($fp2);
  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. // 获取原图信息
  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); // 计算缩放比例
  49. if ($scale >= 1) {
  50. // 超过原图大小不再缩略
  51. $width = $srcWidth;
  52. $height = $srcHeight;
  53. } else {
  54. // 缩略图尺寸
  55. $width = (int) ($srcWidth * $scale);
  56. $height = (int) ($srcHeight * $scale);
  57. }
  58. // 载入原图
  59. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  60. $srcImg = $createFun($image);
  61. //创建缩略图
  62. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  63. $thumbImg = imagecreatetruecolor($width, $height);
  64. else
  65. $thumbImg = imagecreate($width, $height);
  66. // 复制图片
  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);//取消默认的混色模式
  73. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  74. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
  75. imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  76. }
  77. // 对jpeg图形设置隔行扫描
  78. if ('jpg' == $type || 'jpeg' == $type)
  79. imageinterlace($thumbImg, $interlace);

  80. // 生成图片

  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. //检查文件是否存在
  91. if (!file_exists($source) || !file_exists($thumb))
  92. return false;
  93. //图片信息
  94. $sInfo = getImageInfo($source);
  95. $water = thumb($thumb,"wy.jpg","jpg",$sInfo["width"]/4,$sInfo["height"]/4);
  96. $wInfo = getImageInfo($water);
  97. //如果图片小于水印图片,不生成图片
  98. if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
  99. return false;
  100. //建立图像
  101. $sCreateFun = "imagecreatefrom" . $sInfo['type'];
  102. $sImage = $sCreateFun($source);
  103. $wCreateFun = "imagecreatefrom" . $wInfo['type'];
  104. $wImage = $wCreateFun($water);
  105. //设定图像的混色模式
  106. imagealphablending($wImage, true);
  107. //图像位置,默认为右下角右对齐
  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. //生成混合图像
  113. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  114. //输出图像
  115. $ImageFun = 'Image' . $sInfo['type'];
  116. //如果没有给出保存文件名,默认为原图像名
  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);

Copy code

at In the above code, 3 functions are used. The GrabImage() function is to convert the file that generates the QR code into a picture. The next function is to process the scaling of the picture and add the target picture to the second digit.

3. Here is an entry file index.html with the following code:

  1. Pay attention to the submitted URL" method="post">
  2. A QR code generator where you can define your own pictures in the middle

  3. < ;td width="250" height="40" align="center" valign="middle">
  4. Content to be generated in the QR code:
    Hopefully Add your own image address:
  • Copy code
  • Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn