PHP判断上传文件是否为图片且保存为唯一命名

<?php
header("content-type:text/html;charset=utf-8");
$arr_ext = array("jpg","png","gif");
$index = strrpos($_FILES['upload']['name'],".");
$ext = substr($_FILES['upload']['name'],$index+1);
if(!in_array($ext,$arr_ext))
{
 echo "上传的文件不是图像";
 exit();
}else
{
 $src = $_FILES['upload']['tmp_name'];
 $dst = "./upload/".uniqid().".$ext";
 move_uploaded_file($src,$dst);
 echo "上传文件成功";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>判断上传文件是否为图片,为保存为唯一命名</title>
</head>
<body>
 <form name="form1" method="post" enctype="multipart/form-data" action="upload2.php">
 上传图片:<input type="file" name="upload">
 <input type="submit" value="提交表单">
 </form>
</body>
</html>

 

Continue Reading

PHP上传单个文件并保存至服务器

<?php
header("content-type:text/html;charset=utf-8");
if($_FILES['upload']['error']!=0)
{
 echo "上传文件发生错误";
 exit();
}
if($_FILES['upload']['size']>2*1024*1024)
{
 echo "上传文件大小超出要求";
 exit();
}
$src = $_FILES['upload']['tmp_name'];
$dst = "./upload/".$_FILES['upload']['name'];
if(!move_uploaded_file($src,$dst))
{
 echo "上传文件移动保存失败";
 exit();
}
echo "上传文件成功";
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>文件上传</title>
</head>
<body>
 <form name="form1" method="post" enctype="multipart/form-data" action="upload.php">
 上传图片:<input type="file" name="upload">
 <input type="submit" value="提交表单">
 </form>
</body>
</html>

Continue Reading

PHP随机字母数字验证码

<?php
header("content-type:text/html;charset=utf-8");
//变量初始化
$str = "";
$filename = "./images/captcha.png";
$font = 5;
//生成随机验证码字符串
//array_merge合并一个或多个数组
$arr_list = array_merge(range('A','Z'),range(0,9));
//array_rand从数组中随机取出一个或多个单元
$index_list = array_rand($arr_list,8); //随机取8个下标
shuffle($index_list); //打乱数组
foreach($index_list as $value)
{
$str .= $arr_list[$value];
}
//创建画布
$img = imagecreatefrompng($filename);
//分配颜色,随机字体颜色
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//获取画布和字体尺寸
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
//计算字符串的起始坐标
$x = ($imgWidth -$fontWidth*strlen($str))/2;
$y = ($imgHeight - $fontHeight)/2;
//将验证码字符串写入画布上
imagestring($img,$font,$x,$y,$str,$color);
//输出图像
header("content-type:image/png");
imagepng($img,'./images/captcha_new.png');
imagedestroy($img);
?>

Continue Reading

PHP图像字符水印居中

<?php
//让图像上字符串居中
header("content-type:text/html;charset = utf-8");
//(1)创建画布
$filename = "./images/02.jpg";
$img = imagecreatefromjpeg($filename);
$str = "WELCOME to liuguofeng.com";
$font = 5;
//(2)分配颜色:
$color1 = imagecolorallocate($img,255,0,255);
$color2 = imagecolorallocate($img,255,0,255);
//(3)获取画布宽度和高度
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
//(4)获取字体尺寸
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
//(5)计算字符串的起始坐标
$x = ($imgWidth-$fontWidth*strlen($str))/2;
$y = ($imgHeight-$fontHeight)/2;
//(6)写入一个字符串
imagestring($img,$font,$x,$y,$str,$color1);
header("content-type:image/jpeg");
imagejpeg($img);
//(7)关闭图像
imagedestroy($img);
?>

Continue Reading
Close Menu