<?php
// ------------------------------------------------------------------------
// 程序功能:用户选择缩放和裁剪位置,裁剪后自动保存成大图和中图和小图,一般用于网站头像设置
// 作者:徐风 (QQ:377653)
// http://www.jieqi.com/
// ------------------------------------------------------------------------
$imgresize = new ImageResize(); //创建图片缩放和裁剪类
$imgresize->load('source.jpg'); //载入原始图片
$posary=explode(',', $_REQUEST['cut_pos']);
foreach($posary as $k=>$v) $posary[$k]=intval($v); //获得缩放比例和裁剪位置
if($posary[2]>0 && $posary[3]>0) $imgresize->resize($posary[2], $posary[3]); //图片缩放
//保存120*120的大图
$imgresize->cut(94, 125, intval($posary[0]), intval($posary[1]));
$imgresize->save('l.jpg');
//保存48*48的中图
$imgresize->resize(48, 48);
$imgresize->save('m.jpg');
//保存16*16的小图
$imgresize->resize(16, 16);
$imgresize->save('s.jpg', true);
header('Location: index.html');
//--------------------------------------------------------
/**
* 图片缩放和裁剪类
*/
class ImageResize
{
//源图象
var $_img;
//图片类型
var $_imagetype;
//实际宽度
var $_width;
//实际高度
var $_height;
//载入图片
function load($img_name, $img_type=''){
if(!empty($img_type)) $this->_imagetype = $img_type;
else $this->_imagetype = $this->get_type($img_name);
switch ($this->_imagetype){
case 'gif':
if (function_exists('imagecreatefromgif')) $this->_img=imagecreatefromgif($img_name);
break;
case 'jpg':
$this->_img=imagecreatefromjpeg($img_name);
break;
case 'png':
$this->_img=imagecreatefrompng($img_name);
break;
default:
$this->_img=imagecreatefromstring($img_name);
break;
}
$this->getxy();
}
//缩放图片
function resize($width, $height, $percent=0)
{
if(!is_resource($this->_img)) return false;
if(empty($width) && empty($height)){
if(empty($percent)) return false;
else{
$width = round($this->_width * $percent);
$height = round($this->_height * $percent);
}
}elseif(empty($width) && !empty($height)){
$width = round($height * $this->_width / $this->_height );
}else{
$height = round($width * $this->_height / $this->_width);
}
$tmpimg = imagecreatetruecolor($width,$height);
if(function_exists('imagecopyresampled')) imagecopyresampled($tmpimg, $this->_img, 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
else imagecopyresized($tmpimg, $this->_img, 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
$this->destroy();
$this->_img = $tmpimg;
$this->getxy();
}
//裁剪图片
function cut($width, $height, $x=0, $y=0){
if(!is_resource($this->_img)) return false;
if($width > $this->_width) $width = $this->_width;
if($height > $this->_height) $height = $this->_height;
if($x < 0) $x = 0;
if($y < 0) $y = 0;
$tmpimg = imagecreatetruecolor($width,$height);
imagecopy($tmpimg, $this->_img, 0, 0, $x, $y, $width, $height);
$this->destroy();
$this->_img = $tmpimg;
$this->getxy();
}
//显示图片
function display($destroy=true)
{
if(!is_resource($this->_img)) return false;
switch($this->_imagetype){
case 'jpg':
case 'jpeg':
header("Content-type: image/jpeg");
imagejpeg($this->_img);
break;
case 'gif':
header("Content-type: image/gif");
imagegif($this->_img);
break;
case 'png':
default:
header("Content-type: image/png");
imagepng($this->_img);
break;
}
if($destroy) $this->destroy();
}
//保存图片 $destroy=true 是保存后销毁图片变量,false这不销毁,可以继续处理这图片
function save($fname, $destroy=false, $type='')
{
if(!is_resource($this->_img)) return false;
if(empty($type)) $type = $this->_imagetype;
switch($type){
case 'jpg':
case 'jpeg':
$ret=imagejpeg($this->_img, $fname);
break;
case 'gif':
$ret=imagegif($this->_img, $fname);
break;
case 'png':
default:
$ret=imagepng($this->_img, $fname);
break;
}
if($destroy) $this->destroy();
return $ret;
}
//销毁图像
function destroy()
{
if(is_resource($this->_img)) imagedestroy($this->_img);
}
//取得图像长宽
function getxy()
{
if(is_resource($this->_img)){
$this->_width = imagesx($this->_img);
$this->_height = imagesy($this->_img);
}
}
//获得图片的格式,包括jpg,png,gif
function get_type($img_name)//获取图像文件类型
{
if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $img_name, $matches)){
$type = strtolower($matches[1]);
}else{
$type = "string";
}
return $type;
}
}
?>
没有合适的资源?快使用搜索试试~ 我知道了~
PHP&JS 头像设置功能
共15个文件
gif:7个
jpg:4个
html:1个
4星 · 超过85%的资源 需积分: 10 37 下载量 23 浏览量
2010-02-02
10:48:44
上传
评论 1
收藏 64KB RAR 举报
温馨提示
PHP 和JS的图片处理功能 PHP 和JS的图片处理功能 PHP 和JS的图片好用
资源推荐
资源详情
资源评论
收起资源包目录
setavatar.rar (15个子文件)
setavatar
drag.js 4KB
setavatar.php 4KB
l.jpg 3KB
source.jpg 51KB
m.jpg 1KB
s.jpg 827B
images
blank.gif 43B
track.gif 258B
grip.gif 587B
+c.gif 258B
_h.gif 255B
_c.gif 255B
sarrow.png 378B
+h.gif 258B
index.html 8KB
共 15 条
- 1
资源评论
- zhangfuxiang19902013-10-26不可使用,鉴定完毕、
- qinhe0032013-01-25用js 写的图片缩放裁剪,与设置头像还是有点差别,不过还是很有用
- xiaoqiutong2012-11-25不是我想要的功能,不过有参考价值,谢了
- Courage_Yeah2013-10-09效果还可以
xue0523
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功