#1 zhgzzy
index.php加入require(SP_PATH."/Extensions/functions.php");
funtion.php放到Extensions下
使用方法:
resizeImage($upics,$upics,580,580,$ext);//缩图
imageWaterMark($upics,9,'images/link.png',0,0); //加水印
function resizeImage($imagePath, $thumb, $width = 400, $height = 400, $ext) //缩图
{
list($imageWidth, $imageHeight) = getimagesize($imagePath);
$size=getimagesize($imagePath);
switch($size[2]) //判断图片类型根据类型创建图片
{
case 1:
$imagePath = imagecreatefromgif($imagePath);
break;
case 2:
$imagePath = imagecreatefromjpeg($imagePath);
break;
case 3:
$imagePath = imagecreatefrompng($imagePath);
break;
}
if ($width && ($imageWidth < $imageHeight))
{
$width = ($height / $imageHeight) * $imageWidth;
}
else
{
$height = ($width / $imageWidth) * $imageHeight;
}
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $imagePath, 0, 0, 0, 0, $width, $height, $imageWidth, $imageHeight);
if ($ext == "png"){
ImagePNG($image, $thumb);
imagedestroy($image);
}elseif($ext == "gif"){
ImageGif($image, $thumb);
imagedestroy($image);
}else{
ImageJpeg($image, $thumb);
imagedestroy($image);
}
}
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$xOffset=0,$yOffset=0) //加水印
{
$isWaterImage = FALSE;
//读取[du qu]水印文件[wen jian]
if(!empty($waterImage) && file_exists($waterImage)) {
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//取得水印图片的宽
$water_h = $water_info[1];//取得水印图片的高
switch($water_info[2]) { //取得水印图片的格式
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:return 1;
}
}
//读取[du qu]背景图片
if(!empty($groundImage) && file_exists($groundImage)) {
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];//取得背景图片的宽
$ground_h = $ground_info[1];//取得背景图片的高
switch($ground_info[2]) { //取得背景图片的格式
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:return 1;
}
} else {
return 2;
}
//水印位置[wei zhi]
if($isWaterImage) { //图片水印
$w = $water_w;
$h = $water_h;
$label = "图片的";
}
if( ($ground_w < $w) || ($ground_h < $h) ) {
return 3;
}
$posX = $ground_w - $w;
$posY = $ground_h - $h;
//设定图像[tu xiang]的混色模式[mo shi]
imagealphablending($ground_im, true);
if($isWaterImage) { //图片水印
imagecopy($ground_im, $water_im, $posX + $xOffset, $posY + $yOffset, 0, 0, $water_w,$water_h);
}
//生成水印后的图片
@unlink($groundImage);
switch($ground_info[2]) {//取得背景图片的格式
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default: return 6;
}
//释放[shi fang]内存[nei cun]
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
//
return 0;
}
?>
2011-05-31 13:56:20