ホームページ  >  記事  >  バックエンド開発  >  PHPは中央にカスタム画像を含むQRコードを実装します

PHPは中央にカスタム画像を含むQRコードを実装します

WBOY
WBOYオリジナル
2016-07-25 08:54:301395ブラウズ
  1. class QRCode{
  2. public $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 = 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. $result を返す;
  33. }
  34. function outimgase(){
  35. echo $this->qrcode();
  36. }
  37. }
  38. header("Content-type:image/png");
  39. $t = 新しい QRCode(300,300,"tianxin");
复制代

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. readfile($url);
  11. $img = ob_get_contents();
  12. ob_end_clean();
  13. $size = strlen($img);
  14. $fp2=@fopen($ファイル名, "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 = strto lower(substr(image_type_to_extension($imageInfo[2]), 1));
  25. $imageSize = ファイルサイズ($img);
  26. $info = array(
  27. "幅" => $imageInfo[0],
  28. "高さ" => $imageInfo[1],
  29. "タイプ" => $imageType,
  30. "サイズ" => $ imageSize,
  31. "mime" => $imageInfo['mime']
  32. );
  33. $info を返します;
  34. } else {
  35. false を返す;
  36. }
  37. }
  38. function subject($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  39. // 获取原图情報
  40. $info = getImageInfo($画像);
  41. if ($info !== false) {
  42. $srcWidth = $info['width'];
  43. $srcHeight = $info['高さ'];
  44. $type = 空($type) ? $info['type'] : $type;
  45. $type = strto lower($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. 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. imagecopyresize($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  71. if ('gif' == $type || 'png' == $type) {
  72. //imagealphablending($thumbImg, false);//取消默认の混色モード
  73. //imagesavealpha($thumbImg,true); //完全に保存されるアルファ パス情報を設定します
  74. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
  75. imagecolortransparent($thumbImg, $background_color); // 設定は透明色、若注释掉この行に表示される色の画像
  76. }
  77. }
  78. // jpeg 图形設定を隔行扫描
  79. if ('jpg' == $type || 'jpeg' == $type)
  80. imageinterlace ($thumbImg, $interlace);

  81. // 画像を生成します

  82. $imageFun = 'image' 。 ($type == 'jpg' ? 'jpeg' : $type);
  83. $imageFun($thumbImg, $thumbname);
  84. imagedestroy($thumbImg);
  85. imagedestroy($srcImg);
  86. $thumbname を返します;
  87. }
  88. false を返します。
  89. }
  90. function Water($source, $thumb, $savename="", $alpha=100){
  91. //检查文件の有無
  92. if (!file_exists($source) || !file_exists($thumb))
  93. false を返します。
  94. //画像情報
  95. $sInfo = getImageInfo($source);
  96. $water =親指($thumb,"wy.jpg","jpg",$sInfo["幅"]/4,$sInfo["高さ"]/4);
  97. $wInfo = getImageInfo($water);
  98. //場合は写真が水印の写真より小さく、写真は生成されません
  99. if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
  100. false を返します。
  101. //建立画像
  102. $sCreateFun = "imagecreatefrom" . $sInfo['タイプ'];
  103. $sImage = $sCreateFun($source);
  104. $wCreateFun = "imagecreatefrom" . $wInfo['タイプ'];
  105. $wImage = $wCreateFun($water);
  106. //画像の混色モードを設定します
  107. imagealphablending($wImage, true);
  108. //画像の位置、右下角右对齐
  109. // $posY = $sInfo["height"] - $wInfo["height"];
  110. // $posX = $sInfo["width"] - $wInfo["width"];
  111. $posY = ($sInfo["高さ"] - $wInfo["高さ"])/2;
  112. $posX = ($sInfo["幅"] - $wInfo["幅"])/2;
  113. //混合画像生成
  114. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  115. //出画像
  116. $ImageFun = '画像' . $sInfo['タイプ'];
  117. //如果没有给出保存文件名,默认是原画像名
  118. if (!$savename) {
  119. $savename = $source;
  120. @リンク解除($source); 🎜}
  121. //画像を保存します
  122. $ImageFun($sImage, $savename);
  123. imagedestroy($sImage)
  124. }
  125. water($source,$water);

コードをコピーします上記のコードでは、3 つの関数が使用されています。GrabImage() 関数は、QR コードを生成するファイルを画像に変換し、次の関数は画像のスケーリングを処理し、ターゲットの画像を 2 桁目に追加します。 。
3. 以下のコードを含むエントリ ファイルindex.html を示します。

送信された URL メソッドに注意してください="投稿">
  1. 中央に独自の画像を定義できる QR コード ジェネレーター
  2. td height="40" colspan="2" align ="center" valign="middle">
  3. 声明:
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
  4. QR コードで生成されます:
  5. できれば独自の画像アドレスを追加してください: