ImageService.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace common\services;
  3. use common\components\stringUtil;
  4. use common\components\util;
  5. use Yii;
  6. use common\components\configDict;
  7. use common\models\xhActiveOrder;
  8. use linslin\yii2\curl;
  9. /**
  10. * 图片相关接口
  11. * @package common\services
  12. * @author shish <shish@zhhinc.com>
  13. * @time 2019.5.11
  14. */
  15. class ImageService
  16. {
  17. /**
  18. * 生成头像
  19. */
  20. public static function generateAvatar($merchantId, $userId, $url)
  21. {
  22. $directory = configDict::getConfig('imgSavePath', 'wxUserAvatar');
  23. //跑脚本也要生成头像,通过webroot拿不到路径
  24. $root = util::getRootDir();
  25. $path = $root . 'resource/images';
  26. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  27. if (!file_exists($path . $pre)) {
  28. mkdir($path . $pre, 0777, true);
  29. }
  30. $preName = stringUtil::generateAvatarFileName($merchantId, $userId);
  31. $name = $preName . '.jpg';
  32. $fullFileName = $path . $pre . $name;
  33. $fileName = $pre . $name;
  34. $curl = new curl\Curl();
  35. $result = $curl->get($url);
  36. //文件大小
  37. Yii::info('path:'.$fullFileName);
  38. $fp2 = @fopen($fullFileName, 'a');
  39. fwrite($fp2, $result);
  40. fclose($fp2);
  41. //缩略宽 200px 中
  42. $dstImg200 = $path . $pre . $preName . '_200.jpg';
  43. util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
  44. //缩略成宽50px 小
  45. $dstImg50 = $path . $pre . $preName . '_50.jpg';
  46. util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
  47. //默认大小 大
  48. return $fileName;
  49. }
  50. }