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

【php基础】 php 图像处理类

2014-03-27 20:20 405 查看

PHP图像处理类

公共方法

方法名注释参数
verify生成验证码int $length 验证码长度 介于 1-10之间的数字

int $type 验证码类型 0->纯数字 1->纯英文

2->数字英文混合
randNum生成随机数字int $length 数字的位数

返回值 string $result 返回的结果
randChar生成随机字符串 int $length 字符串的长度

返回值 string $result 生成的字符串
randNumAndChar生成中英文混合的字符串int $length 字符串的长度

返回值 string $result 生成的随机字符串
thumbnail缩略图生成string $source 原图路径

int $weight 缩略图的宽度

int $height 缩略图的高度

string $des 缩略图的保存路径
water对图片打水印

如果水印图片的宽高之一大于原图片,则返回false

如果如果没有指定水印的透明度,则默认为80

没有指定处理后图片保存位置,则会覆盖原图片

如果没有指定水印的透明度,则默认为80

如果没有指定水印位置,则默认为随机位置
string $resource 被打水印图片地址

string $water 水印图片地址

string $savename 处理后图片保存地址

int $alpha 水印图片透明度

int $position 水印位置【0->中间位置, 1->左上角,

2->右上角, 3->左下角, 4->右下角, 5->随机位置】
getImageInfo取得图像信息string $image 图像文件名
getError返回错误信息 返回值 string 错误信息

源码:

<?php
/**
*图片处理类
* 图片裁切
* 图片添加水印
* 生成验证码
* 生成缩略图
*/
class Image{
static private $error;
/**
* 生成验证码
* @长度 int $length  介于 1-10之间的数字
* @类型 int $type   0->纯数字   1->纯英文   2->数字英文混合
*/
static public function verify($length,$type=2)
{
//根据$length 和 $type 生成随机串
switch($type){
case 0:$rand = self::randNum($length);break;
case 1:$rand = self::randChar($length);break;
case 2:$rand = self::randNumAndChar($length);break;
default:$rand = self::randNumAndChar($length);break;
}
if(false === $rand){
return false;
}
//session记录随机串
@session_start();
$_SESSION['verify'] = $rand;
//生成画布
//高度为30  宽度为80
$height = 22;
$width = 48;
$verify=imagecreatetruecolor($width,$height);
$white = imagecolorallocate($verify, 0xFF, 0xFF, 0xFF);
imagefill($verify, 0, 0, $white);//置换背景颜色
//将随机串写入画布
$blue=imagecolorallocate($verify,43,51,204);
$red=imagecolorallocate($verify,208,44,44);
$green=imagecolorallocate($verify,51,255,204);
//布置干扰线
imagedashedline($verify,rand(0,$width),0,rand(0,$width),$height,$red);
imagedashedline($verify,rand(0,$width),0,rand(0,$width),$height,$blue);
imagedashedline($verify,rand(0,$width),0,rand(0,$width),$height,$blue);
imageline($verify,0,rand(0,$height),$width,rand(0,$height),$green);
//添加文字,英文
imagestring($verify,5,rand(2,($width/$length)),rand(1,$height/3),$rand,$blue);
imagedashedline($verify,rand(0,$width),0,rand(0,$width),$height,$blue);
imagedashedline($verify,rand(0,$width),0,rand(0,$width),$height,$blue);
header("Content-type: image/jpeg");
imagejpeg($verify);
}

/**
* 生成随机数字
* @param int $length 数字的位数
* @return string  $result  返回的结果
*/
static public function randNum($length)
{
if($length <= 0){
self::$error = '生成随机字符串的长度不能小于等于0';
return false;
}
$result ='';
while($length-- >0){
$num = mt_rand(1,200);
$num = $num%10;
if(0 == $num){
$length++;
continue;
}else{
$result .= $num;
}
}
return $result;
}
/**
* 生成随机字符串
* @param int $length   字符串的长度
* @return string  $result  生成的字符串
*/
static public function randChar($length)
{
if($length <= 0){
self::$error = '生成随机字符串的长度不能小于等于0';
return false;
}
$result ='';
while($length-- >0){
$num = mt_rand(1,200);
$num = $num%26;
if(14 == $num){
$length++;
continue;
}else{
$result .= chr($num%26+97);
}
}
return  $result;
}
/**
* 生成中英文混合的字符串
* @param int $length   字符串的长度
* @return string  $result  生成的随机字符串
*/
static public function randNumAndChar($length)
{
if($length <= 0){
self::$error = '生成随机字符串的长度不能小于等于0';
return false;
}
$result = '';
while($length-- >0){
$num = mt_rand(1,200);
if($num>100){
$num = $num%26;
if(14 == $num){
$length++;
continue;
}else{
$result .= chr($num%26+97);
}
}else{
$num= $num%10;
if(0==$num){
$length++;
continue;
}else{
$result .= $num;
}
}
}
return $result;
}

/**
* 缩略图生成
* @param string $source   原图路径
* @param int    $weight   缩略图的宽度
* @param int    $height   缩略图的高度
* @param string $des      缩略图的保存路径
*/
static public function thumbnail($source, $width=null, $height=null, $savePath, $saveName=null)
{
//获取原图信息
if(is_file($source)){
$info = self::getImageInfo($source);
if(false === $info){
selef::$error = '获取图片信息失败';
return false;
}
}else{
selef::$error = '图片不存在';
return false;
}

if(is_null($width) && is_null($height)){
self::$error = '需要指定宽度和高度中的至少一个';
return false;
}elseif(is_null($width)){
$width = ($info['width']*$height)/$info['height'];
}elseif(is_null($height)){
$height = ($info['height']*$width)/$info['width'];
}
//生成画布
$newImage = imagecreatetruecolor($width,$height);

//加载原图片
$functionName = "imagecreatefrom".$info['type'];
$sourImage = $functionName($source);

imagecopyresampled($newImage, $sourImage, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);

//检测保存目录是否存在且可写
if(!is_dir($savePath)){
if(!mkdir($savePath,0777,true)){
selef::$error  =  '上传目录'.$savePath.'不存在';
return false;
}
}elseif(!is_writable($savePath)){
selef::$error  =  '上传目录'.$savePath.'不可写';
return false;
}
//如果要保存的名称为null
if(is_null($saveName)){
$saveName = time().'.'.$info['type'];
}
imagejpeg($newImage,$savePath.DIRECTORY_SEPARATOR.$saveName);
}

/**
* 对图片打水印
* 如果水印图片的宽高之一大于原图片,则返回false
* 如果没有指定处理后图片保存位置,则会覆盖原图片
* 如果没有指定水印的透明度,则默认为80
* 如果没有指定水印位置,则默认为随机位置
* @param string $resource  被打水印图片地址
* @param string $water     水印图片地址
* @param string $savename  处理后图片保存地址
* @param int $alpha        水印图片透明度
* @param int $position     水印位置【0->中间位置, 1->左上角, 2->右上角, 3->左下角, 4->右下角, 5->随机位置】
*/
static public function water($resource,$water,$savename=null,$alpha=80,$position=5){
//判断源文件和水印文件是否存在,不存在则返回false
if(!file_exists($resource) || !file_exists($water)){
self::$error = '缺少源文件或者水印文件';
return false;
}
//如果透明度 不是【数字或者数字字符串】或者 数字大于100
//则设置为80
if(!is_numeric($alpha) || $alpha >100){
$alpha = 80;
}
//获取文件信息
$resourceInfo = self::getImageInfo($resource);
$waterInfo    = self::getImageInfo($water);
//如果获取文件信息失败,则返回false
if(false === $resourceInfo || false === $waterInfo){
self::$error = '获取文件信息失败';
return false;
}
//如果水印图片的宽高之一大于原图片,则返回false
if($resourceInfo['width'] < $waterInfo['width'] || $resourceInfo['height'] < $waterInfo['height']){
self::$error = '源文件的宽高小于水印图片宽高';
return false;
}
//计算水印的位置
switch($position){
case 0:$positionArr = self::positionMiddle($resourceInfo, $waterInfo);break;
case 1:$positionArr = self::positionLeftTop($resourceInfo, $waterInfo);break;
case 2:$positionArr = self::positionRightTop($resourceInfo, $waterInfo);break;
case 3:$positionArr = self::positionLeftBottm($resourceInfo, $waterInfo);break;
case 4:$positionArr = self::positionRightBottom($resourceInfo, $waterInfo);break;
case 5:$positionArr = self::positionRand($resourceInfo, $waterInfo);break;
default:$positionArr = self::positionRand($resourceInfo, $waterInfo);break;
}

//创建图像资源
$sCreateFun = "imagecreatefrom" . $resourceInfo['type'];
$sImage = $sCreateFun($resource);
$wCreateFun = "imagecreatefrom" . $waterInfo['type'];
$wImage = $wCreateFun($water);

////设定图像的混色模式
imagealphablending($wImage, true);

//生成混合图像
imagecopymerge($sImage, $wImage, $positionArr['left'], $positionArr['top'], 0, 0, $waterInfo['width'], $waterInfo['height'], $alpha);

//输出图像
$ImageFun = 'Image' . $resourceInfo['type'];
//如果没有给出保存文件名,默认为原图像名
if (is_null($savename)) {
$savename = $resource;
@unlink($resource);
}
//保存图像
$ImageFun($sImage, $savename);
imagedestroy($sImage);
}

/**
* 中间水印的位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionMiddle($resourceInfo,$waterInfo){
$left = ($resourceInfo['width'] - $waterInfo['width'])/2;
$top  = ($resourceInfo['height'] - $waterInfo['height'])/2;
return array('left'=>$left, 'top'=>$top);
}
/**
* 左上角水印的位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionLeftTop($resourceInfo,$waterInfo){
return array('left'=>0, 'top'=>0);
}
/**
* 右上角水印的位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionRightTop($resourceInfo,$waterInfo){
$left = $resourceInfo['width'] - $waterInfo['width'];
return array('left'=>$left, 'top'=>0);
}
/**
* 左下角水印的位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionLeftBottm($resourceInfo,$waterInfo){
$top  = $resourceInfo['height'] - $waterInfo['height'];
return array('left'=>0, 'top'=>$top);
}
/**
* 右下角水印的位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionRightBottom($resourceInfo,$waterInfo){
$left = $resourceInfo['width'] - $waterInfo['width'];
$top  = $resourceInfo['height'] - $waterInfo['height'];
return array('left'=>$left, 'top'=>$top);
}
/**
* 随机生成水印位置
* @param array $resourceInfo   原图片信息数组
* @param array $waterInfo      水印图片信息数组
*/
static private function positionRand($resourceInfo,$waterInfo){
$left = $resourceInfo['width'] - $waterInfo['width'];
$top  = $resourceInfo['height'] - $waterInfo['height'];
return array('left'=>mt_rand(0,$left), 'top'=>mt_rand(0,$top));
}

/**
* 取得图像信息
* @static
* @access public
* @param string $image 图像文件名
* @return mixed
* come from ThinkPHP
*/
static function getImageInfo($img) {
$imageInfo = getimagesize($img);
if ($imageInfo !== false) {
$imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
$imageSize = filesize($img);
$info = array(
"width" => $imageInfo[0],
"height" => $imageInfo[1],
"type" => $imageType,
"size" => $imageSize,
"mime" => $imageInfo['mime']
);
return $info;
} else {
return false;
}
}

/**
* 返回错误信息
* @return string self::$error;
*/
static public function getError(){
return self::$error;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: