PHP 添加水印

<?php 
require 'config.inc.php';
if (isset($_POST['send'])) {
	$img = $_FILES['img'];
	if (is_uploaded_file($img['tmp_name']) && is_array($img)) {
		$name = $img["name"];
		$type = $img["type"];
		$size = $img["size"];
		if ( $size > ceil("530000") ) {
			echo "<script>alert('图片大小超过500kb,无法上传')</script>";
			break;
		}
		$cur_time = date("Y-m-d H:i:s");
		$tmp_name = $img["tmp_name"];
		//求出上传图片的名称后缀
		$ext_name = strtolower(substr($name, strrpos($name, '.'), strlen($name)));
		$new_name='jzy_' . time() . rand(1000,9999) . $ext_name ;
		$store_path = ROOT_PATH . UPDIR . $new_name;
		//求上传图片高宽
		$imginfo = getimagesize($tmp_name);
		$width = $imginfo[0];
		$height = $imginfo[1];
		 //添加图片水印             
		switch($ext_name) {
			case '.gif':
				$dst_im = imagecreatefromgif($tmp_name);
				break;
			case '.jpg':
				$dst_im = imagecreatefromjpeg($tmp_name);
				break;
			case '.png':
				$dst_im = imagecreatefrompng($tmp_name);
				break;
			default:
				echo "<script>alert('上传图片格式不正确,请上传gif jpeg或png格式的图片!')</script>";
				break;
		}
		$src_im = imagecreatefrompng(IMG_PATH . 'logo.png');
		//求水印图片高宽
		$src_imginfo = getimagesize(IMG_PATH . 'logo.png');
		$src_width = $src_imginfo[0];
		$src_height = $src_imginfo[1];
		//求出水印图片的实际生成位置
		$src_x = $width - $src_width - 10;
		$src_y = $height - $src_height - 10;
		//新建一个真彩色图像
		$nimage = imagecreatetruecolor($width, $height);               
		//拷贝上传图片到真彩图像
		imagecopy($nimage, $dst_im, 0, 0, 0, 0, $width, $height);          
		//按坐标位置拷贝水印图片到真彩图像上
		imagecopy($nimage, $src_im, $src_x, $src_y, 0, 0, $src_width, $src_height);
		//分情况输出生成后的水印图片
		switch($ext_name) {
			case '.gif':
				imagegif($nimage, $tmp_name);
				break;
			case '.jpg':
				imagejpeg($nimage, $tmp_name);
				break;
			case '.png':
				imagepng($nimage, $tmp_name);
				break;     
		}
		//释放资源 
		imagedestroy($dst_im);
		imagedestroy($src_im);
		unset($imginfo);
		unset($src_imginfo);
		//移动生成后的图片
		@move_uploaded_file($tmp_name, ROOT_PATH.UPDIR . $new_name);
	}
}
?>
<form action="" name="frm" method="post" enctype="multipart/form-data">
	<input type="file" name="img" /> <input type="submit" value="上传" name="send" />
</form>


你可能感兴趣的:(PHP,File,ext,Path,IM,DST)