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根目录生成一个裁切的图像文件
所以本章说明的是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根目录生成一个裁切的图像文件
相关文章推荐
- Thinkphp ajax异步提交的方法总结
- phpdesigner 的配置
- PHP文件操作
- 用Zend OPCache提高PHP的性能
- PHP实现公钥加密机制
- PHP libevent扩展安装
- 【笔记】Yii2 - Gii使用:Module Generator
- YII2调用当前控制器方法
- PHP异步请求 POST和GET方式
- 【笔记】Yii2 - Gii使用:配置
- php整数判断
- php的DES加密算法
- WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL作用
- php 计算字符串长度
- 升级到php7相关问题,日请求过亿QQ会员活动平台PHP7升级实践
- thinkphp查询一周内的新闻排行10条
- php 利用转转法去除重复数组
- php file_put_contents时出现Permission denied的警告
- matplotlib核心剖析
- PHP的self::与static::,new self()和new static()之分