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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[thinkphp学习资料] thinkphp 缩略图

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-27 15:27:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
  1. function imgs_uploads($path_old = null){
    + ~+ a: C  N  `6 H0 z
  2.       
    * i( T, @! v6 f0 T/ Y) U8 |
  3.         $images_path = './Uploads/image/shop/';+ e* c, j  m5 Y7 J
  4.         if (!is_dir($images_path)) {
    / e* `/ d2 Z& w9 o
  5.                 mkdir($images_path);
    ( q; r! v$ |- q8 L+ M0 A
  6.         }          
    1 q! `- Y" h* x% d- c: l
  7.           
    & a5 z* {# E' z) A; a5 a$ Y: ~6 c, l# V
  8. . T7 J! v/ Q! H. E2 h+ j
  9.         $upload = new \Think\Upload();//实列化上传类
    $ t3 M) `1 n5 A& T( w4 S
  10.         $upload->maxSize=4145728;//设置上传文件最大,大小
    ( Z* a4 Q9 g1 d, i8 r) j9 M
  11.         $upload->exts= array('jpg','gif','png','jpeg');//后缀
    5 J* m( S4 l! Y
  12.         $upload->rootPath =$images_path;//上传目录; f/ J( U6 ^, F, m! A* D
  13.         $upload->savePath    = ''; // 设置附件上传(子)目录( L% }7 P" @; k* @! e* \
  14.         //$upload->autoSub     = true;6 |) W& a9 |0 m9 X% g
  15.         //$upload->subName     = array('date','Ymd');
    " @7 x1 p7 u' w
  16.         //$upload->saveName = array('uniqid','');//设置上传文件规则+ J3 O: C% C+ c4 _% v
  17.                 $upload->replace = false;
    * H5 ?  }( E7 M0 G
  18.         $info= $upload->upload();//执行上传方法
    + ]/ N* W  S9 G* e! N! Y& t
  19.         if(!$info){4 [* V9 |5 H7 S1 p
  20.             $res['status'] = 0;" _/ v% I6 O7 e  f9 ~: t3 ^: N
  21.                         $res['res'] = $upload->getError();+ v2 K- x* B0 ]; x0 t
  22.                         2 K7 c$ a2 l, _. V
  23.         }else {8 K% P: r, E4 ~
  24.             7 n" ]% K5 ^; c' w6 l. n
  25.             //获取上传文件信息
    9 {; V# I( n) s$ u+ j5 h
  26.             foreach ($info as $file){
    ) Y5 o/ `# H1 s3 x+ k
  27.             $img_path[] = '/Uploads/image/shop/'.$file['savepath'] . $file['savename'];
    % M4 m5 s! h% D6 M- Z5 v
  28.                         $name[]=$file['key'];
    ' P7 e% Q: i! U* F5 m

  29. 7 e. v0 {" ?' Z  K
  30.                
    + ^9 ~0 G/ t) H6 c9 K* p+ s% H
  31.             }
    9 {+ G: U' O$ z8 L- S& F6 Z
  32.             //图片物理目录删除、改名图片用
    $ A( R5 n* b! [- l% B6 c" V
  33.                         $bigimg=$file['savepath'].$file['savename'];" G  k2 t* g1 K
  34.                         //$_POST['goods_big_img']=$bigimg;# o! {) ~/ ^( A2 j1 _2 R$ `% D
  35.                        
    # Z$ i# R( m2 @$ r' |
  36.                        
    1 e( K- N5 T  p3 _7 F/ q& m
  37.                         $image=new \Think\Image();
    / k- n5 L0 d3 Q; n" Q8 o/ ]
  38.                         : o: b6 f6 t  c6 i5 D
  39.                         $srcimg=$upload->rootPath.$bigimg;
    : [6 d, W/ ?9 Q0 b) }# T
  40.                         $image->open($srcimg);
    5 }& V, z" I& ~2 c2 l
  41.                         $image->thumb(500,500);
    ; `+ B% \5 f7 F# c1 [, r
  42.                         $smallimg=$file['savepath']."small_".$file['savename'];2 _/ `' o! [: O) K& a5 _+ A
  43.                         $image->save($upload->rootPath.$smallimg);
    ( }0 o* B1 x) ~# H3 c
  44.                         //$_POST['goods_small_img']=$smallimg;
    8 K+ V1 m9 l, d1 {' E2 A
  45.            # X8 d+ `& S, B  }& w

  46. 2 Z- ]- D' n) f* v, x
  47.                 ' g4 d8 h6 f& u0 ^4 ^# {
  48.                         if ($path_old && $img_path) {$ B0 Z- c. a2 C; t
  49.                                 unlink('.' . $path_old);
    2 a4 ]+ M4 A. c8 i3 E. v
  50.                         }9 l3 S- o9 S- t' _: z. G! G; W$ L- }
  51.                
      [% ?! Y' ?) d; `( i2 F* F
  52.                 6 }' @5 k) J! g( ]5 c3 v# a
  53.                         $res['stats'] = 1;
    : J( E- ?  F" c$ A" S
  54.                         $res['res'] = $smallimg;* G1 W) q1 `1 g% q
  55.                         $res['name']=$name;
    " K9 p% U$ @$ Y) w, D- p' T4 k
  56.         }8 w  n0 g. |. H1 e% H
  57.         4 x. A6 Z' S# ?1 p! S" F9 y* o& e
  58.        
    7 T! v$ @3 \1 [' l- X* k2 [( \0 l
  59. }
复制代码

5 G* g2 W3 e5 i3 b, x/ w: B. h& |: G" B6 Q" x/ j" g' h
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 19:03 , Processed in 0.085812 second(s), 22 queries .

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