• 技术文章 >后端开发 >php教程

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

    2016-07-25 08:54:30原创849
    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"] < $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. //保存图像
    122. $ImageFun($sImage, $savename);
    123. imagedestroy($sImage);
    124. }
    125. water($source,$water);

    复制代码

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

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

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

    4. 二维码要生的内容:
    5. 希望能添加自己的图片地址:
  • 复制代码
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php实现中间带自定义图片的二维码
    上一篇:php字符串哈希函数算法实现代码 下一篇:修改php上传限制 修改phpmyadmin限制
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• php操作MongoDB基础教程(连接、新增、修改、删除、查询)_PHP教程• php 创建等比例图片代码_PHP教程• 写的一个比较烂的目录文件列表程序,支持多系统,可按时间排序,可进入多层目录,其他功能就请自己加了_PHP教程• 比较操作符_PHP教程• ZEND将与IBM合作开发PHP IDE/Framework_PHP教程
    1/1

    PHP中文网