linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
PHP经典的给图片加水印程序
[ 作者:  加入时间:2007-12-03 16:22:23  来自:Linux联盟收集整理 ]
 如果对PHP的GD库比较熟悉,看懂这篇文章一点都不难了! 3nLLinux联盟
  <?php 3nLLinux联盟
  /********************************************************* 3nLLinux联盟
   3nLLinux联盟
  参数说明: 3nLLinux联盟
  $max_file_size : 上传文件大小限制, 单位BYTE 3nLLinux联盟
  $destination_folder : 上传文件路径 3nLLinux联盟
  $watermark : 是否附加水印(1为加水印,其他为不加水印); 3nLLinux联盟
   3nLLinux联盟
  使用说明: 3nLLinux联盟
  1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 3nLLinux联盟
  2. 将extension_dir =改为你的php_gd2.dll所在目录; 3nLLinux联盟
  *****************************************************************/ 3nLLinux联盟
   3nLLinux联盟
  //上传文件类型列表 3nLLinux联盟
  $uptypes=array( 3nLLinux联盟
   'image/jpg', 3nLLinux联盟
   'image/jpeg', 3nLLinux联盟
   'image/png', 3nLLinux联盟
   'image/pjpeg', 3nLLinux联盟
   'image/gif', 3nLLinux联盟
   'image/bmp', 3nLLinux联盟
   'image/x-png' 3nLLinux联盟
  ); 3nLLinux联盟
   3nLLinux联盟
  $max_file_size=2000000; //上传文件大小限制, 单位BYTE 3nLLinux联盟
  $destination_folder="uploadimg/"; //上传文件路径 3nLLinux联盟
  $watermark=1; //是否附加水印(1为加水印,其他为不加水印); 3nLLinux联盟
  $watertype=1; //水印类型(1为文字,2为图片) 3nLLinux联盟
  $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); 3nLLinux联盟
  $waterstring="http://www.xplore.cn/"; //水印字符串 3nLLinux联盟
  $waterimg="xplore.gif"; //水印图片 3nLLinux联盟
  $imgpreview=1; //是否生成预览图(1为生成,其他为不生成); 3nLLinux联盟
  $imgpreviewsize=1/2; //缩略图比例 3nLLinux联盟
  ?> 3nLLinux联盟
  <html> 3nLLinux联盟
  <head> 3nLLinux联盟
  <title>ZwelL图片上传程序</title> 3nLLinux联盟
  <style type="text/css"> 3nLLinux联盟
  <!-- 3nLLinux联盟
  body 3nLLinux联盟
  { 3nLLinux联盟
   font-size: 9pt; 3nLLinux联盟
  } 3nLLinux联盟
  input 3nLLinux联盟
  { 3nLLinux联盟
   background-color: #66CCFF; 3nLLinux联盟
   border: 1px inset #CCCCCC; 3nLLinux联盟
  } 3nLLinux联盟
  --> 3nLLinux联盟
  </style> 3nLLinux联盟
  </head> 3nLLinux联盟
   3nLLinux联盟
  <body> 3nLLinux联盟
  <form enctype="multipart/form-data" method="post" name="upform"> 3nLLinux联盟
   上传文件: 3nLLinux联盟
   <input name="upfile" type="file"> 3nLLinux联盟
   <input type="submit" value="上传"><br> 3nLLinux联盟
   允许上传的文件类型为:<?=implode(', ',$uptypes)?> 3nLLinux联盟
  </form> 3nLLinux联盟
   3nLLinux联盟
  <?php 3nLLinux联盟
  if ($_SERVER['REQUEST_METHOD'] == 'POST') 3nLLinux联盟
  { 3nLLinux联盟
   if (!is_uploaded_file($_FILES["upfile"][tmp_name])) 3nLLinux联盟
   //是否存在文件 3nLLinux联盟
   { 3nLLinux联盟
   echo "图片不存在!"; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   $file = $_FILES["upfile"]; 3nLLinux联盟
   if($max_file_size < $file["size"]) 3nLLinux联盟
   //检查文件大小 3nLLinux联盟
   { 3nLLinux联盟
   echo "文件太大!"; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   if(!in_array($file["type"], $uptypes)) 3nLLinux联盟
   //检查文件类型 3nLLinux联盟
   { 3nLLinux联盟
   echo "文件类型不符!".$file["type"]; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   if(!file_exists($destination_folder)) 3nLLinux联盟
   { 3nLLinux联盟
   mkdir($destination_folder); 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   $filename=$file["tmp_name"]; 3nLLinux联盟
   $image_size = getimagesize($filename); 3nLLinux联盟
   $pinfo=pathinfo($file["name"]); 3nLLinux联盟
   $ftype=$pinfo['extension']; 3nLLinux联盟
   $destination = $destination_folder.time().".".$ftype; 3nLLinux联盟
   if (file_exists($destination) && $overwrite != true) 3nLLinux联盟
   { 3nLLinux联盟
   echo "同名文件已经存在了"; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   if(!move_uploaded_file ($filename, $destination)) 3nLLinux联盟
   { 3nLLinux联盟
   echo "移动文件出错"; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
$pinfo=pathinfo($destination); 3nLLinux联盟
   $fname=$pinfo[basename]; 3nLLinux联盟
   echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>"; 3nLLinux联盟
   echo " 宽度:".$image_size[0]; 3nLLinux联盟
   echo " 长度:".$image_size[1]; 3nLLinux联盟
   echo "<br> 大小:".$file["size"]." bytes"; 3nLLinux联盟
   3nLLinux联盟
   if($watermark==1) 3nLLinux联盟
   { 3nLLinux联盟
   $iinfo=getimagesize($destination,$iinfo); 3nLLinux联盟
   $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); 3nLLinux联盟
   $white=imagecolorallocate($nimage,255,255,255); 3nLLinux联盟
   $black=imagecolorallocate($nimage,0,0,0); 3nLLinux联盟
   $red=imagecolorallocate($nimage,255,0,0); 3nLLinux联盟
   imagefill($nimage,0,0,$white); 3nLLinux联盟
   switch ($iinfo[2]) 3nLLinux联盟
   { 3nLLinux联盟
   case 1: 3nLLinux联盟
   $simage =imagecreatefromgif($destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 2: 3nLLinux联盟
   $simage =imagecreatefromjpeg($destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 3: 3nLLinux联盟
   $simage =imagecreatefrompng($destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 6: 3nLLinux联盟
   $simage =imagecreatefromwbmp($destination); 3nLLinux联盟
   break; 3nLLinux联盟
   default: 3nLLinux联盟
   die("不支持的文件类型"; 3nLLinux联盟
   exit; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 3nLLinux联盟
   imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 3nLLinux联盟
   3nLLinux联盟
   switch($watertype) 3nLLinux联盟
   { 3nLLinux联盟
   case 1: //加水印字符串 3nLLinux联盟
   imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); 3nLLinux联盟
   break; 3nLLinux联盟
   case 2: //加水印图片 3nLLinux联盟
   $simage1 =imagecreatefromgif("xplore.gif"; 3nLLinux联盟
   imagecopy($nimage,$simage1,0,0,0,0,85,15); 3nLLinux联盟
   imagedestroy($simage1); 3nLLinux联盟
   break; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   switch ($iinfo[2]) 3nLLinux联盟
   { 3nLLinux联盟
   case 1: 3nLLinux联盟
   //imagegif($nimage, $destination); 3nLLinux联盟
   imagejpeg($nimage, $destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 2: 3nLLinux联盟
   imagejpeg($nimage, $destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 3: 3nLLinux联盟
   imagepng($nimage, $destination); 3nLLinux联盟
   break; 3nLLinux联盟
   case 6: 3nLLinux联盟
   imagewbmp($nimage, $destination); 3nLLinux联盟
   //imagejpeg($nimage, $destination); 3nLLinux联盟
   break; 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   //覆盖原上传文件 3nLLinux联盟
   imagedestroy($nimage); 3nLLinux联盟
   imagedestroy($simage); 3nLLinux联盟
   } 3nLLinux联盟
   3nLLinux联盟
   if($imgpreview==1) 3nLLinux联盟
   { 3nLLinux联盟
   echo "<br>图片预览:<br>"; 3nLLinux联盟
   echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); 3nLLinux联盟
   echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; 3nLLinux联盟
   } 3nLLinux联盟
  } 3nLLinux联盟
  ?> 3nLLinux联盟
  </body> 3nLLinux联盟
  </html> 3nLLinux联盟
   3nLLinux联盟
   3nLLinux联盟
   3nLLinux联盟
  最后我觉得有必要说一下给图片加水印的原理: 3nLLinux联盟
   3nLLinux联盟
  取得上传的文件信息,建立一个真彩色图象,分配颜色方案并且填充图像。 3nLLinux联盟
   3nLLinux联盟
  通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像 3nLLinux联盟
   3nLLinux联盟
  程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。 3nLLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·使用PHP往Windows系统中添加用户  (2007-12-03 16:02:29)
 ·轻型数据库SQLite结合PHP的开发  (2007-12-03 16:00:56)
 ·PHP实现自动刷数和“灌水”机  (2007-12-03 16:00:21)
 ·PHP实现网页自动更新块  (2007-12-03 15:59:55)
 ·用PHP读写NTFS文件系统下的文件摘要信息  (2007-12-03 15:57:11)
 ·PHP中通过Web执行C/C++应用程序  (2007-12-03 15:55:58)
 ·PHP下实现端口复用/劫持  (2007-12-03 15:54:52)
 ·PHP和JAVA的XML-RPC中文问题解决办法  (2007-12-03 15:54:04)
 ·使用php的zlib压缩和解压缩swf文件  (2007-11-29 07:29:43)
 ·使用php通过Socket进行发信源码,支持发信认证  (2007-11-28 14:52:50)