Api.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Qrcode\Src;
  3. use Dever;
  4. class Api
  5. {
  6. private $path = 'upload/qrcode';
  7. public function __construct()
  8. {
  9. //$this->config = Dever::config('base', 'project')->qrcode;
  10. $this->config = Dever::config('base', 'qrcode')->qrcode;
  11. Dever::apply('lib/phpqrcode');
  12. }
  13. public function qrcode($string, $local = false, $logo = false, $uid = false)
  14. {
  15. if ($local) {
  16. $this->config['local'] = true;
  17. }
  18. if ($this->config['local']) {
  19. $result = $this->create($string, $local, $logo, $uid);
  20. } else {
  21. $result = $this->create($string);
  22. }
  23. return $result;
  24. }
  25. private function create($value, $local = false, $logo = false, $uid = false)
  26. {
  27. $file = false;
  28. if ($local) {
  29. if ($uid) {
  30. $path = Dever::pathAvatar($this->path, $uid);
  31. } else {
  32. $path = Dever::pathDay($this->path);
  33. }
  34. $file = $path . $local . '.png';
  35. }
  36. if (!is_file($file)) {
  37. $QR = \QRcode::png($value, $file, 'L', $this->config['size'], 2);
  38. if ($logo) {
  39. if (strstr($file, 'http')) {
  40. $file_string = Dever::curl($file);
  41. } else {
  42. $file_string = file_get_contents($file);
  43. }
  44. if (Dever::project('upload')) {
  45. $logo_string = Dever::load('upload/lib/file')->content($logo);
  46. } else {
  47. $logo_string = file_get_contents($logo);
  48. }
  49. $QR = imagecreatefromstring($file_string);
  50. $logo = imagecreatefromstring($logo_string);
  51. $QR_width = imagesx($QR);//二维码图片宽度
  52. $QR_height = imagesy($QR);//二维码图片高度
  53. $logo_width = imagesx($logo);//logo图片宽度
  54. $logo_height = imagesy($logo);//logo图片高度
  55. $logo_qr_width = $QR_width / 5;
  56. $scale = $logo_width/$logo_qr_width;
  57. $logo_qr_height = $logo_height/$scale;
  58. $from_width = ($QR_width - $logo_qr_width) / 2;//重新组合图片并调整大小
  59. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);//输出图片
  60. imagepng($QR, $file);
  61. }
  62. }
  63. $url = str_replace(Dever::data() . 'upload/', Dever::config('host')->uploadRes, $file);
  64. $result = array
  65. (
  66. 'local' => $file,
  67. 'url' => $url,
  68. );
  69. return $result;
  70. }
  71. }