上传图片_创建文件夹_生成缩略图_添加水印

前台

 <!--如果表单提交文件,必须先声明enctype="multipart/form-data"-->
 <form action="" method="post" enctype="multipart/form-data">
     <p>
         <label>封面图:</label>
         <input type="file"  name="pic" id="pic"/>
     </p>
     <p>
        <input class="button" type="submit" value="Submit" />
     </p>
 </form>

后台

if(!empty($_POST))
{
    if(!empty($_FILES['pic']['tmp_name']))
	{
	//创建文件夹来存放上传的图片,子文件夹的名字是日期
	$Y=date("Y",time());
	$M=date("m",time());
	$D=date("d",time());
	if(!file_exists("uploads/$Y/$M/$D"))
		{
		    //mkdir不能跨文件夹创建文件夹,如果想需要7777(权限)
		    mkdir("uploads/$Y/$M/$D",7777,true);
		}
    //上传图片的命名	
	//上传文件的名称包括后缀名
	$file=$_FILES['pic']['name'];
	//获取其后缀,字符串截取。
	//strrpos() 函数查找字符串在另一个字符串中最后一次出现的位置。substr() 函数返回字符串的一部分
	$files_type=substr($file,strrpos($file,'.'));
	//重新定义名字,由时间戳和4个随即数组成
	$beforname=time().mt_rand(1111,9999);
	$file_name=$beforname.$files_type;
	//新文件路径
	$file_path="uploads/$Y/$M/$D/".$file_name;
	//将上传的文件移动到新位置。
	move_uploaded_file($_FILES['pic']['tmp_name'],$file_path);
	//数据库存入文件路径,以便读取。
	$_POST['pic']=$file_path;
	//缩略图的名称;与文件名关联
	$thumname="uploads/$Y/$M/$D/".$beforname.'_thum'.$files_type;
	//把缩略图路径存入数据库
	$_POST['pic_thumb']=thumb($file_path,$thumb_img =$thumname );
	//给图片添加水印
	water($file_path,$water_file ='watermark.png');
	}	

}

图片处理函数

<?php
/**
 * 生成验证码
 * @param int $num:字符数
 * @param int $size:大小
 * @param int $width:宽度
 * @param int $height:高度
 */
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
	if(empty($width)){
		$width = $num * $size * 4 / 5 + 5;
	}
	if(empty($height)){
		$height = $size + 10;
	}
	//组装随机字符
	// 去掉了 0 1 O l 等
	$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
	$code = '';
	for ($i = 0; $i < $num; $i++) {
		$code .= $str[mt_rand(0, strlen($str)-1)];
	}
	// 画图像
	$im = imagecreatetruecolor($width, $height);
	// 定义要用到的颜色
	$back_color = imagecolorallocate($im, 235, 236, 237);
	$boer_color = imagecolorallocate($im, 118, 151, 199);
	$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
	// 画背景
	imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
	// 画边框
	imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
	// 画干扰线
	for($i = 0;$i < 5;$i++) {
		$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
		imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
	}
	// 画干扰点
	for($i = 0;$i < 50;$i++) {
		$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
		imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
	}
	// 画验证码
	imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\\WINDOWS\\Fonts\\simsun.ttc', $code);
	$_SESSION["VerifyCode"]=$code;//用session保存$code,方便验证
	header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
	header("Content-type: image/png;charset=utf8");
	ob_clean();//ob_clean这个函数的作用就是用来丢弃输出缓冲区中的内容,如果你的网站有许多生成的图片类文件,那么想要访问正确,就要经常清除缓冲区。
	imagepng($im);
	imagedestroy($im);
}

/**
 * 生成缩略图
 * @param string $filename:源文件路径
 * @param string $thumb_img:缩略图路径
 * @return string 缩略图路径
 */
function thumb($filename,$thumb_img = 'test.jpg'){
	//获取原图
	$img=imagecreatefromjpeg($filename);
	//获取图片的宽度及高度
	$img_width=imagesx($img);
	$img_height=imagesy($img);
	//缩略图的宽度与高度
	$new_img_width=$img_width/4;
	$new_img_height=$img_height/4;
	//生成新图片(缩略图)
	$new_img=imagecreatetruecolor($new_img_width,$new_img_height);
	//创建新的图像对象(缩略图)
	imagecopyresized($new_img,$img,0,0,0,0,$new_img_width,$new_img_height,$img_width,$img_height);
	//imagecopyresized函数拷贝源图像的全部或部分并调整大小
	//生成图片
	imagejpeg($new_img,$thumb_img);
	//销毁图像
	imagedestroy($new_img);
	//返回缩略图
	return $thumb_img;
}

/**
 * 生成水印
 * @param string $dst_file:源文件
 * @param string $water_file:水印图片路径
 */
function water($dst_file,$water_file = 'watermark.png'){
	//获取原图
	$dst_im = imagecreatefromjpeg($dst_file);
	//获取原图信息
	//$dst_info = getimagesize($dst_file);
	/*
	Array
	(
		[0] => 75 //宽
		[1] => 150 //高
		[2] => 2 //图像类型的标记 1 = GIF,2 = JPG,3 = PNG 详见手册
		[3] => width="75" height="150" //html标记,用于html
		[bits] => 8 //
		[channels] => 3 //
		[mime] => image/jpeg //类型
	)
	*/
	//获取水印图
	$water_im = imagecreatefrompng($water_file);
	//获取水印图信息
	//$water_info = getimagesize($water_file);
	$alpha =90;//透明度
	//生成水印图
	//imagecopymerge($dst_im,$water_im,100,100,0,0,100,100,$alpha);//水印图留有白底
	imagecopy($dst_im,$water_im,100,100,0,0,100,100);//水印图无法透明,但不会留白底
	imagepng($dst_im,$dst_file);
	//销毁图像
	imagedestroy($dst_im);
	imagedestroy($water_im);
}



转载于:https://my.oschina.net/php2014/blog/205650

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值