PHP는 중간에 사용자 정의 그림이 있는 QR 코드를 구현합니다.

WBOY
풀어 주다: 2016-07-25 08:54:30
원래의
1408명이 탐색했습니다.
  1. 클래스 QRCode{
  2. 공개 $w;
  3. 공개 $h;
  4. 공개 $s;
  5. 함수 __construct($w1,$h1,$s1){
  6. $this->w = $w1;
  7. $this->h = $h1;
  8. $this->s = $s1;
  9. $this->outimgase();
  10. }
  11. 함수 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 = 배열();
  19. foreach($post_data as $key => $value)
  20. {
  21. $data_Array[] = $key.'='.$value;
  22. }
  23. $data = implode("&",$data_Array);
  24. $ch = 컬_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 = 컬_exec($ch);
  31. curl_close($ch);
  32. $결과 반환;
  33. }
  34. function outimgase(){
  35. echo $this->qrcode();
  36. }
  37. }
  38. header("콘텐츠 유형:이미지/png");
  39. $t = 새로운 QRCode(300,300,"tianxin");
复system代码

2. 그런 다음 PHP 파일을 통해 QR 코드와 대상 이미지를 함께 그립니다.

  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. 읽기 파일($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. $filename을 반환합니다.
  18. }
  19. $source = GrabImage("http://localhost/QRCode/QRCode.php","Myqrcode.png");
  20. $water =GrabImage($surl,"t.png");
  21. 함수 getImageInfo($img){
  22. $imageInfo = getimagesize($img);
  23. if ($imageInfo !== false) {
  24. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
  25. $imageSize = 파일 크기($img);
  26. $info = array(
  27. "width" => $imageInfo[0],
  28. "height" => $imageInfo[1],
  29. "type" => $imageType,
  30. "크기" => $imageSize,
  31. "mime" => $imageInfo['mime']
  32. );
  33. $정보 반환;
  34. } else {
  35. 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['높이'];
  44. $type = 비어 있음($type) ? $info['유형'] : $유형;
  45. $type = strtolower($type);
  46. $인터레이스 = $인터레이스 ? 1:0;
  47. 설정 해제($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. 그 외
  65. $thumbImg = imagecreate($width, $height); 그 외
  66. imagecopyreised($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  67. if ('gif' == $type || 'png' == $type) {
  68. //imagealphablending($thumbImg, false);//取消默认的混color模式
  69. //imagesavealpha( $thumbImg,true);//설정된 알파 통신
  70. $Background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿color
  71. imagecolortransparent($thumbImg, $Background_color); // 设置为透明color,若注释掉该行则输流绿color的图
  72. }
  73. // 对jpeg图shape设置隔行扫描
  74. if ('jpg' == $type || 'jpeg' = = $type)
  75. imageinterlace($thumbImg, $interlace);

  76. // 生成图文

  77. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  78. $imageFun($thumbImg, $thumbname);
  79. imagedestroy($thumbImg);
  80. imagedestroy($srcImg);
  81. $thumbname을 반환합니다.
  82. }
  83. false를 반환합니다.
  84. }
  85. function water($source, $thumb, $savename="", $alpha=100){
  86. //检查文件是否存재
  87. if (!file_exists($source) || !file_exists($thumb))
  88. false를 반환합니다.
  89. //사진 사진
  90. $sInfo = getImageInfo($source);
  91. $water = Thumb($thumb,"wy.jpg","jpg",$sInfo["width"]/4,$sInfo["height"]/4);
  92. $wInfo = getImageInfo($water);
  93. //如果图 Images 소문자, 不生成图文
  94. if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo[' 높이'])
  95. false를 반환합니다.
  96. //建立图이미지
  97. $sCreateFun = "imagecreatefrom" . $sInfo['유형'];
  98. $sImage = $sCreateFun($source);
  99. $wCreateFun = "imagecreatefrom" . $wInfo['유형'];
  100. $wImage = $wCreateFun($water);
  101. //정의된 컬러 이미지
  102. imagealphablending($wImage, true);
  103. //图image位置,默认为右下角右对齐
  104. // $posY = $sInfo["height"] - $wInfo["height"];
  105. // $posX = $sInfo["너비"] - $wInfo["너비"];
  106. $posY = ($sInfo["높이"] - $wInfo["높이"])/2;
  107. $posX = ($sInfo["너비"] - $wInfo["너비"])/2;
  108. //生成混합图이미지
  109. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  110. //输출图이미지
  111. $ImageFun = '이미지' . $sInfo['유형'];
  112. //如果没有给保存文件name ,默认为原图이미지명
  113. if (!$savename) {
  114. $savename = $source;
  115. @unlink($source);
  116. }
  117. //이미지 저장
  118. $ImageFun($sImage, $savename)
  119. imagedestroy($sImage)
  120. }
  121. water($source,$water);
코드 복사

위 코드에서는 3가지 함수를 사용하여 QR코드를 생성하는 파일을 GrabImage() 함수로 변환합니다. 다음 기능은 이미지의 크기 조정을 처리하고 대상 이미지를 두 번째 위치에 추가하는 것입니다.

3. 다음은 다음 코드가 포함된 항목 파일 index.html입니다.

  1. middle_bbs에서 이미지에 대한 고유한 QR 코드 생성기를 정의할 수 있습니다. it-home.org
  2. 🎜>
    제출된 URL에 주의하세요" method="post">
  3. 가운데는 이미지 맞춤설정을 위한 OK QR 코드 생성기입니다.

  4. height="40" align="center" valign="middle">QR 코드 생성 콘텐츠:
  5. td height="40" colspan="2" align="center" valign="middle">
  6. td>
  7. 이미지를 추가하세요 주소:
  8. 코드 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!