项目中用到了一个上传头像的功能,需要进行无刷新的图片上传,并对上传后的图片进行用户要求的截图,无刷新上传我就不说了,用的Juploader,相信大家并不陌生,重点讲一下jcron和php配置实现图片的截取的功能,好了,言归正传。首先介绍一下jcron的用法,我就不一一解释了,我们只看最经常用的到截图功能:
$(function(){
$('#cropbox').Jcrop({
aspectRatio: 1,
onSelect: updateCoords
});
});
以上是控制,对哪个图片进行截图,“cropbox”是你要截取的img对象的id,“aspectRatio”控制等比例截取,“onSelect”的值是一个方法名,在选取时调用的方法
,个参数详情解释如下:
Option Name | Value Type | Description | Default |
---|---|---|---|
aspectRatio | decimal | Aspect ratio of w/h (e.g. 1 for square) | n/a |
minSize | array [ w, h ] | Minimum width/height, use 0 for unbounded dimension | n/a |
maxSize | array [ w, h ] | Maximum width/height, use 0 for unbounded dimension | n/a |
setSelect | array [ x, y, x2, y2 ] | Set an initial selection area | n/a |
bgColor | color value | Set color of background container | 'black' |
bgOpacity | decimal 0 - 1 | Opacity of outer image when cropping | .6 |
function updateCoords(c)
{
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
};
有了这个方法,可以在你截图是更新隐藏域中的坐标值,通过隐藏域把坐标信息传到后台。
ok,到此,前台已经告一段落,我们看后台的php代码。
后台php主要是根据前台传递的坐标,对原图进行截取,支持jpg,png,和gif三种图片格式,当然,你可以扩展他,使他支持更多的图片格式。
class Img_shot
{
private $filename;
private $ext;
private $x;
private $y;
private $x1;
private $y1;
private $width = 124;
private $height = 124;
private $jpeg_quality = 90;
/**
* 构造器
*
*
*/
public function __construct()
{
log_message('debug', "Img_shot Class Initialized");
}
/**
* 初始化截图对象
*@param filename 源文件路径全明
*@param width 截图的宽
*@param height 截图的高
*@param x 横坐标1
*@param y 纵坐标1
*@param x1 横坐标1
*@param y1 横坐标2
*
*/
public function initialize($filename,$x,$y,$x1,$y1)
{
if(file_exists($filename))
{
$this->filename = $filename;
$pathinfo = pathinfo($filename);
$this->ext = $pathinfo['extension'];
}
else
{
$e = new Exception('the file is not exists!',1050);
throw $e;
}
$this->x = $x;
$this->y = $y;
$this->x1 = $x1;
$this->y1 = $y1;
}
/**
* 生成截图
* 根据图片的格式,生成不同的截图
*/
public function generate_shot()
{
switch($this->ext)
{
case 'jpg':
return $this->generate_jpg();
break;
case 'png':
return $this->generate_png();
break;
case 'gif':
return $this->generate_gif();
break;
default:
return false;
}
}
/**
* 得到生成的截图的文件名
*
*/
private function get_shot_name()
{
$pathinfo = pathinfo($this->filename);
$fileinfo = explode('.',$pathinfo['basename']);
$filename = $fileinfo[0] . '_small.' . $this->ext;
return $pathinfo['dirname'] . '//m.sbmmt.com/m/' .$filename;
}
/**
* 生成jpg格式的图片
*
*/
private function generate_jpg()
{
$shot_name = $this->get_shot_name();
$img_r = imagecreatefromjpeg($this->filename);
$dst_r = ImageCreateTrueColor($this->width, $this->height);
imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y,
$this->width,$this->height,$this->x1,$this->y1);
imagejpeg($dst_r,$shot_name,$this->jpeg_quality);
return $shot_name;
}
/**
* 生成gif格式的图片
*
*/
private function generate_gif()
{
$shot_name = $this->get_shot_name();
$img_r = imagecreatefromgif($this->filename);
$dst_r = ImageCreateTrueColor($this->width, $this->height);
imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y,
$this->width,$this->height,$this->x1,$this->y1);
imagegif($dst_r,$shot_name);
return $shot_name;
}
/**
* 生成png格式的图片
*
*/
private function generate_png()
{
$shot_name = $this->get_shot_name();
$img_r = imagecreatefrompng($this->filename);
$dst_r = ImageCreateTrueColor($this->width, $this->height);
imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y,
$this->width,$this->height,$this->x1,$this->y1);
imagepng($dst_r,$shot_name);
return $shot_name;
}
}
接收到前台的坐标信息后,你可以实例化该类,用来生成图片,返回生成的图片的名称,你就可以使用啦。
截完图之后:
相关文章推荐
• 【活动】充值PHP中文网VIP即送云服务器• ZEND将与IBM合作开发PHP IDE/Framework_PHP教程• 理解PHP中的MVC编程之控制器_PHP教程• 不需要mod_rewrite直接使用php实现伪静态化页面_PHP教程• PHP设计模式漫谈之迭代器模式(2)_PHP教程• php 常用函数收藏(一)_PHP教程独孤九贱(3)_JavaScript视频教程
javascript是运行在浏览器上的脚本语言,连续多年,被评为全球最受欢迎的编程语言。是前端开发必备三大法器中,最具杀伤力。如果前端开发是降龙十八掌,好么javascript就是第18掌:亢龙有悔。没有它,你的前端生涯是不完整的。《php.cn独孤九贱(3)-JavaScript视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了javascript知识。
JavaScript教程127068次播放
独孤九贱(6)_jQuery视频教程
jQuery是一个快速、简洁的JavaScript框架。设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。 核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口;具有高效灵活的css选择器,并且可对CSS选择器进行扩展;拥有便捷的插件扩展机制和丰富的插件。兼容各种主流浏览器,如IE 6.0+、FF 1.5+、Safari 2.0+、Opera 9.0+等,是全球最流行的前端开发框架之一。PHP中文网根据最新版本,独家录制jQuery最新视频教程,回馈PHP中文网的新老用户。
jQuery教程105098次播放
jQuery与Ajax基础与实战
jQuery是最流行的JS函数库,封装了许多实用的功能,其中最引人入胜的就是Ajax。 jQuery中的Ajax操作,语法简单,操作方便,使Ajax从未如此轻松,前端人员从此不再为与服务器异步交互而发愁,本套课程,精选了最常用的几个方法,从基本的语法到每个参数,再到具体实例进行了全面的讲解。
AJAX教程5956次播放
Git教程(60分钟全程无废话版)
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。 Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。 Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持
JavaScript教程5157次播放