欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php实现中间带自定义图片的二维码

程序员文章站 2022-04-10 19:51:44
...
  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,然后,通过一个php文件将二维码和你的目的图片画在一起。

  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"] return false;
  99. //建立图像
  100. $sCreateFun = "imagecreatefrom" . $sInfo['type'];
  101. $sImage = $sCreateFun($source);
  102. $wCreateFun = "imagecreatefrom" . $wInfo['type'];
  103. $wImage = $wCreateFun($water);
  104. //设定图像的混色模式
  105. imagealphablending($wImage, true);
  106. //图像位置,默认为右下角右对齐
  107. // $posY = $sInfo["height"] - $wInfo["height"];
  108. // $posX = $sInfo["width"] - $wInfo["width"];
  109. $posY = ($sInfo["height"] - $wInfo["height"])/2;
  110. $posX = ($sInfo["width"] - $wInfo["width"])/2;
  111. //生成混合图像
  112. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  113. //输出图像
  114. $ImageFun = 'Image' . $sInfo['type'];
  115. //如果没有给出保存文件名,默认为原图像名
  116. if (!$savename) {
  117. $savename = $source;
  118. @unlink($source);
  119. }
  120. //保存图像
  121. $ImageFun($sImage, $savename);
  122. imagedestroy($sImage);
  123. }
  124. water($source,$water);
复制代码

在上面的代码中用3个函数 GrabImage()函数是将生成二维码的文件转化成图片 接下来的函数就是处理图片的缩放 将目的图片添加到二位上。

3,在来一个入口文件index.html 代码如下:

  1. 中间可以自己定义图片的二维码生成器_bbs.it-home.org
  2. 注意提交的URL" method="post">
  3. 中间可以自己定义图片的二维码生成器

  4. 二维码要生的内容:
  5. 希望能添加自己的图片地址:
  • 复制代码