Render random images from filesystem

Just created a small widget, that renders $num_of_images Images that may be located in $path:




class RandomImages extends CWidget

{

  public $path;

  public $num_of_images;


    public function init() {

      parent::init();

    }


    public function run() {

      $images = scandir($path);

      $imagearray = array();

      foreach($images as $img) {

              if((strstr($img, "png") || strstr($img, "jpg"))) {

                      $imagearray[] = $img;

              }

      }

      // hint: remove the shuffle-line to display the images sorted by filename

      shuffle($imagearray);

      $imagearray = array_chunk($imagearray, $num_of_images);

      $this->render("images", array('path' => $path, 'images' => $imagearray[0]));

    }

}




The view may look like:




<?php

foreach($images as $image) : ?>

  <img style="border: 1px solid black;padding:3px;" src="<?php echo $path.$image; ?>" width=50 height=50 />

<?php endforeach; ?>

?>



Usage Example:




$this->widget("RandomImages", array('path' => '/tmp', 'num_of_images' => '5'));



thanks to share~

You may want to add some translation for file system path VS http path

nz