| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace common\services;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- use common\components\configDict;
- use common\models\xhActiveOrder;
- use linslin\yii2\curl;
- /**
- * 图片相关接口
- * @package common\services
- * @author shish <shish@zhhinc.com>
- * @time 2019.5.11
- */
- class ImageService
- {
-
- /**
- * 生成头像
- */
- public static function generateAvatar($merchantId, $userId, $url)
- {
- $directory = configDict::getConfig('imgSavePath', 'wxUserAvatar');
- //跑脚本也要生成头像,通过webroot拿不到路径
- $root = util::getRootDir();
- $path = $root . 'resource/images';
- $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($path . $pre)) {
- mkdir($path . $pre, 0777, true);
- }
- $preName = stringUtil::generateAvatarFileName($merchantId, $userId);
- $name = $preName . '.jpg';
- $fullFileName = $path . $pre . $name;
- $fileName = $pre . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- //文件大小
- Yii::info('path:'.$fullFileName);
- $fp2 = @fopen($fullFileName, 'a');
- fwrite($fp2, $result);
- fclose($fp2);
-
- //缩略宽 200px 中
- $dstImg200 = $path . $pre . $preName . '_200.jpg';
- util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
-
- //缩略成宽50px 小
- $dstImg50 = $path . $pre . $preName . '_50.jpg';
- util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
- //默认大小 大
- return $fileName;
- }
-
- }
|