2013-04-02 60 views
0

我正在爲用戶製作一個網站,以便能夠跟蹤他們最喜歡的樂隊。該網站的其中一項功能是允許用戶上傳他們去過的任何演出的照片,然後將其保存到文件中,並將圖像位置存儲在數據庫中。使用PHP上傳,調整大小和保存(或生成縮略圖)

我的問題是我需要能夠保存時調整圖像的大小,因爲頁面需要很長時間才能加載時有很多圖片。

我知道這裏有很多問題,但我只是不確定如何修改代碼,我必須這樣做。

這是做到這一點的最好方法還是應該使用縮略圖?作爲圖像會顯示在一個畫廊,如果有許多則該頁面會加載較慢

我的PHP的知識是有限的,所以任何幫助表示讚賞

這是我的代碼瞬間:

<?php 

    ///UPLOAD IMAGES 
$sql = "SELECT * FROM photouploads WHERE BandID ='$bandid'"; 
$result = mysql_query($sql,$connect)or die ($error1); 
$row = mysql_num_rows($result); 
    $userid=$_SESSION['userid']; 

if (isset($_POST['submit'])) 

    { 
     $name = $bandid."(".++$row.").jpg"; 

     $tmp_name=$_FILES['photo']['tmp_name']; 

     if ($name) 
     { 
      //start upload 
      $location="Photouploads/".$name; 
      if (move_uploaded_file($tmp_name,$location)) 
        { 
        mysql_query("INSERT INTO photouploads (BandID,UserID,ImageLocation) 
        VALUES ('$bandid', '$userid', '$location')") ; 
        } 

     } 
else 
; 

} 

我的形式:

 <input type='file' name='photo' id='photo'> 
     <input type='submit' class='submitLink' name='submit' id='submit'value='upload'> 
</form>"; 
?> 
+0

http://php.net/manual/en/class.imagick.php –

+0

感謝您的鏈接,但我真的不知道如何實現這個 – zoe

+0

比你需要學習:) –

回答

0

這裏是我過去使用的非常基本的PHP圖像調整類。您需要安裝並啓用PHP GD2模塊才能運行。

用法:

$resized = ImageResizer::resizeExistingImage($_FILES['photo']['tmp_name'], 
                   null, 
                   500, 
                   500, 
                   100); 
if (!$resized){ 
    throw new Exception('Could not resize image'); 
} 

echo '<pre>'; 
print_r($resized); 
echo '</pre>'; 

的類別:

class ImageResizer 
{ 
    const RESULT_RESIZE_NOT_REQUIRED = 'resize_not_required'; 
    const RESULT_RESIZE_SUCCESSFUL = 'resize_successful'; 

    private static $_filePath = null; 
    private static $_newPath = null; 
    private static $_maxwidth = null; 
    private static $_maxheight = null; 
    private static $_quality = null; 

    private static $_newWidth = null; 
    private static $_newHeight = null; 

    private static $_actualWidth = null; 
    private static $_actualHeight = null; 

    /** 
    * Takes an image (from a file path) and resizes it. The newly resized image 
    * can then either be created in a different location, therefore maintainig 
    * the original file. Or can be created in the original location, therefore 
    * overwriting the original file. 
    * 
    * 
    * @static 
    * @access public 
    * @param string $filePath The file path of the image to resize 
    * @param string $newPath The file path where the resized image will 
    *        be created. Null to overwrite original. 
    * @param integer $maxwidth The maximum height of the resized image 
    * @param integer $maxheight The maximum width of the resized image 
    * @param integer $quality The quality of the image 
    */ 
    public static function resizeExistingImage($filePath, 
               $newPath = null, 
               $maxwidth = 1000, 
               $maxheight = 1000, 
               $quality = 100) 
    { 
     if (is_null($newPath)) { 
      $newPath = $filePath; 
     } 

     $gdImage = getimagesize($filePath); 

     $actualWidth = $gdImage[0]; 
     $actualHeight = $gdImage[1]; 

     // Do we even need to resize the image!? 
     if ($actualWidth <= $maxwidth && $actualHeight <= $maxheight){ 
      return array('result' => self::RESULT_RESIZE_NOT_REQUIRED, 
          'newHeight' => $actualHeight, 
          'newWidth' => $actualWidth); 
     } 

     $ratio = $actualWidth/$maxwidth; 
     // echo "ratio : ".$ratio."<br />"; 

     // echo "Current dimensions: ".$actualWidth." : ".$actualHeight." : <br />"; 

     // set the defaults: 
     $newWidth = intval($actualWidth); 
     $newHeight = intval($actualHeight);  

     if ($actualWidth > $maxwidth) { 
      $newWidth = intval($maxwidth); 
      $newHeight = intval($actualHeight/$ratio); 
     } 

     // echo "After width process, dimensions: ".$newWidth." : ".$newHeight." : <br />"; 

     // once we've got the size width, is the height now small enough? 
     if ($newHeight > $maxheight) { 
      // set a new ratio 
      $ratio = $newHeight/$maxheight; 
      $newWidth = intval($newWidth/$ratio); 
      $newHeight = intval($maxheight); 
     }  

     // echo "New dimensions: ".$newWidth." : ".$newHeight." : <br />"; 

     // Assign the class vars 
     self::$_filePath = $filePath; 
     self::$_newPath = $newPath; 
     self::$_maxwidth = $maxwidth; 
     self::$_maxheight = $maxheight; 
     self::$_quality = $quality; 

     self::$_newWidth = $newWidth; 
     self::$_newHeight = $newHeight; 

     self::$_actualWidth = $actualWidth; 
     self::$_actualHeight = $actualHeight; 

     switch (strtolower($gdImage['mime'])) { 
      case 'image/jpeg': 
       self::_createFromJpeg(); 
       break; 
      case 'image/pjpeg': 
       self::_createFromPJpeg(); 
       break; 
      case 'image/png': 
       self::_createFromPng(); 
       break; 
      case 'image/gif': 
       self::_createFromGif(); 
       break; 

      default: 
       throw new Exception('Mime Type \'' . $gdImage['mime'] . '\' not supported'); 
       break; 
     } 

     return array('result' => self::RESULT_RESIZE_SUCCESSFUL, 
         'newHeight' => $newHeight, 
         'newWidth' => $newWidth); 

    } 

    /** 
    * Resizes images of type image/jpeg. 
    * 
    * @static 
    * @access private 
    * @return void 
    */ 
    private static function _createFromJpeg() 
    { 
     $img = imagecreatefromjpeg(self::$_filePath); 

     $new_img = imagecreatetruecolor(self::$_newWidth, self::$_newHeight); 

     imagecopyresampled($new_img, 
          $img, 
          0, 
          0, 
          0, 
          0, 
          self::$_newWidth, 
          self::$_newHeight, 
          self::$_actualWidth, 
          self::$_actualHeight); 

     imagejpeg($new_img, self::$_newPath, self::$_quality); 
    } 

    /** 
    * Resizes images of type image/jpeg. 
    * 
    * @static 
    * @access private 
    * @return void 
    */ 
    private static function _createFromPJpeg() 
    { 
     $img = imagecreatefromjpeg(self::$_filePath); 

     imageinterlace($img, 1); 

     $new_img = imagecreatetruecolor(self::$_newWidth, self::$_newHeight); 

     imagecopyresampled($new_img, 
          $img, 
          0, 
          0, 
          0, 
          0, 
          self::$_newWidth, 
          self::$_newHeight, 
          self::$_actualWidth, 
          self::$_actualHeight); 

     imagejpeg($new_img, self::$_newPath, self::$_quality); 
    } 

    /** 
    * Resizes images of type image/png. 
    * 
    * @static 
    * @access private 
    * @return void 
    */ 
    private static function _createFromPng() 
    { 
     $img = imagecreatefrompng(self::$_filePath); 

     $new_img = imagecreatetruecolor(self::$_newWidth, self::$_newHeight); 

     imagecopyresampled($new_img, 
          $img, 
          0, 
          0, 
          0, 
          0, 
          self::$_newWidth, 
          self::$_newHeight, 
          self::$_actualWidth, 
          self::$_actualHeight); 

     imagepng($new_img, self::$_newPath);   
    } 

    /** 
    * Resizes images of type image/gif. 
    * 
    * @static 
    * @access private 
    * @return void 
    */ 
    private static function _createFromGif() 
    { 
     $img = imagecreatefromgif(self::$_filePath); 

     $new_img = imagecreatetruecolor(self::$_newWidth, self::$_newHeight); 

     imagecopyresampled($new_img, 
          $img, 
          0, 
          0, 
          0, 
          0, 
          self::$_newWidth, 
          self::$_newHeight, 
          self::$_actualWidth, 
          self::$_actualHeight); 

     imagegif($new_img, self::$_newPath);  
    } 
} 

希望有所幫助。

0

檢索所有的數據爲新圖像找到一個名字是非常沒有效率的。一個更好的解決方案是插入一條記錄,但不存儲文件名並從auto_increment id(使用基本路徑/ some/where/Photouploads/$ bandid /)生成文件名。

如果這不是您最大的性能問題,它很快就會出現。

減少圖像的大小也是一個好主意 - 正如檢查重複項一樣。

如果有許多則該頁面會加載較慢

折以下圖片的

推遲裝載 - 有很多現成的腳本來做到這一點(e.g.

+0

謝謝我會給這是一個去 – zoe