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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[thinkphp学习资料] thinkphp 缩略图

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-27 15:27:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. function imgs_uploads($path_old = null){
    / i1 q$ l0 J3 R) V
  2.       
    & |2 l7 g" a( F# @
  3.         $images_path = './Uploads/image/shop/';8 S! I6 e* U. G' u
  4.         if (!is_dir($images_path)) {# f4 Z9 C1 I: |9 J" l/ x  E
  5.                 mkdir($images_path);4 D# Q4 t. y: u+ M+ t! o" W
  6.         }          
    , ]/ ?! F+ y1 V# x' P9 N9 {, B
  7.            4 _2 K+ S: S5 X, Y

  8. . G5 d6 W8 M" U
  9.         $upload = new \Think\Upload();//实列化上传类6 `. s$ F4 j- Q& T# f8 Y
  10.         $upload->maxSize=4145728;//设置上传文件最大,大小+ g& [. ~2 r) n$ s) C! e
  11.         $upload->exts= array('jpg','gif','png','jpeg');//后缀
    . H- k: d, {8 f, W( a% g2 e; J6 N
  12.         $upload->rootPath =$images_path;//上传目录
      C/ }1 @0 h( F) K" b% }
  13.         $upload->savePath    = ''; // 设置附件上传(子)目录7 K4 @9 O# ^8 S
  14.         //$upload->autoSub     = true;
    3 w2 C5 s" U7 [
  15.         //$upload->subName     = array('date','Ymd');6 {" h1 I- R7 {4 ]1 t
  16.         //$upload->saveName = array('uniqid','');//设置上传文件规则3 ?8 Z6 y2 j+ F2 [: f
  17.                 $upload->replace = false;
    7 r" n2 q: s( S; D* V6 t. k
  18.         $info= $upload->upload();//执行上传方法
    : ?  T* F& s3 y- |3 `$ L$ l
  19.         if(!$info){
    6 S6 N7 J6 g; P& g2 N
  20.             $res['status'] = 0;
    5 L0 t8 L; s6 n5 z+ C& n
  21.                         $res['res'] = $upload->getError();+ \) R1 `; b) k: G/ u+ |& K
  22.                         / Q$ s" V4 Y5 N' `/ v0 e
  23.         }else {8 t* {6 |+ q2 h
  24.             - q; R" C. ]) `
  25.             //获取上传文件信息! x4 q1 d8 r7 ^) v% ?, k1 K
  26.             foreach ($info as $file){
    ! c9 @2 C( i/ T: t  j& L, r( O/ [
  27.             $img_path[] = '/Uploads/image/shop/'.$file['savepath'] . $file['savename'];
    1 C+ M% |8 [2 A! U  c+ P* w6 D
  28.                         $name[]=$file['key'];
    $ @' E: K9 p7 K0 p2 U
  29. + H  z0 w9 C; o7 e7 K
  30.                 ) R. [; w  J/ _9 V8 f& U+ |
  31.             }
    ' V) H! t; t3 y5 o
  32.             //图片物理目录删除、改名图片用! D' a# m$ ~, M1 O
  33.                         $bigimg=$file['savepath'].$file['savename'];
    1 U2 u# v# E% J+ ?
  34.                         //$_POST['goods_big_img']=$bigimg;% S2 m# n2 o$ A" s" O- R3 F% r
  35.                         - w5 I2 D3 q9 j4 `
  36.                         9 F" P0 f1 K4 k. a6 v) ]* O" J9 B
  37.                         $image=new \Think\Image();7 A7 d- a, G; u* ]
  38.                        
    " U2 h! H% F5 d6 D. T& ?9 q' \
  39.                         $srcimg=$upload->rootPath.$bigimg;) B1 Y* L* v8 ?' A& p  _
  40.                         $image->open($srcimg);% w! L2 i* H& P, j3 d
  41.                         $image->thumb(500,500);4 b$ @! k6 q6 u0 v& b
  42.                         $smallimg=$file['savepath']."small_".$file['savename'];
    1 R1 y0 T6 W5 M3 m( l
  43.                         $image->save($upload->rootPath.$smallimg);3 R$ Y& Y, c- b% ^; k4 C
  44.                         //$_POST['goods_small_img']=$smallimg;
    ' S0 I( x  z7 H; j
  45.            7 l' B6 T" m  ]4 E* K9 T

  46. , d/ w6 m: t; f$ ]
  47.                 - i$ @2 w5 l) K$ q3 W9 l( {8 _2 k' D
  48.                         if ($path_old && $img_path) {
    " S5 b5 Y* M5 Y$ y# z& k( N4 W
  49.                                 unlink('.' . $path_old);
    $ M+ f9 ?& X! U
  50.                         }
    ' ^$ A* ?# L+ }6 h/ W) ^
  51.                 : t& X( C, A$ Q" k& e( l+ ^
  52.                
    0 I4 W# U- y. K7 h9 \5 @# A
  53.                         $res['stats'] = 1;
    ( z4 S' K9 S8 Q; K) Y/ X
  54.                         $res['res'] = $smallimg;) N) }% e; H8 u9 H
  55.                         $res['name']=$name;: _* d. t, b; x; |# x
  56.         }% F7 ]7 _$ t8 a# G
  57.         2 \3 K) T9 ?  H" H' o
  58.         , U8 x* t1 f6 x  O7 w
  59. }
复制代码
! A  P6 H+ b  K. I" T; C- @" Z: D

5 N' q. c* W+ p& I
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 20:20 , Processed in 0.075415 second(s), 20 queries .

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