最新2012php上传类支持图片水印

时间:2011-12-29 23:21 作者:php 点击:
最新2012php上传类支持图片水印!使用简单!支持无限图片!自动建立文件夹!按日期存储!

  

最新2012php上传类支持图片水印!使用简单!支持无限图片!自动建立文件夹!按日期存储!

 

 

  1. <?php  
  2. class UpImages {  
  3. var $annexFolder = "upload";//附件存放点,默认为:annex  
  4. var $smallFolder = "small";//缩略图存放路径,注:必须是放在 $annexFolder下的子目录,默认为:smallimg  
  5. var $markFolder = "mark";//水印图片存放处  
  6. var $upFileType = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip  
  7. var $upFileMax = 1024;//上传大小限制,单位是“KB”,默认为:1024KB  
  8. var $fontType;//字体  
  9. var $maxWidth = 500; //图片最大宽度  
  10. var $maxHeight = 600; //图片最大高度  
  11. function UpImages($annexFolder,$smallFolder,$markFolder,$id=1) {  
  12. if($id<1)exit('错误');  
  13. $date=date("Ymd");  
  14. $dir1=intval($id/2000);  
  15. $annex=$annexFolder.$dir1.'/'.$id.'/'.$date;  
  16. $small=$smallFolder.$dir1.'/'.$id.'/'.$date;  
  17. $mark=$markFolder.$dir1.'/'.$id.'/'.$date;  
  18. $this->id=$annexFolder.$dir1.'/'.$id.'/';  
  19. $this->annexFolder = $annex;  
  20. $this->smallFolder = $small;  
  21. $this->markFolder = $mark;  
  22. $this->fontType = "data/upload/04B_08__.TTF";  
  23. $this->create_folders($annex);  
  24. $this->create_folders($small);  
  25. $this->create_folders($mark);  
  26. }  
  27. function create_folders($dir){  
  28.     return is_dir($diror ($this->create_folders(dirname($dir)) and mkdir($dir, 0777));   
  29. }   
  30. function upLoad($inputName,$uploadFile='',$FileName='') {  
  31. $imageName = time().$r=rand(1,100);//设定当前时间为图片名称  
  32. $file = $_FILES[$inputName];  
  33. if(@emptyempty($file["name"])) die("没有上传图片信息,请确认");  
  34. $name = explode(".",$file["name"]);//将上传前的文件以“.”分开取得文件类型  
  35. $imgCount = count($name);//获得截取的数量  
  36. $imgType = $name[$imgCount-1];//取得文件的类型  
  37. $file['filename'] = substr($file['name'], 0, strpos($file['name'], '.'));  
  38. $this->name = $file['filename'] . '.' . $imgType;//原始上传文件名  
  39.  
  40.  
  41.  
  42. if(strpos($this->upFileType,$imgType) === false) die(error("上传文件类型仅支持 ".$this->upFileType." 不支持 ".$imgType));  
  43. $photo = $imageName.".".$imgType;//写入数据库的文件名  
  44. if($uploadFile==''){$uploadFile = $this->annexFolder."/".$photo;}//上传后的文件名称  
  45. if($FileName!='')$uploadFile=$this->id.$FileName'.' .$imgType;  
  46. $this->create_folders(dirname($uploadFile));  
  47. $upFileok = move_uploaded_file($file["tmp_name"],$uploadFile);  
  48. if($upFileok) {  
  49. $imgSize = $file["size"];  
  50. $kSize = round($imgSize/1024);  
  51. $this->file_size=$kSize;  
  52. if($kSize > ($this->upFileMax*1024)) {  
  53. @unlink($uploadFile);  
  54. die(error("上传文件超过 ".$this->upFileMax."KB"));  
  55. }  
  56. else {  
  57. die(error(" 上传图片失败,请确认你的上传文件不超过 $upFileMax KB 或上传时间超时"));  
  58. }  
  59. return $uploadFile;  
  60. }  
  61. //下载图片  
  62. function GetImg($url,$filename="") {   
  63.   if($url==""):return false;endif;   
  64.   $str=explode('/',$furl) ;  
  65.   $this->name= $str[count($str)-1] ;  
  66.     
  67.   if($filename=="") {   
  68.     $ext=strrchr($url,".");   
  69.     if($ext!=".gif" && $ext!=".jpg"):return false;endif;   
  70.     $filename=date("dMYHis").$ext;   
  71.   }   
  72.  
  73.   ob_start();   
  74.   readfile($url);   
  75.   $img = ob_get_contents();   
  76.   ob_end_clean();   
  77.   $size = strlen($img);   
  78.   $this->file_size = round($size/1024);  
  79.   if($this->file_size<1):return false;endif;//小于1k  
  80.   $filename$this->annexFolder."/".$filename;  
  81.   $this->create_folders(dirname($filename));  
  82.   $fp2=@fopen($filename"a");   
  83.   fwrite($fp2,$img);   
  84.   fclose($fp2);   
  85.  
  86.   return $filename;   
  87. }   
  88.  
  89. function getInfo($photo) {  
  90. //$photo = $this->annexFolder."/".$photo;  
  91. $imageInfo = getimagesize($photo);  
  92. $imgInfo["width"] = $imageInfo[0];  
  93. $imgInfo["height"] = $imageInfo[1];  
  94. $imgInfo["type"] = $imageInfo[2];  
  95. $imgInfo["name"] = basename($photo);  
  96. $imgInfo["names"] = $this->name;  
  97. $imgInfo["size"] = $this->file_size;  
  98. return $imgInfo;  
  99. }  
  100. function smallImg($photo,$width=128,$height=128) {  
  101. $imgInfo = $this->getInfo($photo);  
  102. //$photo = $this->annexFolder."/".$photo;//获得图片源  
  103. $newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//新图片名称  
  104. if($imgInfo["type"] == 1) {  
  105. $img = imagecreatefromgif($photo);  
  106. elseif($imgInfo["type"] == 2) {  
  107. $img = imagecreatefromjpeg($photo);  
  108. elseif($imgInfo["type"] == 3) {  
  109. $img = imagecreatefrompng($photo);  
  110. else {  
  111. $img = "";  
  112. }  
  113. if(emptyempty($img)) return False;  
  114. $width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width;  
  115. $height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height;  
  116. $srcW = $imgInfo["width"];  
  117. $srcH = $imgInfo["height"];  
  118. if ($srcW * $width > $srcH * $height) {  
  119. $height = round($srcH * $width / $srcW);  
  120. else {  
  121. $width = round($srcW * $height / $srcH);  
  122. }  
  123. if (function_exists("imagecreatetruecolor")) {  
  124. $newImg = imagecreatetruecolor($width$height);  
  125. ImageCopyResampled($newImg$img, 0, 0, 0, 0, $width$height$imgInfo["width"], $imgInfo["height"]);  
  126. else {  
  127. $newImg = imagecreate($width$height);  
  128. ImageCopyResized($newImg$img, 0, 0, 0, 0, $width$height$imgInfo["width"], $imgInfo["height"]);  
  129. }  
  130. if ($this->toFile) {  
  131. if (file_exists($this->smallFolder."/".$newName)) @unlink($this->smallFolder."/".$newName);  
  132. ImageJPEG($newImg,$this->smallFolder."/".$newName);  
  133. return $this->smallFolder."/".$newName;  
  134. else {  
  135. ImageJPEG($newImg);  
  136. }  
  137. ImageDestroy($newImg);  
  138. ImageDestroy($img);  
  139. return $newName;  
  140. }  
  141. function waterMark($photo,$text) {  
  142. $imgInfo = $this->getInfo($photo);  
  143. //$photo = $this->annexFolder."/".$photo;  
  144. $newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) . "_mark.jpg";  
  145. switch ($imgInfo["type"]) {  
  146. case 1:  
  147. $img = imagecreatefromgif($photo);  
  148. break;  
  149. case 2:  
  150. $img = imagecreatefromjpeg($photo);  
  151. break;  
  152. case 3:  
  153. $img = imagecreatefrompng($photo);  
  154. break;  
  155. default:  
  156. return False;  
  157. }  
  158. if (emptyempty($img)) return False;  
  159. $width = ($this->maxWidth > $imgInfo["width"]) ? $imgInfo["width"] : $this->maxWidth;  
  160. $height = ($this->maxHeight > $imgInfo["height"]) ? $imgInfo["height"] : $this->maxHeight;  
  161. $srcW = $imgInfo["width"];  
  162. $srcH = $imgInfo["height"];  
  163. if ($srcW * $width > $srcH * $height) {  
  164. $height = round($srcH * $width / $srcW);  
  165. else {  
  166. $width = round($srcW * $height / $srcH);  
  167. }  
  168. if (function_exists("imagecreatetruecolor")) {  
  169. $newImg = imagecreatetruecolor($width$height);  
  170. ImageCopyResampled($newImg$img, 0, 0, 0, 0, $width$height$imgInfo["width"], $imgInfo["height"]);  
  171. else {  
  172. $newImg = imagecreate($width$height);  
  173. ImageCopyResized($newImg$img, 0, 0, 0, 0, $width$height$imgInfo["width"], $imgInfo["height"]);  
  174. }  
  175.  
  176. $white = imageColorAllocate($newImg, 255, 255, 255);  
  177. $black = imageColorAllocate($newImg, 0, 0, 0);  
  178. $alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);  
  179. ImageFilledRectangle($newImg, 0, $height-26, $width$height$alpha);  
  180. //ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);  
  181. ImageTTFText($newImg, 10, 0, 20, $height-10, $black$this->fontType, $text[0]);  
  182. //ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);  
  183. if($this->toFile) {  
  184. if (file_exists($this->markFolder."/".$newName)) @unlink($this->markFolder."/".$newName);  
  185. ImageJPEG($newImg,$this->markFolder."/".$newName);  
  186. return $this->markFolder."/".$newName;  
  187. else {  
  188. ImageJPEG($newImg);  
  189. }  
  190. ImageDestroy($newImg);  
  191. ImageDestroy($img);  
  192. return $newName;  
  193. }  
  194.  
  195.  
  196. }  
  197. ?>  
  198. <?php   
  199. /*  
  200.  * 开始调用(使用方法)  
  201.  
  202. $text = array("WWW.PHPZY.COM",""); //水印内容  
  203. $img= new UpImages(MAX,MIX,MARK,$store_id); //实例化类文件  
  204. $photo = $img->upLoad("image_1"); //上传的文件域  
  205. echo $photo;  
  206. $img->maxWidth = $img->maxHeight = 600; //设置高,和宽  
  207. $img->toFile = true;  
  208. $newSmallImg = $img->smallImg($photo);  
  209. $newMark = $img->waterMark($photo,$text);  
  210. echo $newSmallImg;  
  211. echo $newMark;  
  212. echo "<img src=".$newSmallImg." border=0><br><br>";  
  213. echo "<img src=".$newMark." border=0><br><br>";   
  214.  */ 
  215. ?> 

 


标签(Tag):
------分隔线----------------------------
推荐内容
热点内容