php實作中間帶有自訂圖片的二維碼

WBOY
發布: 2016-07-25 08:54:30
原創
1408 人瀏覽過
  1. class QRCode{
  2. public $w;
  3. 公$h;
  4. 公$;
  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. $結果=curl_exec($ch);
  31. curl_close($ch);
  32. 回傳$結果;
  33. }
  34. function outimgase(){
  35. echo $this->qrcode();
  36. }
  37. }
  38. header("內容型態: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. 回傳$檔名;
  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. "寬度" => $imageInfo[0],
  28. "高度" => $imageInfo[1],
  29. "類型" => $imageType,
  30. "大小" => $imageSize,
  31. "mime" => $imageInfo['mime']
  32. );
  33. 回傳$訊息;
  34. } else {
  35. 回傳 false;
  36. }
  37. }
  38. functionthumb($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 = 空($type) ? $info['type'] : $type;
  45. $type = strtolower($type);
  46. $交錯 = $交錯?
  47. $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例
  48. if ($scale >= 1) {
  49. //超過原圖大小不再縮放略
  50. $width = $srcWidth;
  51. $height = $srcHeight;
  52. } else {
  53. //最小尺寸
  54. $width = (int) ($srcWidth * $scale);
  55. $height = (int) ($srcHeight * $scale);
  56. }
  57. //載入原圖
  58. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  59. $srcImg = $createFun($image);
  60. //建立夢想
  61. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  62. $thumbImg = imagecreatetruecolor($width, $height);
  63. 其他
  64. $thumbImg = imagecreate($width, $height);
  65. // 複製圖片
  66. if (function_exists("ImageCopyResampled"))
  67. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $widumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcImg. ;
  68. else
  69. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  70. if ('gif' == $type || 'png' == $type) {
  71. //imagealphablending($thumbImg, false);//取消預設的混色模式
  72. //imagesavealpha( $thumbImg,true);//設定保存完整的alpha通道資訊
  73. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一個綠色
  74. imagecolortransparent($thumbImg, $background_color); // 設定為透明顏色,若註解掉該行則輸出綠色的圖形
  75. }
  76. //對jpeg圖形設定隔行掃描
  77. if ('jpg' == $type || 'jpeg' = = $type)
  78. imageinterlace($thumbImg, $interlace);
  79. //產生圖片

  80. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  81. $imageFun($thumbImg, $thumbname);
  82. imagedestroy($thumbImg);
  83. imagedestroy($srcImg);
  84. 回傳$thumbname;
  85. }
  86. 回傳 false;
  87. }
  88. 功能水($source, $thumb, $savename="", $alpha=100){
  89. //檢查檔案是否存在
  90. if (!file_exists($source) || !file_exists($thumb))
  91. 回傳false;
  92. //圖片資訊
  93. $sInfo = getImageInfo($source);
  94. $水 = 拇指($thumb,"wy.jpg","jpg",$sInfo["寬度"]/4,$sInfo["高度"]/4);
  95. $wInfo = getImageInfo($water);
  96. //如果圖片小於浮水印圖片,不產生圖片
  97. if ($sInfo["width"] 返回false;
  98. //建立映像
  99. $sCreateFun = "imagecreatefrom" . $sInfo['類型'];
  100. $sImage = $sCreateFun($source);
  101. $wCreateFun = "imagecreatefrom" 。
  102. $wImage = $wCreateFun($water);
  103. //設定影像的混色模式
  104. imagealphablending($wImage, true);
  105. //影像位置,預設為右下角右對齊
  106. // $posY = $sInfo["height"] - $wInfo["height"];
  107. // $posX = $sInfo["width"] - $wInfo["width"];
  108. $posY = ($sInfo["height"] - $wInfo["height"])/2;
  109. $posX = ($sInfo["width"] - $wInfo["width"])/2;
  110. //產生混合影像
  111. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  112. //輸出影像
  113. $ImageFun = 'Image' . $sInfo['類型'];
  114. //如果沒有給予儲存檔名,預設為原始影像名稱
  115. if (!$savename) {
  116. $savename = $source;
  117. @unlink($source);
  118. }
  119. //儲存影像
  120. $ImageFun($sImage, $savename);
  121. imagedestroy($sImage);
  122. }
  123. water($source,$water);
複製程式碼

在上面的程式碼中用3個函數GrabImage()函數是將產生二維碼的檔案轉換成圖片接下來的函數就是處理圖片的縮放將目的圖片添加到二位。

3,在來一個入口文件index.html 程式碼如下:

  1. 中間可以自己定義圖片的二維碼產生器_bbs.it-home .org
  2. 注意提交的URL" method="post ">
  3. 中間可以自己定義圖片的二維碼產生器

  4. 二維碼要生的內容:
  5. 希望能加入自己的圖片位址:
  • body>
  • 複製程式碼


    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!