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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[thinkphp学习资料] thinkphp 缩略图

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-27 15:27:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
  1. function imgs_uploads($path_old = null){2 I: w1 L8 q8 c7 N1 h
  2.        - ^2 o4 |4 c' }% g9 W" T# Q
  3.         $images_path = './Uploads/image/shop/';
    - ?3 a& J6 A; x4 g: S6 A: h
  4.         if (!is_dir($images_path)) {+ T9 ~9 F4 P5 F% v0 B1 W3 M0 v
  5.                 mkdir($images_path);
    0 y3 T7 m3 u& d* b5 G$ X: J: @
  6.         }          
    + r$ d4 b, [+ j' W) P/ ^2 C. Z9 W
  7.           
    6 c# J4 J) c6 j) {9 e

  8. 0 _2 ?' a* Y) _1 H! U. ~6 |
  9.         $upload = new \Think\Upload();//实列化上传类
    # P# t$ ?2 k) T$ M& t: ], `* j
  10.         $upload->maxSize=4145728;//设置上传文件最大,大小9 k% `# X1 O" x  `- L
  11.         $upload->exts= array('jpg','gif','png','jpeg');//后缀8 J) q8 e8 X) D% R3 ?, R, b3 k
  12.         $upload->rootPath =$images_path;//上传目录, g: u7 w; t/ P) r
  13.         $upload->savePath    = ''; // 设置附件上传(子)目录9 [6 V* {" `% I& L* G# A' `. }6 B
  14.         //$upload->autoSub     = true;& y6 A) O! ]+ O- M! F& V5 Y
  15.         //$upload->subName     = array('date','Ymd');& h8 _: `0 [- E
  16.         //$upload->saveName = array('uniqid','');//设置上传文件规则8 b4 ^  u$ I& r% Z3 q
  17.                 $upload->replace = false;5 X( e: A1 F& X* G
  18.         $info= $upload->upload();//执行上传方法6 c" |8 o. V# v9 c4 f  s- n
  19.         if(!$info){
    + i- e" C* W9 W9 g8 o; f
  20.             $res['status'] = 0;  w1 @$ ~4 |9 t8 {8 t' u# H6 W
  21.                         $res['res'] = $upload->getError();
    - C/ @% o$ b& c/ I/ N
  22.                         0 D" l0 M- Z* |2 `% ~
  23.         }else {5 `, _& w5 b3 p) s" T+ T
  24.             
    : U# [- f4 v7 P' ?9 M7 u9 G
  25.             //获取上传文件信息
    0 l# G0 f( e$ _, Y: U  u3 K/ Y
  26.             foreach ($info as $file){# @: L% u/ A) q4 J
  27.             $img_path[] = '/Uploads/image/shop/'.$file['savepath'] . $file['savename'];
    % F) y, k! G5 F; s  `( o
  28.                         $name[]=$file['key'];
    / Z9 I- u. D+ q# U$ n
  29. , Q8 C, M' S1 A: ?
  30.                
    % t3 B: ]$ z9 O; s0 l
  31.             }
    & b3 D' j; |9 B3 [$ q; s! y
  32.             //图片物理目录删除、改名图片用
    " k/ H- |$ V8 M4 {* D) K. b5 I& `
  33.                         $bigimg=$file['savepath'].$file['savename'];$ `! e1 l/ K0 C' S# q
  34.                         //$_POST['goods_big_img']=$bigimg;3 l$ E) }. _- o5 o; h- ]/ A
  35.                         0 x# ]! b# [1 j/ |
  36.                         0 V+ b7 ~" H7 S. c. t% x# Q: |" X' H
  37.                         $image=new \Think\Image();& i) [4 }% g& o1 z2 x' r$ p8 X( I
  38.                        
    , t' `* p/ y& B; U/ ]+ J
  39.                         $srcimg=$upload->rootPath.$bigimg;
    8 g- f6 e2 J3 X  T2 ?2 E) Z4 K; p
  40.                         $image->open($srcimg);4 O$ i* Z* \* \  ~
  41.                         $image->thumb(500,500);
      w0 [0 P2 E$ ?0 r
  42.                         $smallimg=$file['savepath']."small_".$file['savename'];# H2 Q* i' o7 [2 o
  43.                         $image->save($upload->rootPath.$smallimg);, L5 N( W- k' x7 P2 l- \2 j
  44.                         //$_POST['goods_small_img']=$smallimg;
    : N$ {7 x$ v- S/ u9 e5 o+ t
  45.            0 t; ]2 m  i, N) Z7 M; W

  46. % k# f; T& d- {& ?( B8 s2 j
  47.                 - @5 o% v( ]" |) |3 T- W6 x
  48.                         if ($path_old && $img_path) {
    4 Z2 ~' A/ {, i
  49.                                 unlink('.' . $path_old);
    - C" c$ W- i# g) B
  50.                         }
    ' P, p, d" h: D* g) K* t! y$ ]% V/ e2 n
  51.                
    ( E% N, L, X' x9 Z) Q3 w/ @
  52.                 & T1 T$ k9 L+ L7 x
  53.                         $res['stats'] = 1;' c# o6 ]) ^7 f
  54.                         $res['res'] = $smallimg;, s* _* O3 T9 b
  55.                         $res['name']=$name;) _  y7 X: A+ n+ x' `
  56.         }
    ! B2 p& H; l! Z4 ?& J- `
  57.         , |$ ~9 _' T$ J% l  X6 W
  58.         0 t8 y; r& i8 r2 W, M2 U
  59. }
复制代码
5 z3 O0 `6 E( a- F  m+ U

/ _% W& {' J) ?7 E
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-7-20 00:32 , Processed in 0.737561 second(s), 22 queries .

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