| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace common\services;
- use common\components\dirUtil;
- use common\components\stringUtil;
- use linslin\yii2\curl;
- //图片相关接口
- class ImageService
- {
-
- //生成头像
- public static function generateAvatar($merchantId, $url)
- {
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::imgUploadDir();
- $middle = "/weixin/avatar/{$merchantId}/{$month}/{$date}/";
- if (!file_exists($path . $middle)) {
- mkdir($path . $middle, 0777, true);
- }
- $middleName = stringUtil::uniqueFileName();
- $name = $middleName . '.jpg';
- $fullFileName = $path . $middle . $name;
- $fileName = $middle . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
-
- $fp = @fopen($fullFileName, 'a');
- fwrite($fp, $result);
- fclose($fp);
-
- $imgInfo = getimagesize($fullFileName);
- $width = $imgInfo[0];
- $height = $imgInfo[1];
-
- //生成小图 200px
- $smallWidth = 200;
- $smallHeight = ceil((200 * $height) / $width);
- if ($width <= 200) {
- $smallWidth = $width;
- $smallHeight = $height;
- }
- $smallFullPathFile = $path . $middleName . '_small.' . '.jpg';
- Image::thumbnail($fullFileName, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
-
- return $fileName;
- }
-
- }
|