0

Everything works fine, the only problem is that it loads very slow. It takes 7 seconds to load a page containing 265 images. The images aren't on a remote server, and they are thumbnails. They are being displayed at true height/width as well, so the server doesn't have to resize them.

It shouldn't be the getimagesize(), because in a previous iteration of the page every image also had a getimagesize() function and that loaded without the problem.

The only thing I can find that could be a problem would be the database. This is the setup:

TABLE albums:

| id | naam | urlnaam | actief |


TABLE fotos:

| id | url | ext | description | albumid |

So the albumid from fotos is linked to the id from albums. Here's the code:

$album = mysql_real_escape_string($_GET['fx2']);
mysql_select_db('user_fotos');
$i = 0;
$sql = mysql_query("SELECT a.naam, b.url, b.ext, b.description, b.id FROM albums AS a LEFT JOIN fotos AS b ON a.id = b.albumid WHERE a.urlnaam = '$album'");
while($row = mysql_fetch_assoc($sql)){
    if($i == 0){
        echo "Album: {$row['naam']}<br />";
        echo "<table><tr>";
    }
    if($i % 4 == 0){echo "</tr><tr>";}
    echo "<td align='center'>";
    $photourl = "http://www.mysite.com/fotos/$album/thumbs/" . $row['url'] . $row['ext'];
    $dimensions = getimagesize($photourl);
    $breedte = $dimensions[0];
    $lengte = $dimensions[1];
    if($breedte == '120'){$aspect="width='120px' height='{$lengte}px'";}else{$aspect="height='120px' width='{$breedte}px'";}
        echo "<div class='imageHolder' style='width:{$breedte}px; height:{$lengte}px;' onClick=\"parent.location='$album/{$row['id']}'\" /><img $aspect src='http://www.mysite.com/fotos/$album/thumbs/" . $row['url'] . $row['ext'] . "' alt='{$row['description']}' /></div>";
        echo "</td>";
        $i++;
}
echo "</tr></table>";
7
  • 1
    "The images aren't on a remote server, and they are thumbnails." - since you pass a HTTP URI to getimagesize, it has to connect to that server and download those images over HTTP. Commented Apr 7, 2012 at 14:58
  • 1
    The reason it is taking so long is each photo needs tobe grabbed from the url and sized up and then your outputting the original size so if you removed the imagesize() and the style='width:{$breedte}px; height:{$lengte}px;' parts then the image would still be the original size without wasting time/resources/bandwidth checking the size, you should also cache the results & output. Commented Apr 7, 2012 at 14:58
  • @DCoder - well spotted. If the images are not remote, they should be accessed via the local filesystem. Commented Apr 7, 2012 at 15:01
  • @LawrenceCherone: That is for the box around the image, it isn't setting the height of the image. I suppose I could give it a try though. Commented Apr 7, 2012 at 15:06
  • @halfer: I'm rather new to these kind of troubles, how should I do that? Commented Apr 7, 2012 at 15:07

3 Answers 3

2

As others and myself have mentioned getting the imagesize() from a remote url will involve fetching the image slowing your script down: As I can see your using the actual dimensions so there is no need to actually fetch the size, also you may have some XSS problem with the album variable: Try this, your get the same result but much faster:

  <?php 
    $album = mysql_real_escape_string($_GET['fx2']);
    mysql_select_db('user_fotos');

    $sql = mysql_query("
    SELECT a.naam, b.url, b.ext, b.description, b.id 
    FROM albums AS a 
    LEFT JOIN fotos AS b ON a.id = b.albumid 
    WHERE a.urlnaam = '$album'");

    $i = 0;
    while($row = mysql_fetch_assoc($sql)){
        if($i == 0){
            echo "<p>Album: {$row['naam']}</p>";
            echo "<table><tr>";
        }
        if($i % 4 == 0){echo "</tr><tr>";}
        echo "<td align='center'>";
        echo "<div class='imageHolder' onClick=\"parent.location='".htmlentities($album)."/{$row['id']}'\" /><img src='./fotos/".htmlentities($album)."/thumbs/" . $row['url'] . $row['ext'] . "' alt='{$row['description']}' /></div>";
        echo "</td>";
        $i++;
    }
    echo "</tr></table>";
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

That does speed up the age a lot, but the 'imageHolder's aren't sizing properly...
Also, switch <img src='http://www.mysite.com/fotos to <img src='/fotos - the FQDN is redundant if the images are on the same server.
If the imageHolder class isn't sizing properly you may just need to do some CSS adjustment.
Works like a charm! Totally forgot you could add borders to images in css! Thanks a bunch :)
0

I take it the images themselves are not dynamically generated as well? I suggest you do the getimagesize() when an image is loaded into your database, and then create width/height columns to save the expensive file operation. Also, do some html cacheing.

1 Comment

Well the weird thing is I had a getimagesize() in my previous iteration, and that did it without any slow problems. I suppose I could do that though... Yet I'd still like to know what the problem is right now..
0

may be getimagesize($photourl) make script slow down

your queries are normal

you can check the script easily:

make the width and height are const in php code and run the script again, so u can know what line make the script run slow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.