目录
中间可以自己定义图片的二维码生成器
首页 后端开发 php教程 php实现中间带自定义图片的二维码

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

Jul 25, 2016 am 08:54 AM

  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. 希望能添加自己的图片地址:
  • 复制代码


    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    2 周前 By 尊渡假赌尊渡假赌尊渡假赌
    仓库:如何复兴队友
    4 周前 By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island冒险:如何获得巨型种子
    3 周前 By 尊渡假赌尊渡假赌尊渡假赌

    热工具

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    禅工作室 13.0.1

    禅工作室 13.0.1

    功能强大的PHP集成开发环境

    Dreamweaver CS6

    Dreamweaver CS6

    视觉化网页开发工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神级代码编辑软件(SublimeText3)

    11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

    长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

    Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

    在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

    在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

    Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

    简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

    Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

    这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

    php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

    PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

    在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

    您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

    宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

    2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长

    See all articles