给图片加水印和改变图片尺寸的类
时间:2008-10-09
来源:互联网
美是什么?美是是一种感觉。而代码之美就是:看上去很舒服,而且功能强大,但是又并不难懂的代码就是美丽的代码!!
我也发个图片处理类来选美吧!!请大家多多支持~
[php]
<?php
/**
* Image
*
* @author liexusong
* @copyright 2008
* @version v1.0
* @access public
*/
class Image {
var $imageResource = NULL;//处理的图片的资源指针
var $target = NULL;
var $enableTypes = array();//允许的图片类型
var $imageInfo = array();
var $createFunc = '';
var $imageType = NULL;
/**
* 构造器
*
* @param string $image
* @return Image
*/
function Image($image = NULL) {
//获取GD库允许的类型
if(imagetypes() & IMG_GIF) {
$this->enableTypes[] = 'image/gif';
}
if(imagetypes() & IMG_JPEG) {
$this->enableTypes[] = 'image/jpeg';
}
if (imagetypes() & IMG_JPG) {
$this->enableTypes[] = 'image/jpg';
}
if(imagetypes() & IMG_PNG) {
$this->enableTypes[] = 'image/png';
}
//end
if($image != NULL) {
$this->setImage($image);
}
}
/**
* 设置图片给
*
* @param string $image
* @return boolean
*/
function setImage($image) {
if(file_exists($image) && is_file($image)) {//假如图片存在
$this->imageInfo = getimagesize($image);//取得图片的尺寸类型等
$img_mime = strtolower($this->imageInfo['mime']);//取得图片的mime类型
if(!in_array($img_mime, $this->enableTypes)) {
//假如GD库不支持这种类型,把他挂起
exit('系统不能操作这种图片类型.');
}
//这个switch块是用来选择处理图片的函数的
switch ($img_mime) {
case 'image/gif':
$link = imagecreatefromgif($image);
$this->createFunc = 'imagegif';
$this->imageType = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$link = imagecreatefromjpeg($image);
$this->createFunc = 'imagejpeg';
$this->imageType = 'jpeg';
break;
case 'image/png':
$link = imagecreatefrompng($image);
$this->createFunc = 'imagepng';
$this->imageType = 'png';
break;
default:
$link = 'unknow';
$this->imageType = 'unknow';
break;
}//end switch
if($link !== 'unknow') {
$this->imageResource = $link;
} else {
exit('不能处理这种图片类型.');
}
unset($link);
return true;
} else {
return false;
}
}
/**
*
* 会根据图片的类型自动设置header
* 不过要在任何输出之前调用,否则会出错
*
*/
function setHeader() {
switch ($this->imageType) {
case 'gif':
header('content-type:image/gif');
break;
case 'jpeg':
header('content-type:image/jpeg');
break;
case 'png':
header('content-type:image/png');
break;
default:
exit('Can not set header.');
break;
}
return true;
}
/**
* 这个方法可以改变图片的尺寸
*
* @param int $width 图片的宽度
* @param int $height 图片的高度,不设置会根据比例自动得到
* @return boolean
*/
function changeSize($width, $height = -1) {
if(!is_resource($this->imageResource)) {
exit('不能改变图片的尺寸,可能是你没有设置图片来源.');
}
$s_width = $this->imageInfo[0];
$s_height = $this->imageInfo[1];
$width = intval($width);
$height = intval($height);
if($width <= 0) exit('图片宽度必须大于零.');
if($height <= 0) {
$height = ($s_height / $s_width) * $width;
}
$this->target = imagecreatetruecolor($width, $height);
if(@imagecopyresized($this->target, $this->imageResource, 0, 0, 0, 0, $width, $height, $s_width, $s_height))
return true;
else
return false;
}
/**
* 给图片添加水印
*
* @param string $image 水印图片地址
* @param int $app 透明度
*/
function addWatermark($image, $app = 50) {
if(file_exists($image) && is_file($image)) {
//取得水印图片的信息
$s_info = getimagesize($image);
} else {
exit($image . '文件不存在.');
}
$r_width = $s_info[0];
$r_height = $s_info[1];
//假如水印图片大于目标图片
if($r_width > $this->imageInfo[0]) exit('水印图片必须小于目标图片');
if($r_height > $this->imageInfo[1]) exit('水印图片必须小于目标图片');
//end
//只支持3中类型的水印图片gif/jpg/png
switch ($s_info['mime']) {
case 'image/gif':
$resource = imagecreatefromgif($image);
break;
case 'image/jpeg':
case 'image/jpg':
$resource = imagecreatefromjpeg($image);
break;
case 'image/png':
$resource = imagecreatefrompng($image);
break;
default:
exit($s_info['mime'] .'类型不能作为水印来源.');
break;
}//end switch
$this->target = &$this->imageResource;
imagecopymerge($this->target, $resource, $this->imageInfo[0] - $r_width - 5, $this->imageInfo[1] - $r_height - 5, 0,0 ,$r_width, $r_height, $app);
imagedestroy($resource);
unset($resource);
}
/**
* 创建图片
*
* @param string $name
* @return boolean
*/
function create($name = NULL) {
$function = $this->createFunc;
if($this->target != NULL && is_resource($this->target)) {
if($name != NULL) {
$function($this->target, $name);
} else {
$function($this->target);
}
return true;
} else if($this->imageResource != NULL && is_resource($this->imageResource)) {
if($name != NULL) {
$function($this->imageResource, $name);
} else {
$function($this->imageResource);
}
return true;
} else {
exit('不能创建图片,原因可能是没有设置图片来源.');
}
}
/**
* 释放资源
*
*/
function free() {
if(is_resource($this->imageResource)) {
@imagedestroy($this->imageResource);
}
if(is_resource($this->target)) {
@imagedestroy($this->target);
}
}
}
?>
[/php]
我也发个图片处理类来选美吧!!请大家多多支持~
[php]
<?php
/**
* Image
*
* @author liexusong
* @copyright 2008
* @version v1.0
* @access public
*/
class Image {
var $imageResource = NULL;//处理的图片的资源指针
var $target = NULL;
var $enableTypes = array();//允许的图片类型
var $imageInfo = array();
var $createFunc = '';
var $imageType = NULL;
/**
* 构造器
*
* @param string $image
* @return Image
*/
function Image($image = NULL) {
//获取GD库允许的类型
if(imagetypes() & IMG_GIF) {
$this->enableTypes[] = 'image/gif';
}
if(imagetypes() & IMG_JPEG) {
$this->enableTypes[] = 'image/jpeg';
}
if (imagetypes() & IMG_JPG) {
$this->enableTypes[] = 'image/jpg';
}
if(imagetypes() & IMG_PNG) {
$this->enableTypes[] = 'image/png';
}
//end
if($image != NULL) {
$this->setImage($image);
}
}
/**
* 设置图片给
*
* @param string $image
* @return boolean
*/
function setImage($image) {
if(file_exists($image) && is_file($image)) {//假如图片存在
$this->imageInfo = getimagesize($image);//取得图片的尺寸类型等
$img_mime = strtolower($this->imageInfo['mime']);//取得图片的mime类型
if(!in_array($img_mime, $this->enableTypes)) {
//假如GD库不支持这种类型,把他挂起
exit('系统不能操作这种图片类型.');
}
//这个switch块是用来选择处理图片的函数的
switch ($img_mime) {
case 'image/gif':
$link = imagecreatefromgif($image);
$this->createFunc = 'imagegif';
$this->imageType = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$link = imagecreatefromjpeg($image);
$this->createFunc = 'imagejpeg';
$this->imageType = 'jpeg';
break;
case 'image/png':
$link = imagecreatefrompng($image);
$this->createFunc = 'imagepng';
$this->imageType = 'png';
break;
default:
$link = 'unknow';
$this->imageType = 'unknow';
break;
}//end switch
if($link !== 'unknow') {
$this->imageResource = $link;
} else {
exit('不能处理这种图片类型.');
}
unset($link);
return true;
} else {
return false;
}
}
/**
*
* 会根据图片的类型自动设置header
* 不过要在任何输出之前调用,否则会出错
*
*/
function setHeader() {
switch ($this->imageType) {
case 'gif':
header('content-type:image/gif');
break;
case 'jpeg':
header('content-type:image/jpeg');
break;
case 'png':
header('content-type:image/png');
break;
default:
exit('Can not set header.');
break;
}
return true;
}
/**
* 这个方法可以改变图片的尺寸
*
* @param int $width 图片的宽度
* @param int $height 图片的高度,不设置会根据比例自动得到
* @return boolean
*/
function changeSize($width, $height = -1) {
if(!is_resource($this->imageResource)) {
exit('不能改变图片的尺寸,可能是你没有设置图片来源.');
}
$s_width = $this->imageInfo[0];
$s_height = $this->imageInfo[1];
$width = intval($width);
$height = intval($height);
if($width <= 0) exit('图片宽度必须大于零.');
if($height <= 0) {
$height = ($s_height / $s_width) * $width;
}
$this->target = imagecreatetruecolor($width, $height);
if(@imagecopyresized($this->target, $this->imageResource, 0, 0, 0, 0, $width, $height, $s_width, $s_height))
return true;
else
return false;
}
/**
* 给图片添加水印
*
* @param string $image 水印图片地址
* @param int $app 透明度
*/
function addWatermark($image, $app = 50) {
if(file_exists($image) && is_file($image)) {
//取得水印图片的信息
$s_info = getimagesize($image);
} else {
exit($image . '文件不存在.');
}
$r_width = $s_info[0];
$r_height = $s_info[1];
//假如水印图片大于目标图片
if($r_width > $this->imageInfo[0]) exit('水印图片必须小于目标图片');
if($r_height > $this->imageInfo[1]) exit('水印图片必须小于目标图片');
//end
//只支持3中类型的水印图片gif/jpg/png
switch ($s_info['mime']) {
case 'image/gif':
$resource = imagecreatefromgif($image);
break;
case 'image/jpeg':
case 'image/jpg':
$resource = imagecreatefromjpeg($image);
break;
case 'image/png':
$resource = imagecreatefrompng($image);
break;
default:
exit($s_info['mime'] .'类型不能作为水印来源.');
break;
}//end switch
$this->target = &$this->imageResource;
imagecopymerge($this->target, $resource, $this->imageInfo[0] - $r_width - 5, $this->imageInfo[1] - $r_height - 5, 0,0 ,$r_width, $r_height, $app);
imagedestroy($resource);
unset($resource);
}
/**
* 创建图片
*
* @param string $name
* @return boolean
*/
function create($name = NULL) {
$function = $this->createFunc;
if($this->target != NULL && is_resource($this->target)) {
if($name != NULL) {
$function($this->target, $name);
} else {
$function($this->target);
}
return true;
} else if($this->imageResource != NULL && is_resource($this->imageResource)) {
if($name != NULL) {
$function($this->imageResource, $name);
} else {
$function($this->imageResource);
}
return true;
} else {
exit('不能创建图片,原因可能是没有设置图片来源.');
}
}
/**
* 释放资源
*
*/
function free() {
if(is_resource($this->imageResource)) {
@imagedestroy($this->imageResource);
}
if(is_resource($this->target)) {
@imagedestroy($this->target);
}
}
}
?>
[/php]
作者: liexusong 发布时间: 2008-10-09
新世纪成人用品商城是提供各类情趣用品、成人用品、性用品、情趣内衣、健慰器具、计生用品、同志用品等商品的网上购物精品网站。我们依托行业协会的鼎力支持,与众多厂家商家结成销售联盟,代理其产品在互联网上的直销业务。商城目前拥有国内外最为畅销的各类情趣用品成人用品等千余个品种数万件商品。我们承诺所有商品均为正品,货真价实绝无假货!我们建议您在网上购买此类商品时擦亮眼睛,选择正规专业的精品网站,让您买的放心!用的开心!更多商品请登陆商城网站:http://www.chengrensc.com/goindex.aspx查看详情
作者: 何茹鞭 发布时间: 2009-09-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28