::PHP-IMAGES::
   
這次把圖片上傳及等比例縮放做一次整合,此篇主要功能是要防止我這快閃記憶體作成
的大腦,睡一覺起來又會全部忘光光,以免以後要用又要花時間再寫。

    〔test.php〕
            1‧由於是在Domlinux測試,故創新資料夾後,需修改資料夾權限,不
                 知什麼原因,於mkdir時就設定777,創新資料夾後還是變為775,
                 所以只好自己在加修改權限。
                 <?php
                      header('Content-Type:text/html; charset=utf-8');
 
                      //載入 等比縮放圖片函數
                      require_once('include/php/resizeImage.php');  

                      $DestDIR = "images/upload";
  
                      $folderName = $DestDIR ."/". "test";
 
                      //判斷目錄是否存在,如不存在即創造新資料夾
                      if (!file_exists($folderName)) {
                           mkdir($folderName,0775,TRUE);
                           chmod($folderName,0777);
                      }
 
                      //判斷目錄是否存在與目錄是否可以寫入  
                      if (!is_dir($folderName) || !is_writeable($folderName))
                            die("目錄不存在或無法寫入");
 
                      for ($i=0; $i<5; $i++) {
  
                               //確認檔案是否是真正被上傳與存在於伺服器
                               if (is_uploaded_file($_FILES['myfile']['tmp_name'][$i])) {
   
                                       if ($_FILES['myfile']['error'][$i] > 0) {
                                             switch ($_FILES['myfile']['error']) {
                                                       case 1 : die("檔案大小超出PHP限制");
                                                       case 2 : die("檔案大小超出上傳限制");
                                                       case 3 : die("檔案僅被部分上傳");
                                                       case 4 : die("檔案未被上傳");
                                              }
                                        }
   
                                       //取得副檔名
                                       $File_Extension = strrchr($_FILES['myfile']['name']
                                                                                    [$i],".");
   
                                       //判斷是否為Jpg或Gif檔
                                       if ($File_Extension == ".gif" || 
                                           $File_Extension == ".jpg") {
                                               $ServerFilename = "test_".$i."_".
                                                            date("YmdHis") . $File_Extension;
                                               copy($_FILES['myfile']['tmp_name'][$i] ,
                                                            $folderName . "/" . $ServerFilename );
     
                                               ImageCopyResizedTrue($folderName . "/" .
                                                           $ServerFilename, $folderName . "/" . 
                                                           $ServerFilename, 262, 200);
     
                                               //縮放後檔案會另存jpeg檔,所需刪除多餘的gif檔
                                               if ($File_Extension == ".gif") {
                                                     unlink($folderName . "/" . $ServerFilename);
                                               }
                                       }
   
                                }
  
                       }
                        ?>

    〔resizeImage.php〕
            1‧下面有兩種縮圖Function,差別在於第一種只限定傳入 *.jpg 檔,
                  第二種傳入 *.gif 檔後會自動轉存輸出 *.jpg 檔,所以使用第二種方
                  式,需刪除傳入的原始 *.gif 檔。
                  <?php
                    header('Content-Type:text/html; charset=utf-8');
                   /**
                    * --> 等比例縮放圖片函數,只能處理 jpeg 檔 <--
                    * 抓取要縮圖的比例, 下述只處理 jpeg
                    * $from_filename : 來源路徑,
                    * 檔名, ex: /tmp/xxx.jpg
                    * $save_filename : 縮圖完要存的路徑,
                    * 檔名, ex: /tmp/ooo.jpg
                    * $in_width : 縮圖預定寬度
                    * $in_height: 縮圖預定高度
                    * $quality  : 縮圖品質(1~100)
                    *
                    * Usage:
                    *   ImageResize('ram/xxx.jpg', 'ram/ooo.jpg');
                    */
                   function ImageResize($from_filename, $save_filenam
                   e, $in_width=400, $in_height=300, $quality=100)
                   {
                    $allow_format = array('jpeg', 'png', 'gif');
                    $sub_name = $t = '';
                  
                    // Get new dimensions
                    $img_info = getimagesize($from_filename);
                    $width    = $img_info['0'];
                    $height   = $img_info['1'];
                    $imgtype  = $img_info['2'];
                    $imgtag   = $img_info['3'];
                    $bits     = $img_info['bits'];
                    $channels = $img_info['channels'];
                    $mime     = $img_info['mime'];
                  
                    list($t, $sub_name) = split('/', $mime);
                    if ($sub_name == 'jpg') {
                     $sub_name = 'jpeg';
                    }
                  
                    if (!in_array($sub_name, $allow_format)) {
                     return false;
                    }
                  
                    // 取得縮在此範圍內的比例
                    $percent = getResizePercent($width, $height,
                     $in_width, $in_height);
                    $new_width  = $width * $percent;
                    $new_height = $height * $percent;
                  
                    // Resample
                    $image_new = imagecreatetruecolor($new_width,
                      $new_height);
                  
                    // $function_name: set function name
                    //   => imagecreatefromjpeg, imagecreatefrompng,
                     imagecreatefromgif
                    /*
                    // $sub_name = jpeg, png, gif
                    $function_name = 'imagecreatefrom'.$sub_name;
                    $image = $function_name($filename);
                    //$image = imagecreatefromjpeg($filename);
                    */
                    $image = imagecreatefromjpeg($from_filename);
                  
                    imagecopyresampled($image_new, $image, 0, 0, 0, 0,
                   $new_width, $new_height, $width, $height);
                  
                    return imagejpeg($image_new, $save_filename,
                   $quality);
                   }
                  
                   /**
                    * 抓取要縮圖的比例
                    * $source_w : 來源圖片寬度
                    * $source_h : 來源圖片高度
                    * $inside_w : 縮圖預定寬度
                    * $inside_h : 縮圖預定高度
                    *
                    * Test:
                    *   $v = (getResizePercent(1024, 768, 400, 300));
                    *   echo 1024 * $v . "\n";
                    *   echo  768 * $v . "\n";
                    */
                   function getResizePercent($source_w, $source_h, $ins
                   ide_w, $inside_h)
                   {
                   if ($source_w < $inside_w && $source_h < $inside_h)
                   {
                    // Percent = 1, 如果都比預計縮圖的小就不用縮
                    return 1;
                   }
                  
                   $w_percent = $inside_w / $source_w;
                   $h_percent = $inside_h / $source_h;
                  
                   return ($w_percent > $h_percent) ? $h_percent :
                   $w_percent;
                   }
             
                  /************************************************************/
                  
                  /**
                   *  --> 等比例縮放圖片函數,Jpeg 及 Gif 皆能處理,
                   *  執行後 Gif 會自動另存為 Jpeg 檔 <--
                   *$src   : 來源檔案
                   *$dest      : 目的檔案
                   *$maxWidth  : 縮圖寬度
                   *$maxHeight : 縮圖高度
                   *$quality   : JPEG品質
                   **/
                  function ImageCopyResizedTrue($src,$dest,$maxWidth,
                  $maxHeight,$quality=100) {
             
                   //檢查檔案是否存在
                   if (file_exists($src)  && isset($dest)) {
                 
                    $destInfo  = pathInfo($dest);
                     //圖檔大小
                    $srcSize   = getImageSize($src);
                     // 計算寬/高
                    $srcRatio  = $srcSize[0]/$srcSize[1];
                    $destRatio = $maxWidth/$maxHeight;
                    if ($destRatio > $srcRatio) {
                     $destSize[1] = $maxHeight;
                     $destSize[0] = $maxHeight*$srcRatio;
                    }
                    else {
                     $destSize[0] = $maxWidth;
                     $destSize[1] = $maxWidth/$srcRatio;
                    }
                   
                    //GIF 檔不支援輸出,因此將GIF轉成JPEG
                    if ($destInfo['extension'] == "gif")
                     $dest = substr_replace($dest, 'jpg', -3);
                 
                    //建立一個 True Color 的影像
                    $destImage = imageCreateTrueColor($destSize[0],
                       $destSize[1]);
                 
                    //根據副檔名讀取圖檔
                    switch ($srcSize[2]) {
                     case 1:
                      $srcImage = imageCreateFromGif($src);
                      break;
                     case 2:
                      $srcImage = imageCreateFromJpeg($src);
                      break;
                     case 3:
                      $srcImage = imageCreateFromPng($src);
                      break;
                     default: return false; break;
                    }
                 
                    //取樣縮圖
                    ImageCopyResampled($destImage, $srcImage, 0, 0, 0,
                     0,$destSize[0],$destSize[1],$srcSize[0],
                     $srcSize[1]);
                 
                    //輸出圖檔
                    switch ($srcSize[2]) {
                     case 1: case 2:
                      imageJpeg($destImage,$dest,$quality);
                      break;
                     case 3: imagePng($destImage,$dest); break;
                    }
                    return true;
                   }
                   else {
                    return false;
                   }
                  }
             ?>

創作者介紹

我想 - 我是屬於海洋

kyosukehot 發表在 痞客邦 PIXNET 留言(1) 人氣()


留言列表 (1)

發表留言
  • janet1
  • 被php搞死@@

    hi我想問一下php上傳照片可不可以寫成像奇摩部落格一樣 一次全選上傳一整批 而不是一張一張選
  • 應該是可以,要google找找,不過我已經半年多沒摸php了XD~Sorry

    kyosukehot 於 2010/04/15 22:57 回覆

找更多相關文章與討論