Heim  >  Artikel  >  php教程  >  TP3.2:上传预览+缩略图+水印实例

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

WBOY
WBOYOriginal
2016-06-07 11:41:441082Durchsuche

uploadify上传预览+缩略图+水印实例
首先感谢各位大神网友的分享,我只是改改代码,让其更符合自己的业务逻辑
图片上传+缩略图+水印处理代码:    //文件上传<br>     Public function _upload( $thumb = false , $thumbWidth = '' , $thumbHeight = '') {<br>         $upload = new \Think\Upload();// 实例化上传类<br>         $upload->maxSize = 3145728 ;// 设置附件上传大小<br>         $upload->exts =  array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型<br>         $upload->savePath =  '/' . CONTROLLER_NAME .'/'; // 设置附件上传目录<br>         $info = $upload->upload();<br>         if(!$info) {<br>             return array('status' =>0, 'info'=> $upload->getError() );<br>         }else{<br>             if( $thumb ) {    //生成缩略图<br> <br>                 $image = new \Think\Image(); <br> <br>                 foreach($info as $file) {<br>                     $thumb_file = './Uploads/' . $file['savepath'] . $file['savename'];<br>                     $save_path = './Uploads/' .$file['savepath'] . 'mini_' . $file['savename'];<br>                     $image->open( $thumb_file )->text('德兴房产','./data/1.otf',30,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->save( $thumb_file );<br>                     $image->open( $thumb_file )->text('德兴房产','./data/1.otf',24,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->thumb( $thumbWidth, $thumbHeight )->save( $save_path );<br>                     return array(<br>                         'status' => 1, <br>                         'savepath' => $file['savepath'],<br>                         'savename' => $file['savename'],<br>                         'pic_path' => $file['savepath'] . $file['savename'],<br>                         'mini_pic' => $file['savepath'] . 'mini_' .$file['savename']<br>                     );   <br>                 }<br>             }else{<br>                 foreach($info as $file) {<br>                     return array(<br>                         'status' => 1, <br>                         'savepath' => $file['savepath'],<br>                         'savename' => $file['savename'],<br>                         'pic_path' => $file['savepath'].$file['savename']<br>                     );   <br>                 }<br>             }<br>         }<br>     }前端主要代码(参考http://www.thinkphp.cn/code/151.html):                        <div> <br>                             <div> <br>                                 <div> <br>                                     <div> <br>                                         <div> <br>                                             <input><br>                                         </div> <br>                                         <div> <br>                                             <p><br>                                                 </p> <ul></ul> <br>                                             <br>                                         </div> <br>                                     </div> <br>                                 </div> <br>                             </div> <br>                         </div> <!-- END #TAB3 --><br> <br>                         <div> <br>                             <button>确 认</button><br>                             <a>返 回</a><br>                         </div> <br>                      <br>                 <br>             <br>         <br>     <br> <br> <block><br>     <link> <br>     <link> <br>     <script></script><br>     <script></script><br>     <script><br /> $(function(){<br /> var sid = "{:session_id()}";<br /> $(&#039;#upload&#039;).uploadify({<br /> &#039;swf&#039;:&#039;__PUBLIC__/assets/plugins/uploadify/uploadify.swf&#039;,<br /> &#039;buttonText&#039;: &#039;选择图片&#039;,<br /> &#039;formData&#039;: { &#039;session_id&#039;:sid},<br /> &#039;uploader&#039;: "{:U(&#039;uploadPic&#039;)}",<br /> &#039;fileTypeDesc&#039;:&#039;Image File&#039;,<br /> &#039;fileTypeExts&#039;:&#039;*.jpg; *.jpeg; *.gif; *.png&#039;,<br /> &#039;auto&#039; : true,<br /> &#039;removeCompleted&#039;: false,<br /> onUploadSuccess: function(file, data, response) {<br /> $(&#039;#progress&#039;).hide();<br /> var result = $.parseJSON(data);<br /> //错误处理。。。<br /> if(result.status == 0){<br /> alert(result.info);<br /> return false;<br /> }<br /> //上传成功<br /> var id = Math.random().toString();<br /> id = id.replace(&#039;.&#039;,&#039;_&#039;); //生成唯一标示<br /> var html = &#039;<li class="imageitem" id="&#039;+id+&#039;">&#039;;<br /> html+= &#039;<input type="hidden" name="file[]" value="&#039;+result.pic_path+&#039;">&#039;; //隐藏域,是为了把图片地址入库。。<br /> html+= &#039;<input type="hidden" name="minifile[]" value="&#039;+result.mini_pic+&#039;">&#039;; //隐藏域,是为了把图片地址入库。。<br /> html+= &#039;<img style="max-width:90%" style="max-width:90%" src="__ROOT__/Uploads/&#039;+result.pic_path+&#039;" / alt="TP3.2:上传预览+缩略图+水印实例" >&#039;;<br /> html+= &#039;<span class="txt">&#039;;<br /> html+= &#039;<a title="删除" href=javascript:remove("&#039;+result.pic_path+&#039;","&#039;+id+&#039;");><img src="__PUBLIC__/assets/plugins/uploadify/remove.png" / alt="TP3.2:上传预览+缩略图+水印实例" >&#039;;<br /> html+= &#039;<a title="设为封面" href=javascript:tocover("&#039;+result.pic_path+&#039;");><img src="__PUBLIC__/assets/plugins/uploadify/right.png" / alt="TP3.2:上传预览+缩略图+水印实例" >&#039;;<br /> html+= &#039;&#039;;<br /> html+= &#039;<em><input class="form-control" value="&#039;+file.name+&#039;">&#039;;<br /> html+= &#039;&#039;;<br /> $(&#039;#image_result&#039;).append(html);<br /> },<br /> onUploadStart: function(){<br /> $(&#039;#progress&#039;).text(&#039;正在上传...&#039;); //用户等待提示。<br /> },<br /> onInit: function (){ <br /> $("#upload-queue").hide(); //隐藏上传队列 <br /> } <br /> }); <br /> });<br /> function remove(file,id){<br /> alert(&#039;删除成功!&#039;+"\r\n"+file);<br /> $(&#039;#&#039;+id).remove();<br /> }<br /> function tocover(file){<br /> alert(&#039;设为封面成功&#039;+"\r\n"+file);<br /> }<br /> function check(){<br /> if($(&#039;input[name="title"]&#039;).val()==""){<br /> toastr[&#039;error&#039;](&#039;标题不能为空!&#039;);<br /> return false;<br /> }<br /> }<br /> <br /> </script><br> </block>TP3.2:上传预览+缩略图+水印实例TP3.2:上传预览+缩略图+水印实例

AD:真正免费,域名+虚机+企业邮箱=0元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn