Home  >  Article  >  Backend Development  >  Logging and uploading in Laravel, _PHP tutorial

Logging and uploading in Laravel, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:57754browse

Logging and uploading in Laravel,

There are many frameworks in PHP, and I have come into contact with several of them myself. I didn't understand anything in college, so I picked up ThinkPHP and learned it. There may be many people complaining about TP, but I personally feel that I cannot say which framework is good and which framework is not good. No matter how bad a framework is, you can read the source code again. Once you understand the design ideas of the framework, you can learn a lot. Besides, there are many things that I don’t understand yet, so if you study a framework carefully, you can still learn a lot.

Let’s talk about Laravel first. It has now reached 5.2. As for myself, I have never come into contact with laravel before, but after learning laravel, I feel that this framework is really good, and foreigners enjoy using it. His development community is okay, and the documentation is relatively complete, but the official website documentation is not very good. You can’t read much from it (my own feeling). You have to read the source code for many things. For people like me who are not good at English, I prefer Chinese. Documentation (will have to be changed later). Laravel uses Composer (https://getcomposer.org) to manage dependencies, which is indeed more convenient, but because the mirror is blocked, it is slow (almost fails) when accessing or updating. Here is a solution: http:// pkg.phpcomposer.com/#tip1.

Recently, the project needs to be restructured due to work needs (it goes without saying the reason for the refactoring, everyone understands it), and it needs to be migrated to a new framework. Laravel is a good framework, with powerful routing, convenient configuration, and highly available module dependencies, which indeed saves a lot of effort in development. Considering that our project is mainly about the interface part, it has certain performance requirements (but not harsh), and the routing cannot be changed and it needs to be compatible with the old logic, so Laravel is the first choice. However, there is a problem that we are writing interfaces, so we need There are much fewer things to rely on. For example, the view layer is almost unused, and the test module, upload module (with image bed), localized module file system, etc. are also unused, so using Laravel is quite wasteful. To put it bluntly, He is heavier. So we considered Lumen, a framework based on Laravel. Compared with Laravel, a full-stack framework, Lumen is much simpler, and Lumen is Api-oriented, so we finally chose Lumen.

Laravel is not omnipotent and has advantages and disadvantages. For example, it has too many dependencies. You can take a look at the default dependency source package of the installed laravel framework, which is about 30M, which is indeed a bit large. Until today, I found that Lumen is a bit inadequate during use. As the business logic becomes more and more complex, the access speed has also slowed down in all aspects. We sometimes consider slim and other lightweight ones. In fact, many people here at Sina are Niaoge fans, many people admire yaf. yaf is really awesome. Practice has proved that it is more than 1 or 2 points faster. It is estimated that it will have to be migrated to yaf in the future. Didn’t PHP7 come out during this period, but the test results show that there are a lot of bugs. Migrating the interface to 7 should have a lot of performance improvements. It is said to be 100% improved. I haven’t dared to try it yet. Let’s wait until it is stable!

Having said a lot of nonsense, I will introduce logging and uploading in Laravel. Take your time. I will write part of this article first, because Laravel has a lot of things. I will write out the rest slowly. I will talk about the problems and pitfalls I encountered during use to help everyone learn.

1. Log

1. Description:

The importance of logs is self-evident. Our logs are based on manual recommendations, hobbies and recommendations. There are more students making recommendations than writing interfaces. If there is a problem with the log, the recommendation will be inaccurate or even impossible, which shows the importance of the log.

After the Laravel framework is initialized, error and exception handling have been configured by default. Its log is based on Monolog, a very useful log management tool,

 First of all, let’s talk about Monolog, which is a relatively complete and easily expandable logging component under PHP. Among them, well-known PHP frameworks such as Symfony and CakePHP have built-in Monolog. If you are interested, you can take a look. Each Logger instance has a channel and log processor stack. Whenever you add a log record, it is sent to the log handler stack. You can create many Loggers Each Logger defines a channel (db, request, routing), and each Logger has many log processors. These channels filter the logs. Each log processor has a Formatter (built-in log display format processor). You can also set the log level. (Official website explanation)

Log configuration: Laravel currently supports four log processors,

<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)

After understanding the log processing method, we can set the method we need. The corresponding setting in config/app.php (default):

<span>1</span> 'log' => 'single',

2. Use Log to record logs

Laravel provides the Log method to record logs. The Log actually used by IlluminateLogWriter should be because Writer is injected into the constructor of MonologLogger. The generated log files are stored in the storage/logs directory.

is as follows:

<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>

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> }

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

<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>);

日志内容如下:

二、上传文件。

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

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

上传需要的函数如下:

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

<span>1</span> <span>$request</span>->hasFile('file')

判断上传是否出错:

<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> }

确定上传:

<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> );

你也可以使用:

<span>$path</span> = <span>$file</span> -> move('storage/uploads');

生成缩略图

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

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

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

<span>1</span> Intervention\Image\ImageServiceProvider::<span>class</span>

在aliases数组中添加别名:

<span>1</span> 'Image'     => Intervention\Image\Facades\Image::<span>class</span>,

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

<span>1</span> <span>use</span> Image;

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

<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>

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

<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> }

结束语:

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

转载注明出处

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1084383.htmlTechArticleLaravel中的日志与上传, PHP中的框架众多,我自己就接触了好几个。大学那会啥也不懂啥也不会,拿了一个ThinkPHP学了。也许有好多人吐槽...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn