国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php圖片上傳類同時可生成縮略圖與加水印

2024-09-04 11:44:22
字體:
來源:轉載
供稿:網友

這款圖片上傳代碼可以把上傳的圖片增加水印,生成小圖片,同時還可以生成文字水印,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.                 }//開源代碼Vevb.com 
  136.                 imagedestroy($newimg); 
  137.                 imagedestroy($img); 
  138.                 return $newname
  139.         } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梓潼县| 巨野县| 苏州市| 都兰县| 揭阳市| 连平县| 怀集县| 马边| 铁力市| 宜丰县| 都昌县| 梧州市| 万安县| 仪征市| 习水县| 若羌县| 林西县| 英山县| 临清市| 左贡县| 新河县| 襄垣县| 怀柔区| 淳安县| 尼木县| 道孚县| 吕梁市| 平湖市| 永吉县| 长岭县| 正蓝旗| 庆安县| 青冈县| 宜昌市| 随州市| 景德镇市| 水富县| 潜江市| 高邑县| 伊川县| 云阳县|