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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[thinkphp学习资料] thinkphp 缩略图

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-27 15:27:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. function imgs_uploads($path_old = null){8 G" o# U5 O6 w, h9 `
  2.        ) p% `- m8 E" p0 @  Y1 T7 B
  3.         $images_path = './Uploads/image/shop/';
    ) G5 V  @& w/ P3 o( B
  4.         if (!is_dir($images_path)) {- F1 ?1 R6 d# |3 Q
  5.                 mkdir($images_path);, U5 E1 x. W& ]! W0 [8 _
  6.         }           $ R$ T' r7 k/ W, P8 r
  7.           
    . c! e' @' Q, v
  8. $ {. K' K! q# g# v8 k. ]
  9.         $upload = new \Think\Upload();//实列化上传类
    8 u' @; C' c/ E, ^0 \# {
  10.         $upload->maxSize=4145728;//设置上传文件最大,大小
      v1 u# K6 N! H' q2 i. ]
  11.         $upload->exts= array('jpg','gif','png','jpeg');//后缀
    . R1 Q# f! Y3 D6 K
  12.         $upload->rootPath =$images_path;//上传目录
    5 [6 R& @* |# A3 R9 h& a
  13.         $upload->savePath    = ''; // 设置附件上传(子)目录
    9 P/ s" [+ M: `( k- k! R6 x3 |
  14.         //$upload->autoSub     = true;
    ) M* q- L, B  l5 ]9 e7 @
  15.         //$upload->subName     = array('date','Ymd');
    2 ~9 L" y5 |! [) o1 u* r/ o
  16.         //$upload->saveName = array('uniqid','');//设置上传文件规则
    1 D" G! |6 j9 V' Y. |  h1 ]6 q
  17.                 $upload->replace = false;
    ) w5 W! H7 u$ a4 q2 `  H3 I
  18.         $info= $upload->upload();//执行上传方法" C- w  m3 t3 {* r6 R
  19.         if(!$info){
    & k* e7 G2 C7 n/ Q
  20.             $res['status'] = 0;
    ( a7 A. G1 c. e. J1 c- j4 A
  21.                         $res['res'] = $upload->getError();8 S) n. }' f: P7 x+ u
  22.                        
    5 m- v) Y6 l! m9 {1 z; O
  23.         }else {; I  J* U" i' T- P2 f% q
  24.             ' j6 e$ d( S3 Q4 `
  25.             //获取上传文件信息
    & {* N9 S  o7 F
  26.             foreach ($info as $file){
    ' j  E! \, G6 N7 |) K7 C* i5 z
  27.             $img_path[] = '/Uploads/image/shop/'.$file['savepath'] . $file['savename'];
    ! d3 d5 A+ g" n8 p/ l8 d" N- L1 c
  28.                         $name[]=$file['key'];$ @5 i! ^& f7 j

  29. 9 B/ `+ B! t( K" e. j4 c! @
  30.                 0 z8 U8 @% ^& ?( h$ [, b% B4 v* @
  31.             }
    $ f! V" \- N3 C8 D$ q; s
  32.             //图片物理目录删除、改名图片用1 ~* R- B, x. u8 h
  33.                         $bigimg=$file['savepath'].$file['savename'];. y7 l: w' u- V/ b
  34.                         //$_POST['goods_big_img']=$bigimg;2 a+ P% _1 w7 B
  35.                         * A7 h; P! \$ F1 H; u
  36.                         6 h( {9 K, _1 I! s0 L' ~% D) s
  37.                         $image=new \Think\Image();
    ( m. A( R5 [  @
  38.                        
    4 f( }$ `( M" p) U! ~
  39.                         $srcimg=$upload->rootPath.$bigimg;
    : y1 D5 [: i6 B
  40.                         $image->open($srcimg);" G8 J  _; C- `* \: H
  41.                         $image->thumb(500,500);
    ! @7 A# D6 p. x4 F1 y4 G
  42.                         $smallimg=$file['savepath']."small_".$file['savename'];
    3 W* I& q7 b2 j, W( ^+ n
  43.                         $image->save($upload->rootPath.$smallimg);( P# G6 [2 W" j  {: B
  44.                         //$_POST['goods_small_img']=$smallimg;( ?9 ^" m: v4 a, I- u
  45.            
    $ ~# x$ j4 T0 w& R

  46. + d- |# b6 x/ M5 k7 }. n( o
  47.                   f4 w/ r3 B1 e$ t1 W
  48.                         if ($path_old && $img_path) {
    ' d1 B. u" E4 ^! @% G$ m1 t) p
  49.                                 unlink('.' . $path_old);- T5 T- @/ e3 g
  50.                         }  B: O& P& ]. f% X7 i! M, W
  51.                 , A. V/ B4 w3 E) ?# d% s
  52.                
    " c2 G' t8 h, ]5 h2 I* \4 g
  53.                         $res['stats'] = 1;
    & c) z' _" T2 j5 `) L! t' A
  54.                         $res['res'] = $smallimg;( D  K0 r0 e3 i% H& w! b# b% ^
  55.                         $res['name']=$name;# _+ }& v% d- R) n3 Y) a1 X: l
  56.         }  B, p. l! y7 x8 D1 c3 l& B! V( y
  57.         
    , u% J$ w& ?6 V3 s0 T4 A8 j
  58.        
    6 c; X( N5 O: a$ W
  59. }
复制代码
8 w* L7 i: l8 i: y/ N8 L
  r2 y$ U9 L7 z1 W
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-9-29 05:12 , Processed in 0.118111 second(s), 20 queries .

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