thinkphp jquery剪切图片 Jcrop

运用的是jquery中的插件,温馨提示请下载好js。如有不懂之处请参照上片帖子 上传照片 或者留言

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>适友注册</title>
<link rel="stylesheet" type="text/css" href="__TEST__/css/login.css">
</head>

<script language="javascript" src="__TEST__/js/jquery-1.7.1.js"></script>  

<link rel="stylesheet" href="__TEST__/css/jquery.Jcrop.css">
<script src="__TEST__/js/jquery.Jcrop.js"></script>

<script>

 function tail_photo(){
 var jcrop_api, boundx, boundy;
         $j('#tailor').Jcrop({
minSize: [48,48],
setSelect: [0,0,190,190],
        onChange: updatePreview,
        onSelect: updatePreview,
onSelect: updateCoords,
        aspectRatio: 1
      },
function(){
        // Use the API to get the real image size
        var bounds = this.getBounds();
        boundx = bounds[0];
        boundy = bounds[1];
        // Store the API in the jcrop_api variable
        jcrop_api = this;
    });
function updateCoords(c)
{
var old_width = $j('#old_width0').val();
var rx =  old_width /410;
       // alert(old_width+"asd");
            width= Math.round(rx * c.w) + 'px';
            height= Math.round(rx * c.h) + 'px';
            marginLeft=  Math.round(rx * c.x) + 'px';
            marginTop= Math.round(rx * c.y) + 'px';
$j('#x').attr("value",marginLeft); //距离左侧的距离
$j('#y').attr("value",marginTop);//距离上边的距离
$j('#w').attr("value",width);//宽度
$j('#h').attr("value",height);//高度
};


      function updatePreview(c){
        if (parseInt(c.w) > 0)
        {
          var rx = 48 / c.w;
          var ry = 48 / c.h;


          $j('#preview').css({
            width: Math.round(rx * boundx) + 'px',
            height: Math.round(ry * boundy) + 'px',
            marginLeft: '-' + Math.round(rx * c.x) + 'px',
            marginTop: '-' + Math.round(ry * c.y) + 'px'
          });
        }
   {
          var rx = 165 / c.w;
          var ry = 165 / c.h;
          $j('#preview2').css({
            width: Math.round(rx * boundx) + 'px',
            height: Math.round(ry * boundy) + 'px',
            marginLeft: '-' + Math.round(rx * c.x) + 'px',
            marginTop: '-' + Math.round(ry * c.y) + 'px'
          });
        }
      };
}
});
 
 var have_photo="{$num['have_photo']}";
  var photo="{$num['photo']}"
 
$j('#img_up').attr('src',photo);  
 
  function sub_upload1(){
ima_hidd=$j('#ima_hidden').val();
x=$j('#x').val();
y=$j('#y').val();
w=$j('#w').val();
h=$j('#h').val();
$j.post('../../Home/up_photo',{ima_hidden:ima_hidd,x:x,y:y,w:w,h:h},function (data){

if(have_photo==0){
$j('#img_up').attr('src','/'.data);
$j('#up_photo').removeClass();
 }
$j('#ima').css('display','none');
$j('#aa0').css('display','');
$j("#title_photo0").val(data);
//$j( "#uploader" ).dialog( "close" );
});

util.has_thumbnail = false;
</script>

 <body>

<div id="ima" style="display:none">
               <div style="width:395px;"> <img id='tailor' class="tailor" src="/public/image/20120904/nishishui/1346756269.jpg" style="width:410px"></div>               
    <form id="form2" action="__APP__/Home/up_photo" method="post">
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
        <input type="hidden" id="ima_hidden" name="ima_hidden" value="" />
 <p id="uploader_finished" class="btn small refer " style=" margin-top:10px;"><a href="#" id="" class="" style=""  target="" tabindex="" οnclick="sub_upload1();return false;"></a></p>
                <p id="uploader_startover_button" class="btn small cancel " style="margin-left:135px; margin-top:-30px; margin-bottom:10px" ><a href="#" id="" class="cl_photo" style=""  target="" tabindex=""></a></p>
</form>

</body>

</html>

//php中的代码

 public function qup(){
$data['question']=$_POST['question1'];
$q1a1=$_POST['q1a1'];
$q1a2=$_POST['q1a2'];
$q1a3=$_POST['q1a3'];
$q1a4=$_POST['q1a4'];
$data['keywords']=$_POST['keywords'];
$direction=$_POST['direction'];
$qustion=M('common_question');
$sql = "select question FROM cup_common_question WHERE question LIKE '%".$_POST['keywords']."%'";
$list = $qustion->query($sql);
if(count($direction)==1){
$data['ques_nature']=$direction[0];
}else{
$data['ques_nature']=$direction[0].','.$direction[1];
}
if($q1a3!=null){
$data['ques_option']=$q1a1.'-'.$q1a2.'-'.$q1a3;
}else{
$data['ques_option']=$q1a1.'-'.$q1a2;
}
if($q1a3!=null && $q1a4!=null){
$data['ques_option']=$q1a1.'-'.$q1a2.'-'.$q1a3.'-'.$q1a4;
}
if($data['ques_nature']!=null){
$qustion->data($data)->add();
$sql="select last_insert_id()";
$id=$qustion->query($sql);
}
$this->assign("clist",$list);
$this->assign('qup_id',$id[0]['last_insert_id()']);
$this->display();
}
 
public function up_photo(){
 
$image =substr($_POST['ima_hidden'],2,strlen($_POST


['ima_hidden']));

$res =thumb($image,$_POST['x'],$_POST['y'],$_POST['w'],$_POST


['h'],false,1);
if($res == false){
echo "裁剪失败";
}elseif(is_array($res)){
echo $res['big'];
//加水印
import("ORG.Util.Image");
$Image = new Image();
$nickname=nickname();
            $Image->water($res['big'],'/public/image/1336954430057.png'); 
$User = M('common_user');
$user1 = M("User_dynamic");
// 根据表单提交的POST数据创建数据对象
$data['user_id']=session('user_id');
$data['photos']="/".$res['big'];
$arr=array("photo"=>"/".$res


['big'],"have_photo"=>'1');
$num=$User->where("id='".session


('user_id')."'")->field('photo,have_photo')->find();
//dump($num);
if($num['have_photo']==0){
$User->where("id='".session


('user_id')."'")->setField($arr);
$data['big_photos'] = $_POST['ima_hidden'];
$up_photos= M('user_photos');
$up_photos->add($data);


}else{
$data['big_photos'] = $_POST['ima_hidden'];
$up_photos= M('user_photos');
$up_photos->add($data);     
}
$data['type']="1";
$data['type']=date('Y-m-d H:i:s');
$sql = "update cup_user_dynamic set type='1' , date_time=now() where user_id='".session('user_id')."'";
$user1->query($sql);
echo $res['small'];
}

}

 function getImageInfo($img) {
        $imageInfo = getimagesize($img);
        if( $imageInfo!== false) {
            $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
            $imageSize = filesize($img);
            $info = array(
                "width" =>$imageInfo[0],
                "height" =>$imageInfo[1],
                "type" =>$imageType,
                "size" =>$imageSize,
                "mime" =>$imageInfo['mime'],
            );
            return $info;
        }else {
            return false;
        }
    }


/**
     +----------------------------------------------------------
     * 生成缩略图
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $image  原图
     * @param string $type 图像格式
     * @param string $thumbname 缩略图文件名
     * @param string $maxWidth  宽度
     * @param string $maxHeight  高度
     * @param string $position 缩略图保存目录
     * @param boolean $interlace 启用隔行扫描
     * @param boolean $is_save 是否保留原图
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
 
    function thumb($image,$x,$y,$w,$h,$is_save=true,$suofang=0,$type='',$maxWidth=500,$maxHeight=500,$interlace=true){
        // 获取原图信息
       $info  = getImageInfo($image);
         if($info !== false) {
            $srcWidth  = $info['width'];
            $srcHeight = $info['height'];
            $type = empty($type)?$info['type']:$type;
$type = strtolower($type);
            $interlace  =  $interlace? 1:0;
            unset($info);
            if ($suofang==1) {
                $width  = $srcWidth;
                $height = $srcHeight;
            } else {
                $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
                if($scale>=1) {
                    // 超过原图大小不再缩略
                    $width   =  $srcWidth;
                    $height  =  $srcHeight;
                }else{
                    // 缩略图尺寸
                    $width  = (int)($srcWidth*$scale);//147
                    $height = (int)($srcHeight*$scale);//199
                }
            }
            // 载入原图
            $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
            $srcImg     = $createFun($image);


            //创建缩略图
            if($type!='gif' && function_exists('imagecreatetruecolor'))
                $thumbImg = imagecreatetruecolor($width, $height);
            else
                $thumbImg = imagecreate($width, $height);


            // 复制图片
            if(function_exists("ImageCopyResampled"))
                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
            else
                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,  $srcWidth,$srcHeight);
            if('gif'==$type || 'png'==$type) {
                //imagealphablending($thumbImg, false);//取消默认的混色模式
                //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
                $background_color  =  imagecolorallocate($thumbImg,  0,255,0);  //  指派一个绿色
imagecolortransparent($thumbImg,$background_color);  //  设置为透明色,若注释掉该行则输出绿色的图
            }
            // 对jpeg图形设置隔行扫描
            if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
            //$gray=ImageColorAllocate($thumbImg,255,0,0);
            //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
            // 生成图片
            $imageFun = 'image'.($type=='jpg'?'jpeg':$type); 
$length = strlen("00.".$type) * (-1);
$_type = substr($image,-4);
$length = ($type != $_type ? $length+1 : $length);
            //裁剪
            if ($suofang==1) {

$thumbname01 = substr_replace($image,"01.".$type,$length);//大头像
//$thumbname02 = substr_replace($image,"02.".$type,$length);//小头像
$imageFun($thumbImg,$thumbname01,100);
//$imageFun($thumbImg,$thumbname02,100);


                $thumbImg01 = imagecreatetruecolor(190,195);
                imagecopyresampled($thumbImg01,$thumbImg,0,0,$x,$y,190,195,$w,$h);
$thumbImg02 = imagecreatetruecolor(48,48);
                //imagecopyresampled($thumbImg02,$thumbImg,0,0,$_POST['x'],$_POST['y'],48,48,$_POST['w'],$_POST['h']);


$imageFun($thumbImg01,$thumbname01,100);
//$imageFun($thumbImg02,$thumbname02,100);
// unlink($image);
imagedestroy($thumbImg01);
imagedestroy($thumbImg02);
imagedestroy($thumbImg);
imagedestroy($srcImg);


//return array('big' => $thumbname01 , 'small' => $thumbname02);//返回包含大小头像路径的数组
return array('big' => $thumbname01);
            }else{
if($is_save == false){//缩略图覆盖原图,缩略图的路径还是原图路径
$imageFun($thumbImg,$image,100);
}else{
$thumbname03 = substr_replace($image,"03.".$type,$length);//缩略图与原图同时存在,
$imageFun($thumbImg,$thumbname03,100);


imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname03 ;//返回缩略图的路径,字符串
}
}


         }
         return false;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值