文章介绍的PHP文件上传与上传图片加水印例子是分开来写的了,先是介绍文件上传代码,而后介绍了一个上传图片加水印代码,大家可以整理成一个类来方便调用了.
先来看一段简单的文件上传代码,html文件,主要是表单了上传文件的表单了,代码如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
-
- <!-- 此行定义字符集,在使用汉字的国家,字符集通常有 gb2312,utf-8两种字符集,设置不对会导致页面中文乱码
-
- 因为我的编辑器是utf-8的,所以这里我定义字符集为utf-8。如果你的字符集是gb2312可以修改charset值 -->
-
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
-
-
- <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
-
-
-
- <title>PHP上传实例</title>
-
-
-
- <script type="text/javascript">
-
- // 当页面加载完后执行里边的函数体
-
- // 因为页面的加载顺序是自上到下,否则你把JS移到页脚也可以
-
- window.onload = function(){
-
- // 给上传按钮添加鼠标单击事件
-
- document.forms[0].elements[1].onclick = function(){
-
- // 判断要上传的文件名是否为空
-
- if( document.forms[0].file.value == '' ){
-
- // 如果是空则弹出警告
-
- alert('请先选择文件');
-
- // 结束脚本运行
-
- return;
-
- }
-
-
-
- // 如果文件不为空则上传文件
-
- document.forms[0].submit();
-
- }
-
- }
-
- </script>
-
- </head>
-
- <body>
-
- <!-- 此行是定义form表单区域,上传文件必须用POST方式,还要添加 enctype="multipart/form-data" 属性
-
- action里的值为你要处理上传文件的页面,也可以用url或者相对路径 -->
-
- <form action="upload.php" method="post" enctype="multipart/form-data">
-
-
-
- <input type="file" name="file" value="选择要上传的文件" />
-
-
-
- <input type="button" value="上传" />
-
- </form>
-
- </body>
-
- </html>
php上传处理文件,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
- function alert($msg){
-
- return '<script type="text/javascript">alert("'.$msg.'");window.history.back(-1);</script>';
-
- }
-
-
-
- $allowType = array('image/jpeg','image/gif','image/jpg');
-
-
-
- $filePath = './uploadFileDir/';
-
-
-
- $file = $_FILES['file'];
-
-
-
- if( $file['error'] !== 0 ){
-
- exit(alert('文件上传错误'));
-
- }
-
-
-
- if( $file['size'] > 102400 ){
-
- exit(alert('文件过大'));
-
- }
-
-
-
- if( !in_array(mime_content_type($file['tmp_name']),$allowType) ){
-
- exit(alert('文件类型错误'));
-
- }
-
-
-
- if( !file_exists($filePath) && !mkdir($filePath,0777,true) ){
-
- exit(alert('创建目录错误'));
-
- }
-
-
-
- $filename = time().'_'.$file['name'];
-
-
- if( !copy($file['tmp_name'],$filePath.$filename) ){
-
- exit(alert('上传文件出错,请稍候重试'));
-
- }
-
-
- unlink($file['tmp_name']);
-
-
- echo alert('恭喜,上传文件['.$filename.']成功!');
- ?>
注意:如果你在上传中还带有其它单表字段名我们需要获取需要利用post接受才可以,否则你可能接受不到值.
完成以上步骤以后,你就可以给你上传的图片添加水印了,以下是我写的一个小DEMO水印类,代码如下:
- <?php
-
-
-
-
-
- class water{
-
- private $imgPath;
-
- public function __construct($imgPath="./"){
- $this->imgPath = rtrim($imgPath,"/")."/";
- }
-
-
- public function waterInfo($ground,$water,$pos=0,$prefix="lee_",$tm=50){
- $allPathGround = $this->imgPath.$ground;
- $allPathWater = $this->imgPath.$water;
- $groundInfo = $this->imgInfo($allPathGround);
- $waterInfo = $this->imgInfo($allPathWater);
-
-
- if(!$newPos=$this->imgPos($groundInfo,$waterInfo,$pos)){
- echo "您的水印图片比原图大哦";
- return false;
- }
-
-
- $groundRes=$this->imgRes($allPathGround,$groundInfo['mime']);
- $waterRes=$this->imgRes($allPathWater,$waterInfo['mime']);
-
-
- $newGround=$this->imgCopy($groundRes,$waterRes,$newPos,$waterInfo,$tm);
-
-
- $this->saveImg($newGround,$ground,$groundInfo['mime'],$prefix);
-
- }
-
- private function saveImg($img,$ground,$info,$prefix){
- $path=$this->imgPath.$prefix.$ground;
- switch($info){
- case "image/jpg":
- case "image/jpeg":
- case "image/pjpeg":
- imagejpeg($img,$path);
- break;
- case "image/gif":
- imagegif($img,$path);
- break;
- case "image/png":
- imagepng($img,$path);
- break;
- default:
- imagegd2($img,$path);
- }
- }
-
- private function imgCopy($ground,$water,$pos,$waterInfo,$tm){
- imagecopymerge($ground,$water,$pos[0],$pos[1],0,0,$waterInfo[0],$waterInfo[1],$tm);
- return $ground;
- }
-
- private function imgRes($img,$imgType){
- switch($imgType){
- case "image/jpg":
- case "image/jpeg":
- case "image/pjpeg":
- $res=imagecreatefromjpeg($img);
- break;
- case "image/gif":
- $res=imagecreatefromgif($img);
- break;
- case "image/png":
- $res=imagecreatefrompng($img);
- break;
- case "image/wbmp":
- $res=imagecreatefromwbmp($img);
- break;
- default:
- $res=imagecreatefromgd2($img);
- }
- return $res;
- }
-
-
-
-
-
-
- private function imgPos($ground,$water,$pos){
- if($ground[0]<$water[0] || $ground[1]<$water[1])
- return false;
- switch($pos){
- case 1:
- $x=0;
- $y=0;
- break;
- case 2:
- $x=ceil(($ground[0]-$water[0])/2);
- $y=0;
- break;
- case 3:
- $x=$ground[0]-$water[0];
- $y=0;
- break;
- case 4:
- $x=0;
- $y=ceil(($ground[1]-$water[1])/2);
- break;
- case 5:
- $x=ceil(($ground[0]-$water[0])/2);
- $y=ceil(($ground[1]-$water[1])/2);
- break;
- case 6:
- $x=$ground[0]-$water[0];
- $y=ceil(($ground[1]-$water[1])/2);
- break;
- case 7:
- $x=0;
- $y=$ground[1]-$water[1];
- break;
- case 8:
- $x=ceil($ground[0]-$water[0]/2);
- $y=$ground[1]-$water[1];
- break;
- case 9:
- $x=$ground[0]-$water[0];
- $y=$ground[1]-$water[1];
- break;
- case 0:
- default:
- $x=rand(0,$ground[0]-$water[0]);
- $y=rand(0,$ground[1]-$water[1]);
- }
- $xy[]=$x;
- $xy[]=$y;
- return $xy;
- }
-
-
- private function imgInfo($img){
- return getimagesize($img);
- }
- }
- ?>
用法很简单,我们介绍一下原理吧,我们只要创建一个water类就可以了,非常的简单.例子代码如下:
- if( !copy($file['tmp_name'],$filePath.$filename) ){
-
- exit(alert('上传文件出错,请稍候重试'));
-
- }
如果文件上传成功之后我们可以用如下代码:
- $wt = new sater();
- $water ='a.gif';
- $wt->waterInfo($filePath.$filename,$water)
|