Home Backend Development PHP Tutorial PHP implements QR code with custom picture in the middle

PHP implements QR code with custom picture in the middle

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. Then, draw the QR code and your target image together through a php file.

  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. //Save the image
  122. $ImageFun($sImage, $savename);
  123. imagedestroy($sImage);
  124. }
  125. water($source,$water);

Copy code

at In the above code, 3 functions are used. The GrabImage() function is to convert the file that generates the QR code into a picture. The next function is to process the scaling of the picture and add the target picture to the second digit.

3. Here is an entry file index.html with the following code:

  1. Pay attention to the submitted URL" method="post">
  2. A QR code generator where you can define your own pictures in the middle

  3. < ;td width="250" height="40" align="center" valign="middle">
  4. Content to be generated in the QR code:
    Hopefully Add your own image address:
  • Copy code
  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

    The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

    How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

    How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

    How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

    How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

    Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

    Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    See all articles