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

首頁 > 語言 > PHP > 正文

php 圖片上傳代碼,具有生成縮略圖與增加水印功能

2024-09-04 11:44:23
字體:
來源:轉載
供稿:網(wǎng)友

這款圖片上傳源代碼是一款可以上傳圖片并且還具有給上傳的圖片生成縮略圖與增加水印功能,可以說是一款完美的圖片上傳類,實例代碼如下:

  1.  
  2. class upfile { 
  3.  public $filepath = "m.survivalescaperooms.com/"//上傳文件存放文件夾 
  4.  
  5.  public $filesize = 1000000; //允許上傳的大小 
  6.  
  7.  //如果要修改允許上傳文件的類型  請搜索 【 switch ($upfiletype) { //文件類型  】 
  8.  
  9.  public $reimagesize = array ( 
  10.   true, //是否生成縮略圖 
  11.   400, //縮略圖寬 
  12.   300,//縮略圖高 
  13.   "" //縮略圖存放文件夾 如果為空和當前要生成縮略圖的文件在同一目錄 文件前綴r_ 
  14.  ); //是否生成縮略圖 array(生成或不生成,縮略圖寬,縮略圖高,存放文件夾); 注意:存放文件夾后跟 '/' 
  15.  
  16.  public $india = true; //是否打水印 true打 false不打 
  17.  
  18.  public $indiaimage = ""//水印圖片地址為空則不打圖片水印 如果有文字水印建議不要開啟圖片水印 
  19.  
  20.  public $indiaimagex = 100; //圖片距離圖片左邊距離 
  21.  
  22.  public $indiaimagey = 10; //圖片距離圖片上面距離 
  23.  
  24.  public $indiatext = "m.survivalescaperooms.com"//水印文字 
  25.  
  26.  public $fontsize = 6; //水印文字大小,1最小6最大 
  27.  
  28.  public $indiatextx = 10; //文字距離圖片左邊距離 
  29.  
  30.  public $indiatexty = 10; //文字距離圖片上面距離 
  31.  
  32.  public $r = 250; //圖片顏色三原色 $r紅 
  33.  
  34.  public $g = 250; //$g綠 
  35.  
  36.  public $b = 250; //$b藍 
  37.  
  38.  public $indiapath = ""//加了水印的圖片保存路徑,如果為空就直接替代原來的圖片 
  39.  
  40.  //開始上傳處理 
  41.  function uploadfile($upfile) { 
  42.   if ($upfile == "") { 
  43.    die("uploadfile:參數(shù)不足"); 
  44.   } 
  45.   if (!file_exists($this->filepath)) { 
  46.    mkdir($this->filepath); 
  47.   } 
  48.   $upfiletype = $upfile['type']; 
  49.   $upfilesize = $upfile['size']; 
  50.   $upfiletmpname = $upfile['tmp_name']; 
  51.   $upfilename = $upfile['name']; 
  52.   $upfileerror = $upfile['error']; 
  53.   if ($upfilesize > $this->filesize) { 
  54.    return false; //文件過大 
  55.   } 
  56.   switch ($upfiletype) { //文件類型 
  57.    case 'image/jpeg' : 
  58.     $type = 'jpg'
  59.     break
  60.    case 'image/pjpeg' : 
  61.     $type = 'jpg'
  62.     break
  63.    case 'image/png' : 
  64.     $type = 'png'
  65.     break
  66.    case 'image/gif' : 
  67.     $type = 'gif'
  68.     break
  69.   } 
  70.   if (!isset ($type)) { 
  71.    return false; //不支持此類型 
  72.   } 
  73.   if (!is_uploaded_file($upfiletmpnameor !is_file($upfiletmpname)) { 
  74.    return false; 
  75.    ; //文件不是經(jīng)過正規(guī)上傳的; 
  76.   } 
  77.   if ($this->upfileerror != 0) { 
  78.    return false; //其他錯誤 
  79.   } 
  80.   if ($this->upfileerror == 0) { 
  81.    if (!file_exists($upfiletmpname)) { 
  82.     return false; //臨時文件不存在 
  83.    } else { 
  84.     $filename = date("ymdhis", time() + 3600 * 8); //圖片已當前時間命名 
  85.     $filename = $this->filepath . $filename . "." . $type
  86.     if (!move_uploaded_file($upfiletmpname$filename)) { 
  87.      return false; //文件在移動中丟失 
  88.     } else { 
  89.      if ($this->india == true) { 
  90.       $this->goindia($filename$type,true); 
  91.      } else { 
  92.       if ($this->reimagesize[0] == true) { 
  93.        $this->goreimagesize($filename$type); 
  94.       } else { 
  95.        return true; //上傳成功! 
  96.        unlink($upfiletmpname); 
  97.       } 
  98.      } 
  99.     } 
  100.  
  101.    } 
  102.   } 
  103.  
  104.  } 
  105.  //添加水印處理 
  106.  function goindia($filename$filetype,$reimage=false) { 
  107.   if (!file_exists($filename)) { 
  108.    $this->reerror(7); //要添加水印的文件不存在 
  109.   } else { 
  110.    if ($filetype == "jpg") { 
  111.     $im = imagecreatefromjpeg($filename); 
  112.    } else 
  113.     if ($filetype == "gif") { 
  114.      $im = imagecreatefromgif($filename); 
  115.     } else 
  116.      if ($filetype == "png") { 
  117.       $im = imagecreatefrompng($filename); 
  118.      } 
  119.    if ($this->indiatext != "") { //如果水印文字不為空 
  120.     $textcolor = imagecolorallocate($im$this->r, $this->g, $this->b); //設置文字顏色 
  121.     imagestring($im$this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //將文字寫入圖片 
  122.    } 
  123.    if ($this->indiaimage != "") {//如果水印圖片不為空 
  124.     $indiaimagetype = getimagesize($this->indiaimage); 
  125.     $logow = $indiaimagetype[0]; //得到水印圖片的寬 
  126.     $logoh = $indiaimagetype[1]; //得到水印圖片的高 
  127.     switch ($indiaimagetype[2]) { //判斷水印圖片的格式 
  128.      case 1 : 
  129.       $indiaimagetype = "gif"
  130.       $logo = imagecreatefromgif($this->indiaimage); 
  131.       break
  132.      case 2 : 
  133.       $indiaimagetype = "jpg"
  134.       $logo = imagecreatefromjpeg($this->indiaimage); 
  135.       break
  136.      case 3 : 
  137.       $indiaimagetype = "png"
  138.       $logo = imagecreatefrompng($this->indiaimage); 
  139.       break
  140.     } 
  141.     imagealphablending($im, true); //打開混色模式 
  142.     imagecopy($im$logo$this->indiaimagex, $this->indiaimagey, 0, 0, $logow$logoh); 
  143.     imagedestroy($im); 
  144.     imagedestroy($logo); 
  145.    } 
  146.   } 
  147.   if ($this->indiapath == "") { //如果水印存放地址不為空 
  148.    if ($filetype == "jpg") { 
  149.     imagejpeg($im$filename); 
  150.    } else 
  151.     if ($filetype == "gif") { 
  152.      imagegif($im$filename); 
  153.     } else 
  154.      if ($filetype == "png") { 
  155.       imagepng($im$filename); 
  156.      } 
  157.    if($reimage == true){ 
  158.     $this->goreimagesize($filename,$filetype); 
  159.    }else
  160.     return true; //添加水印成功 
  161.    } 
  162.   } else { 
  163.    if (!file_exists($this->indiapath)) { 
  164.     mkdir($this->indiapath); 
  165.     return false; //請重新上傳 
  166.    } else { 
  167.     $indianame = basename($filename); 
  168.     $indianame = $this->indiapath . $indianame
  169.     if ($filetype == "jpg") { 
  170.      imagejpeg($im$indianame); 
  171.     } else 
  172.      if ($filetype == "gif") { 
  173.       imagegif($im$indianame); 
  174.      } else 
  175.       if ($filetype == "png") { 
  176.        imagepng($im$indianame); 
  177.       } 
  178.     if($reimage == true){ 
  179.      $this->goreimagesize($indianame,$filetype); 
  180.      echo $indianame
  181.     }else
  182.      return true; //添加水印成功 
  183.     } 
  184.    } 
  185.   } 
  186.  } 
  187.  function goreimagesize($filename$filetype) { 
  188.   if (!file_exists($filename)) { 
  189.    return false; //要生成縮略圖的圖片不存在 
  190.   } else { 
  191.    if ($filetype == 'jpg') { 
  192.     $reimage = imagecreatefromjpeg($filename); 
  193.    } 
  194.    elseif ($filetype == 'png') { 
  195.     $reimage = imagecreatefrompng($filename); 
  196.    } else 
  197.     if ($filetype == 'gif') { 
  198.      $reimage = imagecreatefromgif($filename); 
  199.     } 
  200.    if (isset ($reimage)) { 
  201.     $srcimagetype = getimagesize($filename); 
  202.     $srcimagetypew = $srcimagetype[0]; //得到原始圖片寬度 
  203.     $srcimagetypeh = $srcimagetype[1]; //得到原始圖片高度 
  204.     $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]); 
  205.     imagecopyresized($reim$reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew$srcimagetypeh); 
  206.     $reimagepath = $this->reimagesize[3]; 
  207.     if ($reimagepath != "") { //如果存放水印地址不為空 
  208.      if (!file_exists($reimagepath)) { 
  209.       mkdir($reimagepath); 
  210.      } else { 
  211.       $reimagename = basename($filename); 
  212.       $reimagename = $reimagepath . "r_" . $reimagename
  213.       if ($filetype == "gif"
  214.        imagegif($reim$reimagename); 
  215.       else 
  216.        if ($filetype == "jpg"
  217.         imagejpeg($reim$reimagename); 
  218.        else 
  219.         if ($filetype == "png"
  220.          imagepng($reim$reimagename); 
  221.       return true; 
  222.      } 
  223.     } else { 
  224.      $filename = basename($filename); 
  225.      if($this->indiapath == ""){ 
  226.       $filename = $this->filepath."r_" . $filename
  227.      }else
  228.       $filename = $this->indiapath."r_" . $filename
  229.      } 
  230.      if ($filetype == "gif"
  231.       imagegif($reim$filename); 
  232.      else 
  233.       if ($filetype == "jpg"
  234.        imagejpeg($reim$filename); 
  235.       else 
  236.        if ($filetype == "png"
  237.         imagepng($reim$filename); 
  238.      return true; 
  239.     } 
  240.  
  241.    } 
  242.   } 
  243.  } 
  244.  
  245. if ($_post["submit"]) { 
  246.  $file = $_files['uploadfile']; 
  247.  $upfile = new upfile(); 
  248.  echo $upfile->uploadfile($file); 
  249. }//開源代碼Vevb.com 
  250. ?> 
  251.   "" method="post" enctype="multipart/form-data"
  252.   "file" name="uploadfile"/>
     
  253.   "submit" value="上傳" name="submit"/> 
  254.    

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 秦安县| 南雄市| 吕梁市| 南召县| 湘乡市| 宽城| 砀山县| 岳阳市| 民县| 崇文区| 资中县| 塘沽区| 海林市| 堆龙德庆县| 探索| 东台市| 泌阳县| 宜宾市| 疏勒县| 芮城县| 桦南县| 柯坪县| 且末县| 韩城市| 丹寨县| 璧山县| 无为县| 财经| 饶平县| 刚察县| 鄄城县| 延川县| 淮北市| 中超| 卢氏县| 潢川县| 唐河县| 余姚市| 邯郸市| 富民县| 衡南县|