用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

php图片上传类同时可生成缩略图与加水印

时间:2015-01-23  来源:西部数据  作者:西部数据

这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印,php图片上传类同时可生成缩略图与加水印实例代码如下:

  1. class upimages { 
  2.         var $annexfolder = "upload";//附件存放点,默认为:annex 
  3.         var $smallfolder = "small";//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg 
  4.         var $markfolder = "mark";//水印图片存放处 
  5.         var $upfiletype = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip 
  6.         var $upfilemax = 1024;//上传大小限制,单位是"kb",默认为:1024kb 
  7.         var $fonttype;//字体 
  8.         var $maxwidth = 500; //图片最大宽度  
  9.         var $maxheight = 600; //图片最大高度  
  10.         function upimages($annexfolder,$smallfolder,$includefolder) { 
  11.                 $this->annexfolder = $annexfolder
  12.                 $this->smallfolder = $smallfolder
  13.                 $this->fonttype = $includefolder."/04b_08__.ttf"
  14.         } 
  15.         function upload($inputname) { 
  16.                 $imagename = time();//设定当前时间为图片名称 
  17.                 if(@emptyempty($_files[$inputname]["name"])) die("没有上传图片信息,请确认"); 
  18.                 $name = explode(".",$_files[$inputname]["name"]);//将上传前的文件以"."分开取得文件类型 
  19.                 $imgcount = count($name);//获得截取的数量 
  20.                 $imgtype = $name[$imgcount-1];//取得文件的类型 
  21.                 if(strpos($this->upfiletype,$imgtype) === false) die(error("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$imgtype)); 
  22.                 $photo = $imagename.".".$imgtype;//写入数据库教程的文件名 
  23.                 $uploadfile = $this->annexfolder."/".$photo;//上传后的文件名称 
  24.                 $upfileok = move_uploaded_file($_files[$inputname]["tmp_name"],$uploadfile); 
  25.                 if($upfileok) { 
  26.                         $imgsize = $_files[$inputname]["size"]; 
  27.                         $ksize = round($imgsize/1024); 
  28.                         if($ksize > ($this->upfilemax*1024)) { 
  29.                                 @unlink($uploadfile); 
  30.                                 die(error("上传文件超过 ".$this->upfilemax."kb")); 
  31.                         } 
  32.                 } else { 
  33.                         die(error("上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时")); 
  34.                 } 
  35.                 return $photo
  36.         } 
  37.         function getinfo($photo) { 
  38.                 $photo = $this->annexfolder."/".$photo
  39.                 $imageinfo = getimagesize($photo); 
  40.                 $imginfo["width"] = $imageinfo[0]; 
  41.                 $imginfo["height"] = $imageinfo[1]; 
  42.                 $imginfo["type"] = $imageinfo[2]; 
  43.                 $imginfo["name"] = basename($photo); 
  44.                 return $imginfo
  45.         } 
  46.         function smallimg($photo,$width=128,$height=128) { 
  47.                 $imginfo = $this->getinfo($photo); 
  48.                 $photo = $this->annexfolder."/".$photo;//获得图片源 
  49.                 $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称 
  50.                 if($imginfo["type"] == 1) { 
  51.                         $img = imagecreatefromgif($photo); 
  52.                 } elseif($imginfo["type"] == 2) { 
  53.                         $img = imagecreatefromjpeg($photo); 
  54.                 } elseif($imginfo["type"] == 3) { 
  55.                         $img = imagecreatefrompng($photo); 
  56.                 } else { 
  57.                         $img = ""
  58.                 } 
  59.                 if(emptyempty($img)) return false; 
  60.                 $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;  
  61.                 $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;  
  62.                 $srcw = $imginfo["width"];  
  63.                 $srch = $imginfo["height"];  
  64.                 if ($srcw * $width > $srch * $height) { 
  65.                         $height = round($srch * $width / $srcw); 
  66.                 } else { 
  67.                         $width = round($srcw * $height / $srch); 
  68.                 } 
  69.                 if (function_exists("imagecreatetruecolor")) { 
  70.                         $newimg = imagecreatetruecolor($width$height); 
  71.                         imagecopyresampled($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  72.                 } else { 
  73.                         $newimg = imagecreate($width$height); 
  74.                         imagecopyresized($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  75.                 } 
  76.                 if ($this->tofile) { 
  77.                         if (file_exists($this->annexfolder."/".$this->smallfolder."/".$newname)) @unlink($this->annexfolder."/".$this->smallfolder."/".$newname); 
  78.                         imagejpeg($newimg,$this->annexfolder."/".$this->smallfolder."/".$newname); 
  79.                         return $this->annexfolder."/".$this->smallfolder."/".$newname
  80.                 } else { 
  81.                         imagejpeg($newimg); 
  82.                 } 
  83.                 imagedestroy($newimg); 
  84.                 imagedestroy($img); 
  85.                 return $newname
  86.         } 
  87.         function watermark($photo,$text) { 
  88.                 $imginfo = $this->getinfo($photo); 
  89.                 $photo = $this->annexfolder."/".$photo
  90.                 $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg"
  91.                 switch ($imginfo["type"]) { 
  92.                         case 1: 
  93.                                 $img = imagecreatefromgif($photo); 
  94.                         break
  95.                         case 2: 
  96.                                 $img = imagecreatefromjpeg($photo); 
  97.                         break
  98.                         case 3: 
  99.                                 $img = imagecreatefrompng($photo); 
  100.                         break
  101.                         default
  102.                                 return false; 
  103.                 } 
  104.                 if (emptyempty($img)) return false; 
  105.                 $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;  
  106.                 $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;  
  107.                 $srcw = $imginfo["width"];  
  108.                 $srch = $imginfo["height"];  
  109.                 if ($srcw * $width > $srch * $height) { 
  110.                         $height = round($srch * $width / $srcw); 
  111.                 } else { 
  112.                         $width = round($srcw * $height / $srch); 
  113.                 } 
  114.                 if (function_exists("imagecreatetruecolor")) { 
  115.                         $newimg = imagecreatetruecolor($width$height); 
  116.                         imagecopyresampled($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  117.                 } else { 
  118.                         $newimg = imagecreate($width$height); 
  119.                         imagecopyresized($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  120.                 } 
  121.                  
  122.                 $white = imagecolorallocate($newimg, 255, 255, 255); 
  123.                 $black = imagecolorallocate($newimg, 0, 0, 0); 
  124.                 $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40); 
  125.                 imagefilledrectangle($newimg, 0, $height-26, $width$height$alpha); 
  126.                 imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black); 
  127.                 imagettftext($newimg, 4.9, 0, 20, $height-14, $black$this->fonttype, $text[0]); 
  128.                 imagettftext($newimg, 4.9, 0, 20, $height-6, $black$this->fonttype, $text[1]); 
  129.                 if($this->tofile) { 
  130.                         if (file_exists($this->annexfolder."/".$this->markfolder."/".$newname)) @unlink($this->annexfolder."/".$this->markfolder."/".$newname); 
  131.                         imagejpeg($newimg,$this->annexfolder."/".$this->markfolder."/".$newname); 
  132.                         return $this->annexfolder."/".$this->markfolder."/".$newname
  133.                 } else { 
  134.                         imagejpeg($newimg); 
  135.                 }//开源代码phpfensi.com 
  136.                 imagedestroy($newimg); 
  137.                 imagedestroy($img); 
  138.                 return $newname
  139.         } 
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
栏目更新
栏目热门