thinkphp上传图片后由于图片较大需要剪切,但是由于大部分浏览器阻挡剪切弹出窗口造成无法剪切

故使用thinkphp的生成缩略图的功能,上传的同时直接生成缩略图并覆盖原图,这样就可以自定义上传图片的大小(备注,生成缩略图是按原比例缩放的)

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public  function  upload( $action ){
         $upload  new  \Think\Upload();
         $upload ->maxSize = 2097172;
         $upload ->exts =  array ( 'jpg' , 'gif' , 'png' , 'jpeg' );
         $upload ->savePath =  '/Public/Uploads/' ;
         $info  $upload ->upload();
         foreach ( $info  as  $file ){
             $name  $file [ 'savepath' ]. $file [ 'savename' ];
              }
         $this ->assign( 'action' , $action );
         $this ->assign( 'name' , $name );
         if (! $info ){
            $this ->error( $upload ->getError());
         } else {
             
             //生成缩略图
             $image  new  \Think\Image();
             $image ->open( "./Uploads{$name}" );
                         // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                         $image ->thumb(800, 800)->save( "./Uploads{$name}" ); //直接把缩略图覆盖原图
             $this ->display( 'image' );
            
     }