您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9027|回复: 0
打印 上一主题 下一主题

[thinkphp学习资料] thinkphp 缩略图

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-27 15:27:51 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
  1. function imgs_uploads($path_old = null){
    ( \, p; ]! P/ A8 f
  2.        % O0 b0 ^; e) H& L4 {- ~! s
  3.         $images_path = './Uploads/image/shop/';- |7 c; b) G# }( o
  4.         if (!is_dir($images_path)) {5 ?" w1 o- B# A2 \) f* W4 F
  5.                 mkdir($images_path);: s+ ]; X. P7 o' T  p* y4 N% b+ C8 Q
  6.         }          
    ' q! \% k# A: B. Y  m1 ]
  7.           
    * V2 s& \- U2 P0 C4 \5 \% ^
  8. + d: Y/ B6 d  R
  9.         $upload = new \Think\Upload();//实列化上传类
      l, x. o2 H7 B: c
  10.         $upload->maxSize=4145728;//设置上传文件最大,大小* J3 L( @0 B  Q& Q' o
  11.         $upload->exts= array('jpg','gif','png','jpeg');//后缀
    - _2 q& \+ X# d) ~: k8 q
  12.         $upload->rootPath =$images_path;//上传目录
    $ Y' T7 v5 N, b1 ]1 A. P
  13.         $upload->savePath    = ''; // 设置附件上传(子)目录
    4 Q  D& g' P9 z- j& g
  14.         //$upload->autoSub     = true;
    - r# \- Y& }. [9 f7 x" h
  15.         //$upload->subName     = array('date','Ymd');. `( {9 Z2 K6 Y/ W! b& F
  16.         //$upload->saveName = array('uniqid','');//设置上传文件规则
    * w% L, y5 O! b/ V* d: V
  17.                 $upload->replace = false;- A) T2 `6 `4 h; X7 t% Q
  18.         $info= $upload->upload();//执行上传方法
    & @5 d9 K' g- c$ [  Z% ~% h
  19.         if(!$info){
      P% n' ]- B: v+ K
  20.             $res['status'] = 0;, W4 j2 z+ |. @3 M, |8 {8 F
  21.                         $res['res'] = $upload->getError();( K$ {% V  T+ v& a0 ]- [- d7 L
  22.                        
    + c+ d/ q/ r8 x* ~# T" r
  23.         }else {
    ) J+ K/ U, k/ |4 [. a2 q
  24.             5 q- T+ f9 W' j* S) V. j
  25.             //获取上传文件信息. N2 l) q+ P9 o/ a$ s
  26.             foreach ($info as $file){3 m' N, ?4 M4 W' e  R+ x# W/ o4 Y1 g! {
  27.             $img_path[] = '/Uploads/image/shop/'.$file['savepath'] . $file['savename'];
    6 m( d7 \- v' m+ |
  28.                         $name[]=$file['key'];8 w- r+ @) T" b( y7 B

  29. 8 R% N5 O3 X7 e4 H
  30.                
    % Y& A, b$ X/ ?2 A- v1 [
  31.             }
      e0 b' G9 P6 {0 \1 x9 k
  32.             //图片物理目录删除、改名图片用
    8 k9 c1 k; f6 N6 _& s% _; s
  33.                         $bigimg=$file['savepath'].$file['savename'];& F/ D6 K5 H3 Q
  34.                         //$_POST['goods_big_img']=$bigimg;
    * ~+ C2 d  v# D
  35.                        
    , n; P/ s- d6 }
  36.                        
      j( R: n& Y* R* R
  37.                         $image=new \Think\Image();% O) _, N9 i3 ]2 [
  38.                        
    & ~+ T* b7 n% T5 M4 Z2 k% X% s. y
  39.                         $srcimg=$upload->rootPath.$bigimg;
      t' M. b$ `8 N
  40.                         $image->open($srcimg);
    0 N  m: s- i' x+ ?/ n6 g1 r
  41.                         $image->thumb(500,500);
    / t( `) E$ f+ p: K7 u
  42.                         $smallimg=$file['savepath']."small_".$file['savename'];6 i: i! h' k; P) Q0 Y# K% P
  43.                         $image->save($upload->rootPath.$smallimg);% I( W% u( r/ c# H5 c/ O% @
  44.                         //$_POST['goods_small_img']=$smallimg;
    . J$ e$ ]8 x7 z5 N) G: |
  45.            
    7 ^; W5 x% t  m; O
  46. ( C( Z; F' O6 v- j9 p+ s" o5 r) J
  47.                
    2 C: W9 w5 u1 I! h% y$ s0 q
  48.                         if ($path_old && $img_path) {
    * e/ B0 q" n. F0 ?6 N9 l/ M
  49.                                 unlink('.' . $path_old);! p, u& r% e! j. `: x
  50.                         }3 |1 E' D% }/ X' ?( Y$ g4 H  g. B
  51.                
    9 `" L5 i; M8 v& r
  52.                 " M( J9 C  t7 Q" a! k( h2 B- i
  53.                         $res['stats'] = 1;
    : @/ Z2 R3 Y. x) R* z, C
  54.                         $res['res'] = $smallimg;
    % t8 H2 ~5 H$ G: i& d
  55.                         $res['name']=$name;
    . w% j: z: `4 y. F% M- _
  56.         }& a( ]+ w& H4 u$ `2 u* O" [% e0 m
  57.         
    . Q8 b* Y5 i6 Q1 Q2 @
  58.        
    2 Y9 a  |5 H; w, ^1 }  R2 `
  59. }
复制代码

, ~/ g' Q7 t- x, ]& _5 S  ^- x) I& ?" @9 k6 g2 t; E1 c
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-15 23:59 , Processed in 0.108986 second(s), 19 queries .

Copyright © 2001-2024 Powered by cncml! X3.2. Theme By cncml!