GD库 生成缩略图、文字水印、图片水印的函数封装
程序员文章站
2022-06-12 16:02:18
...
慕课网后 端开发PHP教程-->PHP进阶篇--GD库图像处理-->http://www.imooc.com/learn/701-->第3章 图像常用操作
<?php
header("content-type:text/html;charset=utf-8");
/*
指定缩放比例
最大宽度和高度,等比例缩放
可以对缩略图文件添加前缀
选择是否删除缩略图的源文件
*/
/**
* 返回图片信息
* @param [string] $filename [文件地址名]
* @return [array] 图片的宽度、高度、扩展名、创建和输出的函数名
*/
function getImageInfo($filename){
if(@!$info=getimagesize($filename)){
exit('文件不是真实图片');
}
$fileInfo['width']=$info[0];
$fileInfo['height']=$info[1];
$mime=image_type_to_mime_type($info[2]);
$createFun=str_replace('/','createfrom',$mime);
$outFun=str_replace('/','',$mime);
$fileInfo['createFun']=$createFun;
$fileInfo['outFun']=$outFun;
$fileInfo['ext']=strtolower(image_type_to_extension($info[2]));
return $fileInfo;
}
/**
* 形成缩略图
* @param string $filename 文件名
* @param string $dest 缩略图保存路径
* @param string $pre 文件名前缀
* @param [type] $dst_w 最大宽度
* @param [type] $dst_h 最大高度
* @param float $scale 缩放比例
* @param boolean $delSource 是否删除源文件的标志
* @return string 图片保存路径及文件名
*/
function thumb($filename,$dest='thumb',$pre='thumb_',$dst_w=null,$dst_h=null,$scale=0.5,$delSource=false){
$fileInfo=getImageInfo($filename);
$src_w=$fileInfo['width'];
$src_h=$fileInfo['height'];
//如果指定最大宽度和高度,按照等比例缩放进行处理
if(is_numeric($dst_w)&&is_numeric($dst_h)){
$ratio_orig = $src_w/$src_h;
if ($dst_w/$dst_h > $ratio_orig) {
$dst_w = $dst_h*$ratio_orig;
} else {
$dst_h = $dst_w/$ratio_orig;
}
}else{
$dst_w=ceil($src_w*$scale);
$dst_h=ceil($src_h*$scale);
}
$src_image=$fileInfo['createFun']($filename);
$dst_image=imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled($dst_image,$src_image,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
//检测目标目录是否存在
if($dest&&!file_exists($dest)){
mkdir($dest,0777,true);
}
$randNum=mt_rand(1000,9999);
$dstName=$pre.$randNum.$fileInfo['ext'];
$destination=$dest?$dest.'/'.$dstName:$dstName;
$fileInfo['outFun']($dst_image,$destination);
imagedestroy($src_image);
imagedestroy($dst_image);
if($delSource){
@unlink($filename);
}
return $destination;
}
/**
* 文字水印
* @param string $filename 文件名
* @param string $fontfile 字体文件
* @param string $text 水印文字
* @param string $dest 缩略图保存路径
* @param string $pre 文件名前缀
* @param boolean $delSource 是否删除源文件的标志
* @param integer $r 红
* @param integer $g 绿
* @param integer $b 蓝
* @param integer $alpha 透明度
* @param integer $size 文字大小
* @param integer $angle 倾斜角度
* @param integer $x 水印位置x
* @param integer $y 水印位置y
* @return string 图片保存路径及文件名
*/
function waterText($filename,$fontfile,$text="慕课网",$dest="water",$pre="waterText_",$delSource=false,$r=255,$g=0,$b=0,$alpha=60,$size=30,$angle=0,$x=0,$y=30){
$fileInfo=getImageInfo($filename);
$image=$fileInfo['createFun']($filename);
$color=imagecolorallocatealpha($image,$r,$g,$b,$alpha);
imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
//检测目标目录是否存在
if($dest&&!file_exists($dest)){
mkdir($dest,0777,true);
}
$randNum=mt_rand(1000,9999);
$dstName=$pre.$randNum.$fileInfo['ext'];
$destination=$dest?$dest.'/'.$dstName:$dstName;
$fileInfo['outFun']($image,$destination);
imagedestroy($image);
if($delSource){
@unlink($filename);
}
return $destination;
}
/**
* [waterPic description]
* @param string $dstName 源文件名
* @param string $srcName 水印图片文件名
* @param integer $pos 水印图片位置--九宫格
* @param string $dest 缩略图保存路径
* @param string $pre 文件名前缀
* @param integer $pct 合并程度
* @param boolean $delSource 是否删除源文件的标志
* @return string 图片保存路径及文件名
*/
function waterPic($dstName,$srcName,$pos=0,$dest="water",$pre="waterPic_",$pct=50,$delSource=false){
$dstInfo=getImageInfo($dstName);
$srcInfo=getImageInfo($srcName);
$dst_im=$dstInfo['createFun']($dstName);
$src_im=$srcInfo['createFun']($srcName);
$src_w=$srcInfo['width'];
$src_h=$srcInfo['height'];
$dst_w=$dstInfo['width'];
$dst_h=$dstInfo['height'];
switch($pos){
case 0://左上角
$x=0;
$y=0;
break;
case 1:
$x=($dst_w-$src_w)/2;
$y=0;
break;
case 2://右上角
$x=$dst_w-$src_w;
$y=0;
break;
case 3:
$x=0;
$y=($dst_h-$src_h)/2;
break;
case 4:
$x=($dst_w-$src_w)/2;
$y=($dst_h-$src_h)/2;
break;
case 5:
$x=$dst_w-$src_w;
$y=($dst_h-$src_h)/2;
break;
case 6://左下角
$x=0;
$y=$dst_h-$src_h;
break;
case 7:
$x=($dst_w-$src_w)/2;
$y=$dst_h-$src_h;
break;
case 8://右下角
$x=$dst_w-$src_w;
$y=$dst_h-$src_h;
break;
default://
$x=0;
$y=0;
break;
}
imagecopymerge($dst_im,$src_im,$x,$y,0,0,$src_w,$src_h,$pct);
//检测目标目录是否存在
if($dest&&!file_exists($dest)){
mkdir($dest,0777,true);
}
$randNum=mt_rand(1000,9999);
$name=$pre.$randNum.$dstInfo['ext'];
$destination=$dest?$dest.'/'.$name:$name;
$dstInfo['outFun']($dst_im,$destination);
imagedestroy($dst_im);
imagedestroy($src_im);
if($delSource){
@unlink($dstName);
}
return $destination;
}
?>
<?php
require_once 'image.func.php';
$filename='image/1.jpg';
$fontfile="fonts/7.ttf";
//thumb($filename);
//waterText($filename,$fontfile);
$dstName="image/6.jpg";
$srcName="image/logo.jpg";
waterPic($dstName,$srcName,4);
?>
上一篇: php结构语句与打印