thinkphp_圖片上傳裁剪功能_已遷移

準備材料php

  1. THINKPHPhtml

  2. jQuery表單插件前端

  3. cropper 裁剪插件html5


思路:  利用THINKPHP上傳文件類與圖片裁剪類,前臺想辦法組合參數給後臺  那怎麼樣能夠異步提交文件呢 關鍵就是 jquery表單插件了 jquery


後臺準備git

thinkphp 上傳類  http://www.kancloud.cn/manual/thinkphp/1876 github

thinkphp  裁剪類 http://www.kancloud.cn/manual/thinkphp/1878 ajax


前臺準備thinkphp

jQuery表單插件  http://malsup.com/jquery/form/#code-samples  中文文檔 http://www.cnblogs.com/linzheng/archive/2010/11/17/1880288.html json

裁剪插件  http://www.jqcool.net/image-cropper.html   https://github.com/fengyuanchen/cropper 

即時預覽圖片  (不會上傳文件)

/** 
* 從 file 域獲取 本地圖片 url 
*/ 
function getFileUrl(sourceId) { 
var url; 
if (navigator.userAgent.indexOf("MSIE")>=1) { // IE 
url = document.getElementById(sourceId).value; 
} else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox 
url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
} else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome 
url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
} 
return url; 
} 



/** 
* 將本地圖片 顯示到瀏覽器上 
*/ 
function preImg(sourceId, targetId) { 
var url = getFileUrl(sourceId); 
var imgPre = document.getElementById(targetId); 
imgPre.src = url; 
} 


//觸發事件調用
 preImg(this.id,'imgPre');


個人實現代碼

前端

<form id="avatar_form" action="{:U('Picture/ajaxAvatar')}" method="post" enctype="multipart/form-data">
    <div class="txsc">
     <input type="hidden" class="avatar-data" name="avatar_data"><!-- 頭像裁剪數據 -->
    <div class="close"><img src="__IMG__/icon12.png" width="15" height="16" /></div>
        <div class="tx_top">
            <p>
                <span class="l">上傳新頭像</span>
                <span class="file">選擇文件<input id="imgOne"  type="file" name="avatar_file"></span>
                <span class="l">未選擇文件</span>
            </p>
          <p class="font12 color4">支持格式:jpg、jpeg、png、gif格式,大小不超過5M</p>
        </div>
      <div class="tx_img">
            <P>
            <div class="container avatar_img">
                <img id="imgPre" src="__IMG__/img12.jpg" width="291" />
            </div>
            </P>
            <P class="fangda"><span id="enlarge"><img  src="__IMG__/icon19.png" width="18" height="18" /></span><span id="shrink" style="border-left:#c1c1c1 solid 1px;"><img src="__IMG__/icon18.png" width="18" height="18" /></span></P>
        </div>
        <div class="tx_an"><span id="avatar_sumbit" class="button l">肯定</span><span id="resetUplode" class="button1 r">從新上傳</span></div>
        <div class="tx_ts"><p>舒適提示:</p><p>禁止上傳違反中華人民共和國相關法律的照片,若上傳,相關
    法律後果有我的承擔</p></div>
    </div>
</form>
<script>
    

    
//頭像上傳裁剪  start
$(function(){

    //打開或關閉彈出層
    $(document).ready(function() {
        $('.toux').click(function(){
            $('.txsc').fadeIn(300);
        })
        $('.txsc .close').click(function(){
            $('.txsc'). fadeOut(300);
        })

    });

    /** 
    * 從 file 域獲取 本地圖片 url 
    */ 
    function getFileUrl(sourceId) { 
    var url; 
    if (navigator.userAgent.indexOf("MSIE")>=1) { // IE 
    url = document.getElementById(sourceId).value; 
    } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox 
    url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
    } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome 
    url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
    } 
    return url; 
    } 


    /** 
    * 將本地圖片 顯示到瀏覽器上 
    */ 
    function preImg(sourceId, targetId) { 
    var url = getFileUrl(sourceId); 
    var imgPre = document.getElementById(targetId); 
    imgPre.src = url; 
    } 
    //先清除原來的圖片
    $("#imgOne").click(function(){
        $('.avatar_img').html('<img id="imgPre" src="__IMG__/img12.jpg" width="291" />');
    })
    //點擊從新上傳至關於點擊了上傳文件
    $("#resetUplode").click(function(){
        $("#imgOne").click();
    })
    $("#imgOne").live('change',function(){
        preImg(this.id,'imgPre');
        //圖片裁剪初始化
        $('.container > img').cropper({
          aspectRatio: 16 / 9,
          rotateControls:true,
          zoomable: true,
          crop: function(data) {
            //$('.avatar-data').
           var x = data.x;
           var y = data.y;
           var width = data.width;
           var height = data.height;
           var avatarData = '{"x":'+x+',"y":'+y+',"height":'+height+',"width":'+width+',"rotate":0}';
           $('.avatar-data').val(avatarData);
          }
        });
    });
    //放大
    $("#enlarge").click(function(){
        $('.container > img').cropper('zoom', +0.1);

    })
    //縮小
    $("#shrink").click(function(){
        $('.container > img').cropper('zoom', -0.1);
    })

    /* 異步上傳圖片 */
    $("#avatar_sumbit").click(function(){
            $('#avatar_form').ajaxSubmit(function(data){
                //console.log(data);
                //替換當前圖片的路徑
                $(".toux").attr('src',data.crop_path);
                //接收圖片ID後,放到當前頁面中
                $("input[name='head_img']").val(data.pic_id);
                //關閉當前彈出層
                $('.txsc'). fadeOut(300);
            });

        return false;
    });
});
//頭像上傳裁剪  end
</script>


PHP端

控制器中的方法

	public function ajaxAvatar(){
		$avatarArr = json_decode(I('post.avatar_data'),1);
		$picModel = D('picture');
		$info = $picModel->ajaxUpload($_FILES,$avatarArr);
		$this->ajaxReturn($info);
	}
	
圖片model層代碼
	/**
	 * 圖片上傳
	 * @param  array  $files   要上傳的圖片列表(一般是$_FILES數組)
	 * @param  array  $setting 圖片上傳配置
	 * @param  string $driver  圖片驅動名稱
	 * @param  array  $config  圖片驅動配置
	 * @return array           圖片上傳成功後的信息
	 */
	public function upload($files, $setting, $driver = 'Local', $config = null){
		/* 上傳文件 */
		$Upload = new \Think\Upload($setting, $driver, $config);
		$info   = $Upload->upload($files);

		/* 設置文件保存位置 */
		$this->_auto[] = array('location', 'Ftp' === $driver ? 1 : 0, self::MODEL_INSERT);

		if($info){ //文件上傳成功,記錄文件信息
			foreach ($info as $key => &$value) {
				/* 已經存在文件記錄 */
				if(isset($value['id']) && is_numeric($value['id'])){
					$value['path'] = substr($setting['rootPath'], 1).$value['savepath'].$value['savename'];	//在模板裏的url路徑
					continue;
				}

				$value['path'] = '.'.substr($setting['rootPath'], 1).$value['savepath'].$value['savename'];	//在模板裏的url路徑
				/* 記錄文件信息 */
				if($this->create($value) && ($id = $this->add())){
					$value['id'] = $id;
				} else {
					//TODO: 文件上傳成功,可是記錄文件信息失敗,需記錄日誌
					unset($info[$key]);
				}
			}
			return $info; //文件上傳成功
		} else {
			$this->error = $Upload->getError();
			return false;
		}
	}

	/*更新或添加*/
	public function update($data)
	{
		$data = $this->create($data);
		if(!$data){ return false;}
		return empty($data['id']) ? $this->add() : $this->save();
	}

	/**
	 *異步上傳並裁剪圖片
	 * @param $file   $_FILES 文件信息
	 * @param $pic    要裁剪信息
	 * @param $pre    裁剪後圖片前綴
	 * @return array  圖片裁剪以後的地址  原圖片地址   圖片ID
	 */
	public function ajaxUpload($file,$picArr,$pre='crop'){
		$config = array(
		    'maxSize'    =>    3145728,
		    'rootPath'   =>    './Uploads/Picture/',
		    'savePath'   =>    '',
		    'saveName'   =>    array('uniqid',''),
		    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
		    'autoSub'    =>    true,
		    'subName'    =>   array('date','Y-m-d'),
		);
		//能夠直接調用系統的上傳類
		$info = $this->upload($file,$config);

		//截圖略縮圖
		$image = new \Think\Image();
		$image->open($info['avatar_file']['path']);
		$crop_path = './Uploads/Picture/'.$info['avatar_file']['savepath'].$pre.'_'.$info['avatar_file']['savename'];
		$image->crop($picArr['width'], $picArr['height'],$picArr['x'],$picArr['y'])->save($crop_path);
		//原圖片路徑
		$info['org_path'] = $info['avatar_file']['path'];
		//裁剪後圖片路徑
		$info['crop_path'] = $crop_path;
		//圖片ID
		$info['pic_id'] = $info['avatar_file']['id'];
		return $info;
	}
相關文章
相關標籤/搜索