您的位置:首页 > 编程语言 > PHP开发

Thinkphp 图像处理GD库结合jcrop插件总结

2016-06-03 14:01 471 查看
Thinkphp 图像处理使用的是GD库和imagick库,默认使用GD库,imagick怎么使用我还不知道。

所以本章说明的是GD库怎么用

GD库的类库文件在\ThinkPHP\Library\Think\Image.class.php文件

前端代码如下

<img id="imghead" border=0 src='__PUBLIC__/default/images/meinv.jpg' />
<form action="__URL__/imgjc" method="post" onsubmit="return checkCoords();">
<input type="text" id="x" name="x" hidden />
<input type="text" id="y" name="y" hidden />
<input type="text" id="w" name="w" hidden />
<input type="text" id="h" name="h" hidden />
<input type="text" name="imgurl" id="imgurl" />
<input type="submit" value="提交">
</form>
JS代码如下
<script language="Javascript">
jQuery(function(){
jQuery('#imghead').Jcrop({
aspectRatio: 1,
onSelect: updateCoords, //选中区域时执行对应的回调函数
onChange: updateCoords, //选择区域变化时执行对应的回调函数
bgFade: true,
bgOpacity: .2,
setSelect: [ 60, 70, 540, 330 ],
minSize:[200,200],
});
});
function updateCoords(c)
{
jQuery('#x').val(c.x); //选中区域左上角横
jQuery('#y').val(c.y); //选中区域左上角纵坐标
//jQuery("#x2").val(c.x2); //选中区域右下角横坐标
//jQuery("#y2").val(c.y2); //选中区域右下角纵坐标
jQuery('#w').val(c.w); //选中区域的宽度
jQuery('#h').val(c.h); //选中区域的高度
};
function checkCoords()
{
if (parseInt(jQuery('#w').val())>0) return true;
alert('请选择需要裁切的图片区域.');
return false;
};
jQuery('doucment').ready(function(e) {
var t=jQuery('#imghead').attr('src');
jQuery('#imgurl').val(t);
});

</script>


CSS代码
<style type="text/css">
#preview{width:100px;height:100px;border:1px solid #000;overflow:hidden;}
#imghead{filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}
</style>


使用的时候在后台方法如下

public function imgjc(){
$image = new \Think\Image();
$imgurl=substr(I('post.imgurl',''),1);//去掉URL第一个/
$imgtype=substr(I('post.imgurl',''),-3);//取得图片类型
$image->open($imgurl);
$imgname=uniqid();

$img_url=('post.');
$img_x=I('post.x','');
$img_y=I('post.y','');
$img_w=I('post.w','');
$img_h=I('post.h','');

$re=$image->crop($img_w, $img_h,$img_x,$img_y)->save('./'.$imgna
4000
me.'.'.$imgtype);
var_dump($imgtype);
}


该方法会在www根目录生成一个裁切的图像文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: