WxBaseClass.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace bizHd\wx\classes;
  3. use common\components\dirUtil;
  4. use common\components\stringUtil;
  5. use Yii;
  6. use bizHd\base\classes\BaseClass;
  7. use yii\imagine\Image;
  8. use linslin\yii2\curl;
  9. class WxBaseClass extends BaseClass
  10. {
  11. public static $baseFile = '\bizHd\wx\models\WxBase';
  12. //保存Logo ssh 2020.3.11
  13. public static function saveLogo($url, $sjId)
  14. {
  15. $path = dirUtil::getImgDir();
  16. $middle = "wx/merchant/logo/";
  17. if (!file_exists($path . $middle)) {
  18. mkdir($path . $middle, 0777, true);
  19. }
  20. if (empty($sjId)) {
  21. $sjId = stringUtil::shortUniqueId($sjId);
  22. }
  23. $name = $sjId . '.jpg';
  24. $fullFileName = $path . $middle . $name;
  25. $fileName = $middle . $name;
  26. if (file_exists($fullFileName)) {
  27. return $fileName;
  28. }
  29. $curl = new curl\Curl();
  30. $result = $curl->get($url);
  31. $fp = @fopen($fullFileName, 'a');
  32. fwrite($fp, $result);
  33. fclose($fp);
  34. $imgInfo = getimagesize($fullFileName);
  35. if (empty($imgInfo) || isset($imgInfo[0]) == false) {
  36. return '';
  37. }
  38. $width = $imgInfo[0];
  39. $height = $imgInfo[1];
  40. //生成小图 280px
  41. $smallWidth = 280;
  42. $smallHeight = ceil((280 * $height) / $width);
  43. if ($width <= 280) {
  44. $smallWidth = $width;
  45. $smallHeight = $height;
  46. }
  47. $smallFullPathFile = $path . $middle . $sjId . '_small.jpg';
  48. Image::thumbnail($fullFileName, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  49. return $fileName;
  50. }
  51. //保存公众号二维码 ssh 2020.3.11
  52. public static function saveQrCode($url, $sjId)
  53. {
  54. $path = dirUtil::getImgDir();
  55. $middle = "wx/merchant/qrCode/";
  56. if (!file_exists($path . $middle)) {
  57. mkdir($path . $middle, 0777, true);
  58. }
  59. if (empty($sjId)) {
  60. $sjId = stringUtil::shortUniqueId($sjId);
  61. }
  62. $name = $sjId . '.jpg';
  63. $fullFileName = $path . $middle . $name;
  64. $fileName = $middle . $name;
  65. if (file_exists($fullFileName)) {
  66. return $fileName;
  67. }
  68. $curl = new curl\Curl();
  69. $result = $curl->get($url);
  70. $fp = @fopen($fullFileName, 'a');
  71. fwrite($fp, $result);
  72. fclose($fp);
  73. $imgInfo = getimagesize($fullFileName);
  74. if (empty($imgInfo) || isset($imgInfo[0]) == false) {
  75. return '';
  76. }
  77. $width = $imgInfo[0];
  78. $height = $imgInfo[1];
  79. //生成小图 280px
  80. $smallWidth = 280;
  81. $smallHeight = ceil((280 * $height) / $width);
  82. if ($width <= 280) {
  83. $smallWidth = $width;
  84. $smallHeight = $height;
  85. }
  86. $smallFullPathFile = $path . $middle . $sjId . '_small.jpg';
  87. Image::thumbnail($fullFileName, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  88. return $fileName;
  89. }
  90. //取公众号信息 0表示平台的公众号 ssh 2020.4.26
  91. public static function getWxBase($id = 0)
  92. {
  93. return self::getByCondition(['sjId' => 0]);
  94. }
  95. }