开发者

Selecting random photos from featured album, PHP

开发者 https://www.devze.com 2023-02-03 02:50 出处:网络
Greetings, I am adding four random photos from a featured photo album to the front page of a site in the form of a jQuery photo slider. Since all of the images must be the same size, I select only ho

Greetings,

I am adding four random photos from a featured photo album to the front page of a site in the form of a jQuery photo slider. Since all of the images must be the same size, I select only horizontal photos, shuffle them, and then trim that down to 4. Here is the code I am using. My question - is there a simpler, perhaps more efficient way to do this? Or is my method fairly sound?

Thanks!

$getImages = $gallery_db->query("SELECT * FROM images WHERE album = '5'"); //sample SQL
$imagesArr = array();
while ($image = $getImages->fetch()) {
    $path = "http://somewhere.com/gallery/photos/" .
 $image['album'] . "/" . $image['filename'] . ".jpg"; //All files are .jpg
        list ($width, $height) = getimagesize($path);
        i开发者_高级运维f ($width > $height) {
            $imagesArr[] = $path;
        }
    }
    shuffle($imagesArr);
    array_splice($imagesArr, 4)

and then, to output:

foreach ($imagesArr as $path) {
        echo "<img src=\"$path\" width=\"220\" height=\"110\"/><br/>\n";
    }


Your solution looks just fine, it's pretty simple and straight forward. But remember, premature optimization is the root of all evil :)

A improvement would be to store the dimensions of each image in your database, so that you can fetch all images of a certain size and just take 4 random images.

0

精彩评论

暂无评论...
验证码 换一张
取 消