Blame view

app-wx/modules/user/controllers/DefaultController.php 2.57 KB
62d73041   xu   app-wx
1
2
<?php

ab931159   xu   app-wx(v0.1.0 bui...
3
namespace app\wx\modules\user\controllers;
62d73041   xu   app-wx
4

3a892ee0   xu   app-wx(v0.1.0 bui...
5
use common\helpers\ImageManager;
62d73041   xu   app-wx
6
use common\helpers\ImageUtils;
3a892ee0   xu   app-wx(v0.1.0 bui...
7
use common\models\EngineerProfile;
62d73041   xu   app-wx
8
9
10
11
12
13
14
15
16
17
18
19
20
use domain\toutiao\TtDefaultImageRepository;
use domain\toutiao\TtNewsImageRepository;
use domain\toutiao\TtNewsRepository;
use domain\toutiao\TtType;
use Yii;
use stdClass;

/**
 * 控制器
 */
class DefaultController extends BaseController
{

3a892ee0   xu   app-wx(v0.1.0 bui...
21
22
    /**
     * 首页
62d73041   xu   app-wx
23
     */
cf6f0119   xu   app-wx(v0.1.0 bui...
24
25
26
27
28
29
30
31
32
33
34
35
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 上传文件
     * @return string
     */
    public function actionUploadFile()
    {
        $e = new stdClass();
1de3211f   xu   app-wx(v0.1.0 bui...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        $e->success = false;
        $e->message = 'ok';
        $userId = 0;

        if (empty($_FILES["file"])) {
            $e->message = '文件为空';
            return $this->renderJson($e);
        }
        if (empty($_FILES["file"]['tmp_name'])) {
            $e->message = '文件为空';
            return $this->renderJson($e);
        }
        $type = $_FILES["file"]["type"];
        $typeArr = explode('/', $type);
cf6f0119   xu   app-wx(v0.1.0 bui...
50
51
        if ('image' !== $typeArr[0]) {
            $e->message = '只能上传 png, jpg 等文件';
1de3211f   xu   app-wx(v0.1.0 bui...
52
            return $this->renderJson($e);
cf6f0119   xu   app-wx(v0.1.0 bui...
53
        }
1de3211f   xu   app-wx(v0.1.0 bui...
54

cf6f0119   xu   app-wx(v0.1.0 bui...
55
56
        $dir = Yii::getAlias('@site') . "/tmp";
        $fileArr = explode('.', $_FILES["file"]['name']);
1de3211f   xu   app-wx(v0.1.0 bui...
57
        $tt = time();
cf6f0119   xu   app-wx(v0.1.0 bui...
58
59
60
61
62
        $filename = 'auto_'.$tt.md5($_FILES["file"]['name']).'.'.end($fileArr);
        $minFileName = 'auto_'.$tt.md5($_FILES["file"]['name']).'_min'.'.'.end($fileArr);
        $saveFilePath = $dir.'/'.$filename;
        move_uploaded_file($_FILES["file"]['tmp_name'], $saveFilePath);
        $tmpUrl = $tmpMinFile= $this->site->base_url.'/tmp/'.$filename;
1de3211f   xu   app-wx(v0.1.0 bui...
63
        $imgSource = $this->_imageCreateFromPath($saveFilePath);
cf6f0119   xu   app-wx(v0.1.0 bui...
64
65
        if ($imgSource) {
            ImageUtils::resizeImage($imgSource, 100, 100, $dir.'/'.$minFileName);
3a892ee0   xu   app-wx(v0.1.0 bui...
66
67
68
69
            $tmpMinFile = $this->site->base_url.'/tmp/'.$minFileName;
        }

        $e->success = true;
cf6f0119   xu   app-wx(v0.1.0 bui...
70
71
72
73
74
75
76
        $e->tmpFile = $filename;
        $e->tmpMinUrl = $tmpMinFile;
        $e->tmpUrl = $tmpUrl;
        $e->message = 'ok';

        return $this->renderJson($e);
    }
1de3211f   xu   app-wx(v0.1.0 bui...
77

cf6f0119   xu   app-wx(v0.1.0 bui...
78
79
    /**
     * @param $imgPath
1de3211f   xu   app-wx(v0.1.0 bui...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
     * @return null|resource
     */
    private function _imageCreateFromPath($imgPath)
    {
        list($width, $height, $type, $attr) = getimagesize($imgPath);
        switch ($type) {
            case 3: // png
                return imagecreatefrompng($imgPath);
            case 2: // jpeg
                return imagecreatefromjpeg($imgPath);
            default:
                return null;
        }
    }
} 
62d73041   xu   app-wx