项目开发中部分方法记录
1.关于文件夹与子文件删除与创建
//$a为路径
if(!is_dir($a)){//判断文件夹是否存在
mkdir($a,0777,true);//创建文件夹
}
//删除文件名
public function deldir($a) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
2.关于图片绘制文字与加水印
//参数实例
//$imageDefault 必须有
$imageDefault = array(
'url'=>$logcode,
'left'=>300,
'top'=>1240,
'right'=>0,
'bottom'=>0,
'width'=>150,
'height'=>150,
'opacity'=>100
);
//图片绘制扩展
$config['image'][]= array(
'url'=>$userimg,
'left'=>335,
'top'=>165,
'right'=>0,
'bottom'=>0,
'width'=>80,
'height'=>80,
'opacity'=>100,
'stream'=>true
);
//文字定位
$textDefault= array(
'text'=>“11111”,
'left'=> 310,
'top'=>330,
'fontSize'=>25, //字号
'fontColor'=>'0,0,0', //字体颜色
'angle'=>0,
'fontPath'=>'./ffsm/statics/ffsm/xydd/mnjkt.ttf'//字体
);
$config['text'][]= array(
'text'=>$row['nickname'],
'left'=> 300,
'top'=>190,
'fontSize'=>15, //字号
'fontColor'=>'0,0,0', //字体颜色
'angle'=>0,
'fontPath'=>'./ffsm/statics/ffsm/xydd/mnjkt.ttf'
);
$background = './statics/tgcode/dc.png';//海报最底层得背景
$filename='/statics/fenxiang/'.date("Ymd").'/'.time().round(0,100).'.jpg';
//#这两个相当于声明吧,没有的话部分有失败的问题
$config['image'][]['urls'] = '';
$config['text'][]['url']="";
//### mod_order::getbgqrcode($imageDefault,$textDefault,$background,$filename,$config);
//调用方法
public function getbgqrcode($imageDefault,$textDefault,$background,$filename="",$config=array()){
//如果要看报什么错,可以先注释调这个header
// if(empty($filename)) header("content-type: image/png");
//背景方法
$backgroundInfo = getimagesize($background);
$ext = image_type_to_extension($backgroundInfo[2], false);
$backgroundFun = 'imagecreatefrom'.$ext;
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); //背景宽度
$backgroundHeight = imagesy($background); //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
//处理了图片
if(!empty($config['image'])){
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault,$val);
if($val['stream']){
//此处争对远程加载过慢问题把其作为字符串图像流
//如果传的是字符串图像流
//此处可以解决微信图片路径绘画时过慢问题
$raw = util::curl_file_get_contents($val['url']);
$im = imagecreatefromstring($raw);
$info = getimagesizefromstring($raw);
$function = 'imagecreatefromstring';
$res = $function($raw);
}else{
$info = getimagesize($val['url']);
$function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
$res = $function($val['url']);
}
$resWidth = $info[0];
$resHeight = $info[1];
//建立画板 ,缩放图片至指定尺寸
$canvas=imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
//放置图像
imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);
//左,上,右,下,宽度,高度,透明度
}
}
//处理文字
if(!empty($config['text'])){
foreach ($config['text'] as $key => $val) {
$val = array_merge($textDefault,$val);
list($R,$G,$B) = explode(',', $val['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
}
}
// var_dump(!empty($filename));die;
//生成图片
if(!empty($filename)){
$res = imagejpeg ($imageRes,$filename,100);
//保存到本地
imagedestroy($imageRes);
}else{
//直接打印图片显示,以图片形式输出
header("content-type: image/jpeg");
imagejpeg ($imageRes);
echo imagedestroy($imageRes);die;
}
}
3.关于二维码生成
//此方法为中间带log的二维码图片
$value="http://www.baidu.com";根据自己的要生成的处理
$logo = './statics/favicon.png';//准备好的logo图片就是网站图标的那种
$logcode='./statics/fenxiang/qrcode_cg.png';//生成图片存放地址
$uid=1;
if(!file_exists($logcode)){//
$this->getcode($value,$uid,$logo,$logcode);
}
public function getcode($value,$uid,$logo,$cgcod){
include './core/phpqrcode.php';//此文加网上找找就有
$errorCorrectionLevel = 'M';//容错级别L|7%/M|15%//Q|25%/H|30%
$matrixPointSize = 6;//生成图片大小
QRcode::png($value,'./statics/tgcode/qrcode_'.$uid.'.png', $errorCorrectionLevel, $matrixPointSize, 2);//生成的没有log的二维码
$QR = './ffsm/statics/tgcode/qrcode_'.$uid.'.png';//已经生成的原始二维码图
if ($logo !== FALSE) {
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//二维码图片宽度
$QR_height = imagesy($QR);//二维码图片高度
$logo_width = imagesx($logo);//logo图片宽度
$logo_height = imagesy($logo);//logo图片高度
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新组合图片并调整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//输出图片
imagepng($QR,$cgcod);
}
4.关于邮件发送
//qq需要开启IMAP/SMTP服务 会有授权码
$Subject="找回密码";
$code = rand(100000,999999); // 验证码
$Subject="找回密码";
$email=“xxxx@qq.com”;
$result=mod_order::sendtoemail($email, $Subject,$code);
public static function sendtoemail($email,$Subject,$code){
//调用文件可以网上找到
require './ffsm/PHPMailer/Exception.php';
require './ffsm/PHPMailer/PHPMailer.php';
require './ffsm/PHPMailer/SMTP.php';
//查询是否存在,有就让其失效
$appemailcode['email']=$email;
$appemailcode['type']=$Subject;
$appemailcode['addtime']=time();//添加时间
$appemailcode['expiretime']=time()+1800;//过期时间
$appemailcode['code']=$code;
// 实例化PHPMailer核心类
$mail = new PHPMailer\PHPMailer\PHPMailer();
// 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可默认关闭debug调试模式
$mail->SMTPDebug = 2;
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 链接qq域名邮箱的服务器地址
$mail->Host = 'smtp.qq.com';
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// 设置ssl连接smtp服务器的远程服务器端口号
$mail->Port = 465;
// 设置发送的邮件的编码
$mail->CharSet = 'UTF-8';
// 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
$mail->FromName = '********';
// smtp登录的账号 QQ邮箱即可
$mail->Username = '********@qq.com'; // 你的QQ邮箱
// smtp登录的密码 使用生成的授权码
$mail->Password = 'XXXX';
// 设置发件人邮箱地址 同登录账号
$mail->From = '********@qq.com'; // 你的QQ邮箱
// 邮件正文是否为html编码 注意此处是一个方法
$mail->isHTML(true);
// 设置收件人邮箱地址
$mail->addAddress($email);
// 添加多个收件人 则多次调用方法即可
//$mail->addAddress('********@163.com');
// 添加该邮件的主题
$mail->Subject =$Subject;
$a="<div class='card'><div class='head'><span style='position:relative;top:10px'>测试</span></div><div class='container'><h5><b style='position: relative;top:-20px'>亲爱的用户,您正在进行邮箱验证,的验证码为:</b></h5> <p><h1 style='position: relative;top:-20px;background: #f5f6f7;width: 200px; font-weight:bold;margin:0 auto'>".$code."</h1></p> <hr/></div><div class='end'>本邮件为系统文件,不需要回复<br/>本版权归测试所有</div></div><style>.card {box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);transition: 0.3s;border-radius: 5px;margin: 0 auto;}.card:hover {box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);}img {border-radius: 5px 5px 0 0;}.container {padding: 2px 16px;}h1{text-align: center;}.head{background: black;height: 40px;text-align: center;color:white;}hr{color: #9d9d9d;}.end{color: #999;margin-top: -10px;padding: 5px;font-size: 12px;text-align: center;}</style>";//此处可以写html
// 添加邮件正文
$mail->Body = $a;
// 为该邮件添加附件
//$mail->addAttachment('./example.pdf');
// 发送邮件 返回状态
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$status = $mail->send();
return $status;
}