Maison > php教程 > php手册 > le corps du texte

Laravel中的日志与上传,

WBOY
Libérer: 2016-06-13 08:49:25
original
875 Les gens l'ont consulté

Laravel中的日志与上传,

  PHP中的框架众多,我自己就接触了好几个。大学那会啥也不懂啥也不会,拿了一个ThinkPHP学了。也许有好多人吐槽TP,但是个人感觉不能说哪个框架好,哪个框架不好,再不好的框架你能把源码读上一遍,框架的设计思想理解了也能学到好多东西。况且有好多东西自己还不理解,所以认真学习一个框架这还是可以学不少东西的。

  还是先说说Laravel吧,现在已经到5.2了。就我自己来说之前没有接触过laravel,但是学习过laravel之后感觉这个框架确实不错,并且老外用的不亦乐乎。他的开发社区还可以,文档比较齐全,但是官网文档不咋地,从上面读不出多少东西(自己感觉),好多东西还得阅读源码,对于我这种英语不好的人还更喜欢中文文档(以后得改)。Laravel是使用Composer(https://getcomposer.org)来管理依赖,确实比较方便,但是因为镜像被墙的原因在访问或者更新的时候比较慢(几乎失败),这里有解决办法:http://pkg.phpcomposer.com/#tip1。

  最近因为工作需要项目要重构(重构原因不用多说,大家懂得),需要迁移到新的框架上。Laravel是一个不错的框架,强大的路由,便捷的配置,高可用的模块依赖,确实为开发省了不少力气。考虑到我们这个项目主要是接口部分,对性能有一定的要求(但是不是苛刻),并且路由不能改,要兼容老的逻辑,所以Laravel是首选,但是有一个问题就是我们是写接口,那么要依赖的东西就少很多,比如view层几乎用不到,还有就是测试模块,上传模块(有图床),本地化模块文件系统等也用不到,所以使用Laravel还是比较浪费的,说白了他比较重。所以我们就考虑了基于laravel的一个框架Lumen,相比Laravel这个全栈框架而言Lumen精简了不少,并且Lumen是面向Api的,所以最后就选择Lumen了。

  laravel也不是全能的有优点也有缺点,比如他的依赖太多,可以看一下安装好的laravel框架默认的依赖源包就有30M左右,确实有点大。直到今天在使用过程中发现Lumen也是有点力不从心,随着业务逻辑越来越复杂,访问速度各方面也下来了,我们有时候考虑 slim 等更轻量级的,其实吧新浪这边不少人是鸟哥粉,不少人推崇yaf,yaf确实牛逼,实践证明快的不止一点两点,估计以后还得迁移到yaf。这段时间PHP7不是出了吗,但是测试结果表明Bug不少,把接口迁移到7上应该有不少的性能提升,据说是提高100%,还没敢尝试,等稳定了再说吧!

  说了不少废话,下面我就介绍一下Laravel中的日志与上传,慢慢来,这篇文章先写一部分,因为Laravel东西比较多,其他的我会慢慢写出来。我就说说自己在使用过程中遇到的问题,遇到的坑,帮助大家学习。

一、日志

1、说明:

日志重要性不言而喻,我们这边的日志是人工推荐,兴趣爱好,推荐位的依赖。做推荐的同学比写接口的还要多,日志出了问题,推荐就会不准确甚至无法推荐,可见日志的重要性。

Laravel框架初始化好了以后错误和异常处理已经默认配置好了,他的日志是基于一款很好用的日志管理工具Monolog,

  首先说一下Monolog,是php下比较全又容易扩展的记录日志组件。其中Symfony 、 CakePHP等知名php框架都内置了Monolog,有兴趣的可以看一下。每个Logger实例都有一个通道和日志处理器栈。每当你添加一条日志记录,它会被发送到日志处理器栈。 你可以创建很多Logger每个Logger定义一个通道(db,请求,路由),每个Logger有很多日志处理器。这些通道会过滤日志。每个日志处理器都有一个Formatter(内置的日志显示格式处理器)。你还可以设定日志级别。(官网解释)

  日志配置:Laravel目前支持四种日志处理器,

<span>1</span> <span>S</span><span><span>ingle</span>(将日志记录到单个文件中。该日志处理器对应,对应StreamHandler),
</span><span>2</span> 
<span>3</span> <span>D</span><span><span>aily</span> (以日期为单位将进行日志记录对应RotatingFileHandler)
</span><span>4</span> 
<span>5</span> <span>S</span><span>yslog</span><span>(将日志记录到Syslog中。对应SysLogHandler)
</span><span>6</span> 
<span>7</span> <span>Errorlog</span>(将日志记录到PHP的error_log中。对应ErrorLogHandler)
Copier après la connexion

明白了日志的处理方式我们就可以设置自己需要的方式,在 config/app.php中的对应项设置(默认的):

<span>1</span> 'log' => 'single',
Copier après la connexion

2、使用Log记录日志

Laravel提供了Log方法记录日志,Log实际上使用的 Illuminate\Log\Writer,应为在其中 Writer 的构造函数中注入了Monolog\Logger。生成的日志文件存放在storage/logs目录下。

如下:

<span>1</span> <span>Log</span>::emergency(<span>$error</span>); <span>//</span><span>紧急,如系统挂掉</span>
<span>2</span> <span>Log</span>::alert(<span>$error</span>);     <span>//</span><span>需要立即采取行动,如数据库异常等</span>
<span>3</span> <span>Log</span>::critical(<span>$error</span>);  <span>//</span><span>严重问题,如异常</span>
<span>4</span> <span>Log</span>::error(<span>$error</span>);     <span>//</span><span>运行时错误,不需要立即处理但需要被记录和监控</span>
<span>5</span> <span>Log</span>::warning(<span>$error</span>);   <span>//</span><span>警告但不是错误,比如使用了被废弃的API</span>
<span>6</span> <span>Log</span>::notice(<span>$error</span>);    <span>//</span><span>普通但值得注意的事件</span>
<span>7</span> <span>Log</span>::info(<span>$error</span>);      <span>//</span><span>感兴趣的事件,比如登录、退出</span>
<span>8</span> <span>Log</span>::debug(<span>$error</span>);     <span>//</span><span>详细的调试信息</span>
Copier après la connexion

3、按照自己的需求记录日志

Laravel中如果按照原来的配置貌似不能按照自己的需求记录日志,我就按照自己的需求写了一个,供大家参考,当然你可以跳过他提供的日志处理方法Log,在容器中把 Monolog对象写入容器,可以写成单例的形式,这样在加载的时候只实例化一次,然后按照monolog来配置自己想要的记录日志的方法。

<span> 1</span> <span>class</span><span> Save_log
</span><span> 2</span> <span>{
</span><span> 3</span>     <span>//</span><span>存放每个级别实例</span>
<span> 4</span>     <span>private</span> <span>static</span> <span>$obj_log</span> =<span> [];
</span><span> 5</span> 
<span> 6</span>     <span>//</span><span>日志类型映射</span>
<span> 7</span>     <span>private</span> <span>static</span> <span>$classify_arr</span> = ['default', 'debug_log','error_log'<span>];
</span><span> 8</span> 
<span> 9</span>     <span>/*</span><span>*
</span><span>10</span> <span>     * 单利初始化以及调取对象
</span><span>11</span> <span>     * @param $classify 日志的的频道,对应不同的目录
</span><span>12</span> <span>     * @param $max_num  日志记录的最大数量
</span><span>13</span>      <span>*/</span>
<span>14</span>     <span>public</span> <span>static</span> <span>function</span> get_log_instance(<span>$classify</span> = 'default', <span>$max_num</span> = 0<span>)
</span><span>15</span> <span>    {
</span><span>16</span>         <span>if</span>(<span>empty</span>(self::<span>$obj_log</span>[<span>$classify</span><span>])) {
</span><span>17</span>             self::<span>$obj_log</span>[<span>$classify</span>] = <span>new</span> Writer(<span>new</span> Logger(<span>$classify</span><span>));
</span><span>18</span>             self::<span>$obj_log</span>[<span>$classify</span>]->useDailyFiles(self::get_path(<span>$classify</span>), <span>$max_num</span><span>);
</span><span>19</span> <span>        }
</span><span>20</span>         <span>return</span> self::<span>$obj_log</span>[<span>$classify</span><span>];
</span><span>21</span> <span>    }
</span><span>22</span> 
<span>23</span>     <span>/*</span><span>*
</span><span>24</span> <span>     * 映射对应的目录
</span><span>25</span> <span>     * @param $classify 日志的不同的频道
</span><span>26</span>      <span>*/</span>
<span>27</span>     <span>private</span> <span>static</span> <span>function</span> get_path(<span>$classify</span><span>)
</span><span>28</span> <span>    {
</span><span>29</span>         <span>$root_path</span> =<span> public_path();
</span><span>30</span>         <span>$path</span> = <span>$root_path</span> . '/../../logs/'<span>; //可以是自己的任意路径
</span><span>31</span>         <span>$log_arr</span> = self::<span>$classify_arr</span><span>;
</span><span>32</span>         <span>if</span>(!<span>empty</span>(<span>$log_arr</span>) && !<span>empty</span>(<span>$classify</span><span>)) {
</span><span>33</span>             <span>if</span>(<span>in_array</span>(<span>$classify</span>, <span>$log_arr</span><span>)) {
</span><span>34</span>                 <span>return</span> <span>$path</span> . <span>$classify</span>. '/' . <span>$classify</span> . '.log'<span>;
</span><span>35</span> <span>            }
</span><span>36</span> <span>        }
</span><span>37</span>         <span>return</span> <span>$path</span> . 'default/default.log'<span>;
</span><span>38</span> <span>    }
</span><span>39</span> 
<span>40</span>     <span>/*</span><span>*
</span><span>41</span> <span>     * 映射对应的目录
</span><span>42</span> <span>     * @param $func 调用的方法
</span><span>43</span> <span>     * @param $arguments 参数,包括数据和日志等级
</span><span>44</span>      <span>*/</span>
<span>45</span>     <span>public</span> <span>static</span> <span>function</span> __callStatic(<span>$func</span>, <span>$arguments</span><span>)
</span><span>46</span> <span>    {
</span><span>47</span>         <span>$get_obj</span> = self::get_log_instance(<span>$func</span><span>);
</span><span>48</span>         <span>if</span>(<span>empty</span>(<span>$get_obj</span><span>)) {
</span><span>49</span>             <span>log</span>::error('Save Log Error!'<span>);
</span><span>50</span> <span>        }
</span><span>51</span>         <span>if</span>(<span>empty</span>(<span>$arguments</span>) || !<span>is_array</span>(<span>$arguments</span>) || !<span>isset</span>(<span>$arguments</span>[0<span>])) {
</span><span>52</span>             <span>$get_obj</span>->info('No Data Save!'<span>);
</span><span>53</span>         } <span>else</span> <span>if</span>(!<span>isset</span>(<span>$arguments</span>[1<span>])) {
</span><span>54</span>             <span>$get_obj</span>->info(<span>$arguments</span>[0<span>]);
</span><span>55</span>         } <span>else</span><span> {
</span><span>56</span>             <span>$get_obj</span>->{<span>$arguments</span>[1]}(<span>$arguments</span>[0<span>]);
</span><span>57</span> <span>        }
</span><span>58</span> <span>    }
</span><span>59</span> }
Copier après la connexion

使用的时候可以指定,如下:

<span>1</span> Save_log::<span>error_log</span>(<span>$info</span>, 'error'<span>);
</span><span>2</span> Save_log::debug_log(<span>$info</span>);
Copier après la connexion

日志内容如下:

二、上传文件。

Laravel中的上传文件是基于Flysystem提供的文件系统来实现上传,删除,移动。他支持多种驱动,还有一个值得看的云存储,在SAE上需要用到。

文件系统配置位于Config/filesystems.php,我使用的试本地驱动。Laravel中的上传目录有两个:public和Storage两个,有人说这两个一样,其实是有区别的,应该说是各有好处,如果放在public中,服务器可以直接控制访问,方便效率高,放在Storage中可以加上用户控制比如权限等。

上传需要的函数如下:

判断是否进行了上传,是否存在文件:

<span>1</span> <span>$request</span>->hasFile('file')
Copier après la connexion

判断上传是否出错:

<span>1</span> <span>$file</span> = <span>$request</span>-><span>file</span>('file'<span>);
</span><span>2</span> <span>//</span><span>判断文件上传过程中是否出错</span>
<span>3</span> <span>if</span>(!<span>$file</span>-><span>isValid()) {
</span><span>4</span>      <span>exit</span>('文件上传出错!'<span>);
</span><span>5</span> }
Copier après la connexion

确定上传:

<span>1</span> <span>$bytes</span> = Storage::<span>put(
</span><span>2</span>       <span>$savePath</span>,
<span>3</span>       <span>file_get_contents</span>(<span>$file</span>-><span>getRealPath())
</span><span>4</span> );
Copier après la connexion

你也可以使用:

<span>$path</span> = <span>$file</span> -> move('storage/uploads');
Copier après la connexion

生成缩略图

Laravel木有提供函数生成缩略图,但是我们可以借助强大的Composer来引入图片处理库 Integration/Image

在项目根目录中的composer.json中的require中添加:"intervention/image": "dev-master",如下图:

然后在config/app.php中providers数组中添加:

<span>1</span> Intervention\Image\ImageServiceProvider::<span>class</span>
Copier après la connexion

在aliases数组中添加别名:

<span>1</span> 'Image'     => Intervention\Image\Facades\Image::<span>class</span>,
Copier après la connexion

这样就可以使用了,在类文件中添加:

<span>1</span> <span>use</span> Image;
Copier après la connexion

下面是添加水印并且生成缩略图:

<span>$Image</span>->text('@ u/'. <span>$user_id</span>, <span>$news_width</span> - 40 - <span>$length</span> * 10, <span>$news_height</span> - 24, <span>function</span>(<span>$font</span><span>) {
       </span><span>$font</span>-><span>file</span>('public/foos.ttf'<span>);
       </span><span>$font</span>->size(14<span>);
       </span><span>$font</span>->color('#ffffff'<span>);
 });</span>
Copier après la connexion

最后附上整个源码,其中生成缩略图部分可以抽象出来,因为有好几个地方都需要用到,并且水印还有看图片大小等等。

<span>  1</span> <span>/*</span><span>*
</span><span>  2</span> <span> * 上传文件
</span><span>  3</span> <span> * @param  Object Request
</span><span>  4</span> <span> * @return Json result
</span><span>  5</span>  <span>*/</span>
<span>  6</span> <span>public</span> <span>function</span> upload_file(Request <span>$request</span><span>)
</span><span>  7</span> <span>{
</span><span>  8</span>     <span>$user_id</span> = <span>$request</span>->get('user_id'<span>);
</span><span>  9</span>     <span>$width</span> = <span>$request</span>->get('width'<span>);
</span><span> 10</span>     <span>$height</span> = <span>$request</span>->get('height'<span>);
</span><span> 11</span>     <span>$upload_type</span> = <span>$request</span>->get('upload_type'<span>);
</span><span> 12</span>     <span>$watermark</span> = <span>$request</span>->get('watermark'<span>);
</span><span> 13</span> 
<span> 14</span>     <span>//</span><span>参数检查</span>
<span> 15</span>     <span>if</span>(<span>empty</span>(<span>$user_id</span><span>)) {
</span><span> 16</span>         <span>return</span> response()->json(['code' => 1001, 'msg' => '参数错误'<span>]);
</span><span> 17</span> <span>    }
</span><span> 18</span> 
<span> 19</span>     <span>//</span><span>得到上传文件名</span>
<span> 20</span>     <span>if</span>(!<span>empty</span>(<span>$_FILES</span><span>)) {
</span><span> 21</span>         <span>$key_arr</span> =  <span>array_keys</span>(<span>$_FILES</span><span>);
</span><span> 22</span>         <span>$file_key</span> = <span>$key_arr</span>[0<span>];
</span><span> 23</span> <span>    }
</span><span> 24</span>     
<span> 25</span>     <span>$file_key</span> = !<span>isset</span>(<span>$file_key</span>) || <span>empty</span>(<span>$file_key</span>) ? 'fileselect' : <span>$file_key</span><span>;
</span><span> 26</span> 
<span> 27</span>     <span>if</span>(!<span>$request</span>->hasFile(<span>$file_key</span><span>)) {
</span><span> 28</span>         <span>return</span> response()->json(['code' => 1002, 'msg' => '上传文件为空'<span>]);
</span><span> 29</span> <span>    }
</span><span> 30</span> 
<span> 31</span>     <span>$upload_files</span> = <span>$request</span>-><span>file</span><span>();
</span><span> 32</span>     <span>if</span>(<span>empty</span>(<span>$upload_files</span>) || !<span>is_array</span>(<span>$upload_files</span><span>)) {
</span><span> 33</span>         <span>return</span> response()->json(['code' => 1003, 'msg' => '上传失败'<span>]);
</span><span> 34</span> <span>    }
</span><span> 35</span> 
<span> 36</span>     <span>//</span><span>兼容单文件上传</span>
<span> 37</span>     <span>if</span>(Utils::arrayLevel(<span>$upload_files</span>) < 2<span>) {
</span><span> 38</span>         <span>$files</span>[<span>$file_key</span>][0] = <span>$upload_files</span>[<span>$file_key</span><span>];
</span><span> 39</span>     } <span>else</span><span> {
</span><span> 40</span>         <span>$files</span> = <span>$upload_files</span><span>;
</span><span> 41</span> <span>    }
</span><span> 42</span> 
<span> 43</span>     <span>if</span>(<span>$upload_type</span> == 'userphoto' && <span>count</span>(<span>$files</span>[<span>$file_key</span>]) > 1<span>) {
</span><span> 44</span>         <span>return</span> response()->json(['code' => 1004, 'msg' => '头像只能上传一张'<span>]);
</span><span> 45</span> <span>    }
</span><span> 46</span> 
<span> 47</span>     <span>if</span>(<span>count</span>(<span>$files</span>[<span>$file_key</span>]) ><span> MAX_UPLOAD_FILE) {
</span><span> 48</span>         <span>return</span> response()->json(['code' => 1005, 'msg' => '大于最大上传数限制'<span>]);
</span><span> 49</span> <span>    }
</span><span> 50</span> 
<span> 51</span>     <span>//</span><span>过滤大于MAX_FILE_SIZE的情况</span>
<span> 52</span>     <span>foreach</span> (<span>$files</span>[<span>$file_key</span>] <span>as</span> <span>$key</span> => <span>$file</span><span>) {
</span><span> 53</span>         <span>if</span>(<span>$file</span>-> getClientSize() > MAX_FILE_SIZE * 1024 * 1024<span>) {
</span><span> 54</span>             <span>return</span> response()->json(['code' => 1006, 'msg' => '文件大小不能超过'<span>]);
</span><span> 55</span> <span>        }
</span><span> 56</span> <span>    }
</span><span> 57</span> 
<span> 58</span>     <span>$file_info</span> =<span> [];
</span><span> 59</span>     <span>$length</span> = <span>strlen</span>(<span>$user_id</span> . ''<span>);
</span><span> 60</span>     <span>//</span><span>兼容批量上传</span>
<span> 61</span>     <span>foreach</span> (<span>$files</span>[<span>$file_key</span>] <span>as</span> <span>$key</span> => <span>$file</span><span>) {
</span><span> 62</span>         <span>if</span>(!<span>$file</span>-><span>isValid()) {
</span><span> 63</span>             <span>return</span> response()->json(['code' => 1007, 'msg' => '上传出错'<span>]);
</span><span> 64</span> <span>        }
</span><span> 65</span> 
<span> 66</span>         <span>if</span>(<span>$upload_type</span> == 'userpic'<span>) {
</span><span> 67</span>             <span>$file_dir</span> = 'userpic'<span>;
</span><span> 68</span>         } <span>else</span><span> {
</span><span> 69</span>             <span>$type</span> = <span>$file</span>-><span>getMimeType();
</span><span> 70</span>             <span>if</span>(<span>empty</span>(<span>$type</span>) && !<span>is_array</span>(<span>$type</span><span>)) {
</span><span> 71</span>                 <span>return</span> response()->json(['code' => 1008, 'msg' => '得到文件类型出错'<span>]);
</span><span> 72</span> <span>            }
</span><span> 73</span> 
<span> 74</span>             <span>//</span><span>映射文件类型</span>
<span> 75</span>             <span>$type_arr</span> = <span>explode</span>("/", <span>$type</span><span>);
</span><span> 76</span>             <span>switch</span>(<span>$type_arr</span>[0<span>]){
</span><span> 77</span>                 <span>case</span> "image"      : <span>$file_dir</span> = "image"; <span>break</span><span>;
</span><span> 78</span>                 <span>case</span> "video"      : <span>$file_dir</span> = "video"; <span>break</span><span>;
</span><span> 79</span>                 <span>case</span> "audio"      : <span>$file_dir</span> = "voice"; <span>break</span><span>;
</span><span> 80</span>                 <span>case</span> "text"       : <span>$file_dir</span> = "doc";   <span>break</span><span>;
</span><span> 81</span>                 <span>case</span> "application": <span>$file_dir</span> = "doc";   <span>break</span><span>;
</span><span> 82</span>                 <span>default</span>           : <span>$file_dir</span> = "other"; <span>break</span><span>;
</span><span> 83</span> <span>            }
</span><span> 84</span> <span>        }
</span><span> 85</span> 
<span> 86</span>         <span>//</span><span>文件后缀</span>
<span> 87</span>         <span>$postfix</span> = <span>$file</span>-><span>getClientOriginalExtension();
</span><span> 88</span>         <span>$save_dir</span> =<span> UPLOAD_FILE_PATH;
</span><span> 89</span>         <span>$file_date</span> = <span>date</span>('Ym'<span>);
</span><span> 90</span>         <span>$file_name</span> = <span>$file_dir</span> . '_' . <span>$file_date</span> . '_' . <span>rand</span>(111111, 999999) . <span>$user_id</span><span>;
</span><span> 91</span>         <span>$save_name</span> = <span>$file_name</span> . '.' . <span>$postfix</span><span>;
</span><span> 92</span>         <span>$save_path</span> = <span>$file_dir</span> . '/' . <span>$file_date</span> . '/' . <span>$save_name</span><span>;
</span><span> 93</span>         Storage::<span>put(
</span><span> 94</span>             <span>$save_path</span>, 
<span> 95</span>             <span>file_get_contents</span>(<span>$file</span>-><span>getRealPath())
</span><span> 96</span> <span>        );
</span><span> 97</span>         <span>if</span>(!Storage::exists(<span>$save_path</span><span>)) {
</span><span> 98</span>             <span>return</span> response()->json(['code' => 1009, 'msg' => '保存文件失败'<span>]);
</span><span> 99</span> <span>        }
</span><span>100</span> 
<span>101</span>         <span>//</span><span>生成缩略图</span>
<span>102</span>         <span>if</span>(<span>$file_dir</span> == 'image' && (!<span>empty</span>(<span>$width</span>) || !<span>empty</span>(<span>$height</span><span>))) {
</span><span>103</span>             <span>$Image</span> = Image::make(<span>$save_dir</span> . <span>$save_path</span><span>);
</span><span>104</span>             <span>$img_width</span> = <span>$Image</span>-><span>width();
</span><span>105</span>             <span>$img_height</span> = <span>$Image</span>-><span>height();
</span><span>106</span> 
<span>107</span>             <span>//</span><span>如果有一个为空,则与另一个相等;</span>
<span>108</span>             <span>if</span>(<span>empty</span>(<span>$width</span><span>)) {
</span><span>109</span>                 <span>//</span><span>传入的高度如果比实际高度大,就取实际高度</span>
<span>110</span>                 <span>$height</span> = <span>$img_height</span> < <span>$height</span> ? <span>$img_height</span> : <span>$height</span><span>;
</span><span>111</span>                 <span>$width</span> = <span>$height</span><span>;
</span><span>112</span>             } <span>else</span> <span>if</span>(<span>empty</span>(<span>$height</span><span>)) {
</span><span>113</span>                 <span>$width</span> = <span>$img_width</span> < <span>$width</span> ? <span>$img_width</span> : <span>$width</span><span>;
</span><span>114</span>                 <span>$height</span> = <span>$width</span><span>;
</span><span>115</span>             } <span>else</span><span> {
</span><span>116</span>                 <span>$height</span> = <span>$img_height</span> < <span>$height</span> ? <span>$img_height</span> : <span>$height</span><span>;
</span><span>117</span>                 <span>$width</span> = <span>$img_width</span> < <span>$width</span> ? <span>$img_width</span> : <span>$width</span><span>;
</span><span>118</span> <span>            }
</span><span>119</span> 
<span>120</span>             <span>//</span><span>拼接缩略图路径</span>
<span>121</span>             <span>$Image</span>->resize(<span>$width</span>, <span>$height</span><span>);
</span><span>122</span>             <span>$save_name_s</span> = <span>$file_name</span> . '_s.' . <span>$postfix</span><span>;
</span><span>123</span>             <span>$save_path_s</span> = <span>$save_dir</span> . <span>$file_dir</span> . '/' . <span>$file_date</span> . '/' . <span>$save_name_s</span><span>;
</span><span>124</span>             <span>$file_path_s</span> = <span>$request</span>->root() . '/' . <span>$save_path_s</span><span>;
</span><span>125</span> 
<span>126</span>             <span>if</span>(<span>$watermark</span> != 1<span>) {
</span><span>127</span>                 <span>//</span><span>添加缩略图水印</span>
<span>128</span>                 <span>$news_width</span> = <span>$Image</span>-><span>width();
</span><span>129</span>                 <span>$news_height</span> = <span>$Image</span>-><span>height();
</span><span>130</span>                 <span>if</span>(<span>$news_width</span> > 100<span>) {
</span><span>131</span>                     <span>$Image</span>->text('@ u/'. <span>$user_id</span>, <span>$news_width</span> - 40 - <span>$length</span> * 10, <span>$news_height</span> - 24, <span>function</span>(<span>$font</span><span>) {
</span><span>132</span>                         <span>$font</span>-><span>file</span>('public/foos.ttf'<span>);
</span><span>133</span>                         <span>$font</span>->size(14<span>);
</span><span>134</span>                         <span>$font</span>->color('#ffffff'<span>);
</span><span>135</span> <span>                    });
</span><span>136</span> <span>                }
</span><span>137</span> <span>            }
</span><span>138</span> 
<span>139</span>             <span>//</span><span>保存缩略图</span>
<span>140</span>             <span>$Image</span>->save(<span>$save_path_s</span>, 100<span>);
</span><span>141</span>             <span>$file_size_s</span> = <span>round</span>(<span>$Image</span>-><span>filesize</span>() / 1024 ,2) . 'K'<span>;
</span><span>142</span> <span>        }
</span><span>143</span> 
<span>144</span>         <span>$file_path</span> = <span>$request</span>->root() . '/' . <span>$save_dir</span> . <span>$save_path</span><span>;
</span><span>145</span>         <span>$file_size</span> = <span>round</span>(<span>$file</span>-> getClientSize() / 1024 ,2) . 'K'<span>;
</span><span>146</span>         <span>$file_info</span>[] = <span>compact</span><span>(
</span><span>147</span>             'save_name', 'file_size', 'file_path', 'save_name_s', 'file_size_s', 'file_path_s'
<span>148</span> <span>        );
</span><span>149</span> <span>    }
</span><span>150</span> 
<span>151</span>     <span>if</span>(<span>empty</span>(<span>$file_info</span><span>)) {
</span><span>152</span>         <span>return</span> response()->json(['code' => 1010, 'msg' => '异常出错'<span>]);
</span><span>153</span>     } <span>else</span><span> {
</span><span>154</span>         <span>return</span> response()->json(['code' => 0, 'msg' => '', 'data' => <span>$file_info</span><span>]);
</span><span>155</span> <span>    }
</span><span>156</span> }
Copier après la connexion

结束语:

以上是我学习中遇到的一部分问题,不对之处欢迎指正,这篇文章只是说了日志和上传,以后会持续更新,包括路由,中间件,容器等等,还有好多需要说的。另外会同步更新到我的个人网站:www.zhaoyafei.cn,欢迎访问

转载注明出处

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!