TP3.2:上传预览+缩略图+水印实例

uploadify上传预览+缩略图+水印实例
首先感谢各位大神网友的分享,我只是改改代码,让其更符合自己的业务逻辑
图片上传+缩略图+水印处理代码:
  1.     //文件上传
  2.     Public function _upload( $thumb = false , $thumbWidth = '' , $thumbHeight = '') {
  3.         $upload = new \Think\Upload();// 实例化上传类
  4.         $upload->maxSize = 3145728 ;// 设置附件上传大小
  5.         $upload->exts =  array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  6.         $upload->savePath =  '/' . CONTROLLER_NAME .'/'; // 设置附件上传目录
  7.         $info = $upload->upload();
  8.         if(!$info) {
  9.             return array('status' =>0, 'info'=> $upload->getError() );
  10.         }else{
  11.             if( $thumb ) {    //生成缩略图
  12.                 $image = new \Think\Image(); 
  13.                 foreach($info as $file) {
  14.                     $thumb_file = './Uploads/' . $file['savepath'] . $file['savename'];
  15.                     $save_path = './Uploads/' .$file['savepath'] . 'mini_' . $file['savename'];
  16.                     $image->open( $thumb_file )->text('德兴房产','./data/1.otf',30,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->save( $thumb_file );
  17.                     $image->open( $thumb_file )->text('德兴房产','./data/1.otf',24,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->thumb( $thumbWidth, $thumbHeight )->save( $save_path );
  18.                     return array(
  19.                         'status' => 1, 
  20.                         'savepath' => $file['savepath'],
  21.                         'savename' => $file['savename'],
  22.                         'pic_path' => $file['savepath'] . $file['savename'],
  23.                         'mini_pic' => $file['savepath'] . 'mini_' .$file['savename']
  24.                     );   
  25.                 }
  26.             }else{
  27.                 foreach($info as $file) {
  28.                     return array(
  29.                         'status' => 1, 
  30.                         'savepath' => $file['savepath'],
  31.                         'savename' => $file['savename'],
  32.                         'pic_path' => $file['savepath'].$file['savename']
  33.                     );   
  34.                 }
  35.             }
  36.         }
  37.     }
复制代码
前端主要代码(参考 http://www.thinkphp.cn/code/151.html ):
  1.                         <div class="tab-pane" id="tab3">
  2.                             <div class="row">
  3.                                 <div class="col-md-12">
  4.                                     <div class="tab-pane">
  5.                                         <div class="form-group">
  6.                                             <input type="file" id="upload" class="form-control input-medium" />
  7.                                         </div>
  8.                                         <div style="width:100%; float:left;padding:10px 20px 20px; background-color:#ccc">
  9.                                             <p>
  10.                                                 <ul class="imagelist" id="image_result"></ul>
  11.                                             </p>
  12.                                         </div>
  13.                                     </div>
  14.                                 </div>
  15.                             </div>
  16.                         </div> <!-- END #TAB3 -->
  17.                         <div class="margin-top-10">
  18.                             <button type="submit" class="btn green ajax-post">确 认</button>
  19.                             <a href="javascript:" onclick="javascript:history.back(-1);return false;" class="btn default">返 回</a>
  20.                         </div>
  21.                     </div> 
  22.                 </div>
  23.             </form>
  24.         </div>
  25.     </div>
  26. </block>
  27. <block name="foot">
  28.     <link href="__PUBLIC__/assets/plugins/uploadify/uploadify.css" rel="stylesheet" type="text/css"/>
  29.     <link href="__PUBLIC__/assets/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>
  30.     <script src="__PUBLIC__/assets/plugins/uniform/jquery.uniform.min.js" type="text/javascript" ></script>
  31.     <script src="__PUBLIC__/assets/plugins/uploadify/jquery.uploadify.min.js" type="text/javascript" ></script>
  32.     <script type="text/javascript">
  33.     $(function(){
  34.         var sid = "{:session_id()}";
  35.         $('#upload').uploadify({
  36.             'swf':'__PUBLIC__/assets/plugins/uploadify/uploadify.swf',
  37.             'buttonText': '选择图片',
  38.             'formData': { 'session_id':sid},
  39.             'uploader': "{:U('uploadPic')}",
  40.             'fileTypeDesc':'Image File',
  41.             'fileTypeExts':'*.jpg; *.jpeg; *.gif; *.png',
  42.                       'auto' : true,
  43.             'removeCompleted': false,
  44.             onUploadSuccess: function(file, data, response) {
  45.                 $('#progress').hide();
  46.                 var result = $.parseJSON(data);
  47.                 //错误处理。。。
  48.                 if(result.status == 0){
  49.                     alert(result.info);
  50.                     return false;
  51.                 }
  52.                 //上传成功
  53.                 var id = Math.random().toString();
  54.                 id = id.replace('.','_'); //生成唯一标示
  55.                 var html = '<li class="imageitem" id="'+id+'">';
  56.                     html+= '<input type="hidden" name="file[]" value="'+result.pic_path+'">'; //隐藏域,是为了把图片地址入库。。
  57.                     html+= '<input type="hidden" name="minifile[]" value="'+result.mini_pic+'">'; //隐藏域,是为了把图片地址入库。。
  58.                     html+= '<img height="160" width="160" src="__ROOT__/Uploads/'+result.pic_path+'" />';
  59.                     html+=  '<span class="txt">';
  60.                     html+=  '<a title="删除" href=javascript:remove("'+result.pic_path+'","'+id+'");><img src="__PUBLIC__/assets/plugins/uploadify/remove.png" /></a>';
  61.                     html+=  '<a title="设为封面" href=javascript:tocover("'+result.pic_path+'");><img src="__PUBLIC__/assets/plugins/uploadify/right.png" /></a>';
  62.                     html+=  '</span>';
  63.                     html+=  '<em><input class="form-control" value="'+file.name+'"></em>';
  64.                     html+=  '</li>';
  65.                 $('#image_result').append(html);
  66.             },
  67.             onUploadStart: function(){
  68.                 $('#progress').text('正在上传...');  //用户等待提示。
  69.             },
  70.             onInit: function (){  
  71.                 $("#upload-queue").hide(); //隐藏上传队列                
  72.             }    
  73.         }); 
  74.     });
  75.     function remove(file,id){
  76.         alert('删除成功!'+"\r\n"+file);
  77.         $('#'+id).remove();
  78.     }
  79.     function tocover(file){
  80.         alert('设为封面成功'+"\r\n"+file);
  81.     }
  82.     function check(){
  83.         if($('input[name="title"]').val()==""){
  84.             toastr['error']('标题不能为空!');
  85.             return false;
  86.         }
  87.     }
  88.     </script>
  89. </block>
复制代码
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TP3.2 是一个基于PHP的开源框架,它提供了很多方便的功能,其中包括利用jQuery Ajax实现分页功能。下面是一个例子说明如何使用jQuery Ajax实现前台与后台的分页功能: 前台源码: ```html <!DOCTYPE html> <html> <head> <title>分页示例</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ var currentPage = 1; // 当前页码 // 加载数据函数 function loadData(page){ $.ajax({ url: 'loadData.php', type: 'POST', data: {page: page}, success: function(response){ $("#dataContainer").html(response); } }); } // 初始加载数据 loadData(currentPage); // 点击页面切换按钮 $(document).on("click", ".pagination a", function(e){ e.preventDefault(); var page = $(this).attr("data-page"); currentPage = page; loadData(currentPage); }); }); </script> </head> <body> <div id="dataContainer"></div> </body> </html> ``` 后台源码(loadData.php): ```php <?php include "dbconfig.php"; // 引入数据库配置文件 $page = $_POST['page']; $perPage = 10; // 每页显示记录数 $offset = ($page - 1) * $perPage; // 计算偏移量 $result = $conn->query("SELECT * FROM your_table LIMIT $offset, $perPage"); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "<p>{$row['name']}</p>"; } } $conn->close(); ?> ``` 上述代码中,前台页面加载时会发送一个Ajax请求到后台的`loadData.php`文件,同时传递一个`page`参数表示当前页码。后台根据参数查询对应的数据,并将结果返回给前台,然后前台更新页面内容。用户可以通过点击页面切换按钮,改变`page`参数的值,从而实现翻页功能。 需要注意,后台代码中的`dbconfig.php`文件应该包含数据库连接的配置信息,以确保能够成功连接数据库并查询数据。 这只是一个简单的分页功能示例,你可以根据自己的实际情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值