php 圖片上傳並產生縮圖代碼

來源:互聯網
上載者:User

 if($_FILES['image']['size']){
     if($_FILES['image']['type'] == "image/pjpeg"){
      $im = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
      $n_bmp.='.jpg';
     }elseif($_FILES['image']['type'] == "image/x-png"){
      $im = @imagecreatefrompng($_FILES['image']['tmp_name']);
      $n_bmp.='.png';
     }elseif($_FILES['image']['type'] == "image/gif"){
      $im = @imagecreatefromgif($_FILES['image']['tmp_name']);
      $n_bmp.='.gif';
     }  
    ResizeImage($im,8888,8888,$n_bmp);    
       ImageDestroy ($im);
    $n_tag =1;
      
   }
    
function Uploadfile($str){
   
   $save_path = './product/';//檔案儲存目錄路徑
   
   $ext_arr   = array('rar','zip','jpg','gif','png','bmp');//定義允許上傳的副檔名
   
   $max_size  = 1000000;//最大檔案大小
   
   $file_rand ='';
   
   $file_ext  ='';
   
   @mkdir($save_path, 0777); //更改目錄許可權    
   
   if ($_FILES[$str]['name']) {//有上傳檔案時
    
    $file_name = $_FILES[$str]['name'];//原檔案名稱
    
    $tmp_name = $_FILES[$str]['tmp_name'];//伺服器上臨時檔案名稱
    
    $file_size = $_FILES[$str]['size'];//檔案大小
    
    if (@is_dir($save_path) === false) {//檢查目錄
     alert("上傳目錄不存在。",'');
    }
    
    if (@is_writable($save_path) === false) {//檢查目錄寫入權限
     alert("上傳目錄沒有寫入權限。",'');
    }
    
    if (@is_uploaded_file($tmp_name) === false) {//檢查是否已上傳
     alert("臨時檔案可能不是上傳檔案。",'');
    }
    
    if ($file_size > $max_size) {//檢查檔案大小
     alert("上傳檔案大小超過限制。",'');
    }
    
    $temp_arr = explode(".", $file_name);//獲得副檔名
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);
    $file_rand= md5(date("Y-m-d"));
    if (in_array($file_ext, $ext_arr) === false) {//檢查副檔名
     alert("上傳副檔名是不允許的副檔名。",'');
    }
    
    if (move_uploaded_file($tmp_name, $save_path.$file_rand.'.'.$file_ext) === false) {//移動檔案
     alert("上傳檔案失敗。",'');
    }    
   } 
  return $save_path.$file_rand.'.'.$file_ext;
 }
 
 
 function ResizeImage($im,$maxwidth,$maxheight,$name){
   $width = imagesx($im);
   $height = imagesy($im);
  if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
   if($maxwidth && $width > $maxwidth){
   $widthratio = $maxwidth/$width;
   $RESIZEWIDTH=true;
   }
   if($maxheight && $height > $maxheight){
   $heightratio = $maxheight/$height;
   $RESIZEHEIGHT=true;
   }
  if($RESIZEWIDTH && $RESIZEHEIGHT){
  if($widthratio < $heightratio){
   $ratio = $widthratio;
   }else{
   $ratio = $heightratio;
   }
  }elseif($RESIZEWIDTH){
   $ratio = $widthratio;
  }elseif($RESIZEHEIGHT){
   $ratio = $heightratio;
  }
  $newwidth = $width * $ratio;
  $newheight = $height * $ratio;
  if(function_exists("imagecopyresampled")){
  $newim = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  }else{
  $newim = imagecreate($newwidth, $newheight);
  imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  }
  ImageJpeg ($newim,'./product_e/'.$name);
  ImageDestroy ($newim);
  }else{
  ImageJpeg ($im,'./product_e/'.$name);
  }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.