0

ユーザーがお気に入りのバンドを追跡できるようにするための Web サイトを作成しています。Web サイトの機能の 1 つは、ユーザーが行ったギグの写真をアップロードできるようにすることです。写真はファイルに保存され、画像の場所はデータベースに保存されます。

私の問題は、画像が多いとページの読み込みに時間がかかるため、保存時に画像のサイズを変更できる必要があることです。

このような質問がたくさんあることは知っていますが、これを行う必要があるコードを変更する方法がわかりません。

これが最善の方法ですか、それともサムネイルを使用する必要がありますか? 画像はギャラリーに表示されるため、画像が多いとページの読み込みが遅くなります

私の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>";
?>
4

2 に答える 2

0

新しい画像の名前を見つけるためにすべてのデータを取得するのは非常に非効率的です。より良い解決策は、ファイル名を保存せずにレコードを挿入し、auto_increment ID からファイル名を生成することです (ベース パス /some/where/Photouploads/$bandid/ を使用)。

これが最大のパフォーマンスの問題ではない場合でも、すぐにそうなるでしょう。

重複をチェックするのと同様に、画像のサイズを縮小することも良い考えです。

数が多い場合、ページの読み込みが遅くなります

フォールド下の画像の読み込みを遅らせる - これを行うための既製のスクリプトがたくさんあります ()

于 2013-04-02T10:20:37.900 に答える
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);    
    }
}

それが役立つことを願っています。

于 2013-04-02T12:53:50.990 に答える