Help with displaying random photos & 100 quality thumbs in this script

Hey Everyone,

I chopped up this script from a couple of different sources & it works great, right now I’m just trying to add 2 more things. 1) Randomize the photos being called from the thumbs directory. 2) Create a better quality thumbnail (using 100).

I tried a couple of variations, but I may be placing the scripts in the wrong areas, or not writing them correctly. Here’s the clean version & if anyone can help add in the rest, would be really grateful:

[php]

<?php /* function: generates thumbnail */ function make_thumb($src,$dest,$desired_width) { /* read the source image */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); /* find the "desired height" of this thumbnail, relative to the desired width */ $desired_height = floor($height*($desired_width/$width)); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width,$desired_height); /* copy source image at a resized size */ imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image,$dest); } /* function: returns files from dir */ function get_files($images_dir,$exts = array('jpg')) { $files = array(); if($handle = opendir($images_dir)) { while(false !== ($file = readdir($handle))) { $extension = strtolower(get_file_extension($file)); if($extension && in_array($extension,$exts)) { $files[] = $file; } } closedir($handle); } return $files; } /* function: returns a file's extension */ function get_file_extension($file_name) { return substr(strrchr($file_name,'.'),1); } ?>

[/php]

[php]

<?php /** CALL INTO PHOTO GALLERY DIRECTORY AND FOR EACH IMAGE, CREATE HYPERLINK AND DISPLAY ON WEBSITE**/ $images_dir = "PATHTOIMAGES/"; $thumbs_dir = "PATHTOIMAGES/thumbs/"; $thumbs_width = 300; /** generate photo gallery **/ $image_files = get_files($images_dir); if(count($image_files)) { $index = 0; foreach($image_files as $index=>$file) { $index++; $thumbnail_image = $thumbs_dir.$file; if(!file_exists($thumbnail_image)) { $extension = get_file_extension($thumbnail_image); if($extension) { make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width); } } echo ''; } } ?>

[/php]

imagejpeg($virtual_image,$dest);
//to
imagejpeg($virtual_image, $dest, 100);

for the random pic look at

http://php.net/manual/en/function.array-rand.php

Ahhh. I was placing the 100 in the wrong area, thanks.

Since I’m a beginner at this, I took a look at that array_rand script & just want to see if this makes sense. This is the php example which just displays 2 of the array values, and I’m not sure how to integrate this into my script so it’ll display each file without having to add an additional echo for every file.

PHP.net example:
[php]

<?php $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2); echo $input[$rand_keys[0]] . "\n"; echo $input[$rand_keys[1]] . "\n"; ?>

[/php]

I’m guessing, I’ll need to use a variable for the array & then define that variable as the whole URL for the photo. So hopefully this will echo all files available in the directory from the foreach loop, am I on the right track here?

[php]
$galleryimages = ‘

  • ’;
    $input = array($galleryimages);
    $rand_keys = array_rand($input);
    echo $input[$rand_keys];

    [/php]

    how about we glob it

    but first are we displaying all pictures for the folder or a few ?

    I’m displaying all the photos in the directory using the foreach loop. This way, all I need to do is upload the photos, and the php script does the rest so theres no maintenance required.

    Here’s the idea:

    1)upload photos to directory
    2)php creates thumbs
    3)php displays all photos & thumbs in gallery
    4)randomize photos so that 1st image changes & content looks “fresh”

    So far I have everything working except the random part.

    but why are you randomizing then if you are displaying all the photos in a gallery

    I want to randomize them so that when repeat visitors come to the site, the first photo in the gallery will be a different image each time. I just want to shuffle the order in which they display.

    [php]
    $glob = glob($thumbs_dir);
    foreach($glob as $file) {
    $input = array($file);
    $file1 = array_rand($input);
    echo ‘

  • ’;
    }

    [/php]

    just goin on a quick whim let me know how that works will fix if dont

    Sponsor our Newsletter | Privacy Policy | Terms of Service