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 />
$('#upload').uploadify({<br />
'swf':'__PUBLIC__/assets/plugins/uploadify/uploadify.swf',<br />
'buttonText': '选择图片',<br />
'formData': { 'session_id':sid},<br />
'uploader': "{:U('uploadPic')}",<br />
'fileTypeDesc':'Image File',<br />
'fileTypeExts':'*.jpg; *.jpeg; *.gif; *.png',<br />
'auto' : true,<br />
'removeCompleted': false,<br />
onUploadSuccess: function(file, data, response) {<br />
$('#progress').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('.','_'); //生成唯一标示<br />
var html = '<li class="imageitem" id="'+id+'">';<br />
html+= '<input type="hidden" name="file[]" value="'+result.pic_path+'">'; //隐藏域,是为了把图片地址入库。。<br />
html+= '<input type="hidden" name="minifile[]" value="'+result.mini_pic+'">'; //隐藏域,是为了把图片地址入库。。<br />
html+= '<img style="max-width:90%" style="max-width:90%" src="__ROOT__/Uploads/'+result.pic_path+'" / alt="TP3.2:上传预览+缩略图+水印实例" >';<br />
html+= '<span class="txt">';<br />
html+= '<a title="删除" href=javascript:remove("'+result.pic_path+'","'+id+'");><img src="__PUBLIC__/assets/plugins/uploadify/remove.png" / alt="TP3.2:上传预览+缩略图+水印实例" >';<br />
html+= '<a title="设为封面" href=javascript:tocover("'+result.pic_path+'");><img src="__PUBLIC__/assets/plugins/uploadify/right.png" / alt="TP3.2:上传预览+缩略图+水印实例" >';<br />
html+= '';<br />
html+= '<em><input class="form-control" value="'+file.name+'">';<br />
html+= '';<br />
$('#image_result').append(html);<br />
},<br />
onUploadStart: function(){<br />
$('#progress').text('正在上传...'); //用户等待提示。<br />
},<br />
onInit: function (){ <br />
$("#upload-queue").hide(); //隐藏上传队列 <br />
} <br />
}); <br />
});<br />
function remove(file,id){<br />
alert('删除成功!'+"\r\n"+file);<br />
$('#'+id).remove();<br />
}<br />
function tocover(file){<br />
alert('设为封面成功'+"\r\n"+file);<br />
}<br />
function check(){<br />
if($('input[name="title"]').val()==""){<br />
toastr['error']('标题不能为空!');<br />
return false;<br />
}<br />
}<br />
<br />
</script><br>
</block>
AD:真正免费,域名+虚机+企业邮箱=0元