0

this page just load random images from database.
problem is that this page take like 10sec to load. which is too slow. is there any thing i can do to load it faster. note i want store image in database.

<?php
include("include/header.php");

//check, if user is loged in or not
if(isset($_SESSION['username']))      /*** user is loged in ***/
{
    $user = $_SESSION['username'];    //get name of who is loged in

    $count = 0;
    $Q = "SELECT (SELECT image_short_name FROM user WHERE username = '$user') as `image_short_name`"
    .",`image_id`,`user_id`,`image_short_name`,`image`"
    ." FROM `image`"
    ." ORDER BY RAND()"
    ." LIMIT 30";
    $R = mysql_query($Q);

    echo "
        <head>
            <link rel='stylesheet' type='text/css' href='css/top_menu.css' />
            <link rel='stylesheet' type='text/css' href='css/all_css.css' />
        </head>

        <div id ='bg3'>
            <div id='context3'>

                <table  width='1000px' height='500px' cellpadding='45px'>                       
                    <tr height='100px'>     
                    ";           
                        while($row = mysql_fetch_assoc($R))
                            {
                                //get user_id from image table
                                $user_id_r         = $row['user_id'];
                                $image_short_name_r = $row['image_short_name'];

                                //get user name from user table
                                $queryget = mysql_query("SELECT username FROM user WHERE user_id = '$user_id_r'") ;
                                $row2 = mysql_fetch_assoc($queryget);   
                                $user_name_db = $row2['username'];

                                echo "<td width='100px'>";
                                $image_db = $row['image'];
                                $src = "data:image/gif;base64," . $image_db;
                                echo" <a href='zoom.php'><img src=\"$src\" width='130px' height='130px' class='image_p' /></a>";
                                echo "<center id='image_name'><a href='zoom.php'>" . $image_short_name_r . "</a></center>";
                                echo " <center id='image_name2'>by $user_name_db</center>";
                                echo "</td>";
                                $count++;
                                if($count == 5)       //5 rows
                                {
                                    echo "</tr><tr height='100px'>"; // Start a new row at 6 cells
                                    $count = 0;
                                }
                            }
                echo " 
                    </tr>
                </table>

            </div>
        </div>   
        ";
}
2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by diafol
2

Do you really need to use a nested SQL query? A join will be quicker.
Also, your images may be enormous, even though your display size is small. If each image is 1Mb+ in size, then 30 images will kill you. Your host will probably try to contact you as well! Profile images need to be VERY small. Perhaps using an ajax scroller or paginator can help so that you only display, say 10 at a time?

If your members are uploading their own photos, either have a filesize limit (say 50kB)
My DW Avatar: 5.7K (GIF), 15K (JPEG), 13K PNG32 (less for PNG16, PNG8) for a 100x100px image.

You may get better results from limiting filetype too. For oversize (pixels or memory), you can decide whether to reject the image or convert it (e.g. GD library).

Just a few thoughts.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.