random images and timer function?

Use GitHub to post feature requests for phpwcms.
Locked
cyrano
Posts: 1598
Joined: Sat 31. Jan 2004, 18:33
Location: Stuttgart
Contact:

random images and timer function?

Post by cyrano »

hi to all,

i want to use random images but want to use them in a way that it shows one image a day.

how to make this? using a random php function?
does someone know a scriptor setup this yet?

thank you for tips.

EDITED:

I found this one:

Code: Select all

<?php 

// print a random image.  Don't forget ending slash!
// setting $type to 'all' will return all images.
print getRandomImage('../images/'); 

function getRandomImage($dir,$type='random')
{ 
global $errors,$seed; 

  if (is_dir($dir)) {  

  $fd = opendir($dir);  
  $images = array(); 

      while (($part = @readdir($fd)) == true) {  

          if ( eregi("(gif|jpg|png|jpeg)$",$part) ) {
              $images[] = $part; 
          } 
      } 

    // adding this in case you want to return the image array
    if ($type == 'all') return $images;

    if ($seed !== true) {
      mt_srand ((double) microtime() * 1000000);
      $seed = true;
    }
      
      $key = mt_rand (0,sizeof($images)-1); 

    return $dir . $images[$key]; 

  } else { 
      $errors[] = $dir.' is not a directory'; 
      return false; 
  } 
} 

?>
so my question is can i use this microtim to set up 24 h changing?
I'am not familar with php so it would be kind to get a feedback by someone who is :-)

Thank you.
Gruß/ regards cyrano
--------------------------------------------------------
templates -> http://www.128.weitzelmedia.de
planepix -> http://www.planepix.de
XING -> https://www.xing.com/profile/Thomas_Weitzel3
Neelix
Posts: 80
Joined: Wed 29. Sep 2004, 12:06
Location: España

my suggestion

Post by Neelix »

Code: Select all

#
#-------------------------------------------------------
# 					-=[ {RANDOM}-HACK ]=-
#-------------------------------------------------------
#	 4phpwcms: Release 1.1-RC4 27-08-2004
#-------------------------------------------------------
#	  request: www.phpwcms.de/forum/viewtopic.php?t=5605
#			 by: cyrano (tester+new ideas)
# suggestion: extends the RT {RANDOM}
#			 by: Neelix
#	  example: (generic) {RANDOM:[path]:[interval]}
#				  (live)		{RANDOM:picture/my_random_images:1440}
#								This shows your random images which you have placed in a subfolder
#								called "my_random_images" within the folder "picture" with an
#								intervall of 1440 min = 24 hours.
#
#						- [path] = search here for images
#							example:picture/sub
#						- [interval] - using the old value
#						- >0 = interval in minutes
#						- 0 = normal RT
#
# 	 history: v0.1 - stand alone - without core connection
#				 v0.2 - using core {random} with fixed interval
#						  (1time/day)
#				 v0.3 - extends interval in minutes
#-------------------------------------------------------
#
# extends the {RANDOM}-RT to the possibility to 
# create an RANDOM-Value only one times per interval -
# the random-image will now show the whole interval
#
#-------------------------------------------------------

#
#-----[ CREATE AN DB-TABLE ]----------------------------
#
# -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
# if you use an DB-Tableprefix (DB_PREPEND)
# => insert this before phpwcms_random
#	example: CREATE TABLE my_phpwcms_random (
# -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!

CREATE TABLE phpwcms_random (
  rid bigint(20) unsigned NOT NULL default '0',
  datum datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (datum)
) TYPE=MyISAM COMMENT='save values for {RANDOM++}';

#
#-----[ BACKUP ]----------------------------------------
# Backup original file -->include/inc_front/front.func.inc.php
cp front.func.inc.php front.func.inc.php_backup

#
#-----[ OPEN ]------------------------------------------
#
include/inc_front/front.func.inc.php

#
#-----[ FIND ]------------------------------------------
#
$search[15]		= '/\{RANDOM:(.*?)\}/e';
$replace[15]	= 'get_random_image_tag("$1");';

#
#-----[ REPLACE WITH ]------
#
$search[15]		= '/\{RANDOM:(.*?):(\d+)\}/e';
$replace[15]	= 'get_random_image_tag("$1","$2");';

#
#-----[ FIND ]------------------------------------------
#
	function get_random_image_tag($path) {

#
#-----[ REPLACE WITH ]------
#
	function get_random_image_tag($path,$rand) {

#
#-----[ FIND ]------------------------------------------
#
	if(sizeof($imgArray)) $imageinfo = is_random_image($imgArray, $imgpath);

#
#-----[ REPLACE WITH ]------
#
	if(sizeof($imgArray)) $imageinfo = is_random_image($imgArray, $imgpath,0, $rand);

#
#-----[ FIND ]------------------------------------------
#
function get_random_image_tag($path,$rand) {
...
}

#
#-----[ AFTER, ADD ]------
#

   function getRandomID($iInterval=1440)
   {
	   global $db;
		$sQuery = 'SELECT rid FROM '.DB_PREPEND.'phpwcms_random WHERE (NOW()<DATE_ADD(datum, Interval '.$iInterval.' minute));';

      if (isset($db) && $db)
          $hResult = mysql_query($sQuery, $db);
      else
          $hResult = mysql_query($sQuery);
      $row=mysql_fetch_object($hResult);

      return ($row?$row->rid:-1); //0 can be an valid value! => negativ's are save
		
   } //fnc-end

   function saveRandomID($iRID)
   {
	   global $db;
		$sQuery1 = 'DELETE FROM '.DB_PREPEND.'phpwcms_random WHERE DATUM>0';
		$sQuery2 = 'INSERT INTO '.DB_PREPEND.'phpwcms_random(rid,datum) VALUES ('.$iRID.', NOW());';
		
      if (isset($db) && $db)
          $hResult = mysql_query($sQuery1, $db);
      else
          $hResult = mysql_query($sQuery1);
			 
      if (isset($db) && $db)
          $hResult = mysql_query($sQuery2, $db);
      else
          $hResult = mysql_query($sQuery2);
			 
      return $hResult; //ok .or. err
		
   } //fnc-end

#
#-----[ FIND ]------------------------------------------
#
function is_random_image($imgArray, $imagepath, $count=0) {
...
}

#
#-----[ REPLACE WITH ]------ (whole function)
#
function is_random_image($imgArray, $imagepath, $count=0, $rand=0) {

	$count++;
	$rid=-1;//init
	
	//$rand==1 => use the value in the DB
	if ($rand)
	{
		$rid = getRandomID($rand);
		$randval = $rid;
	}
	//if normal || new date: $rid is set to init
	//if extended we need an new id ...
	if ($rid<0)
	{
      mt_srand ((double) microtime() * 1000000); //this is the init/start-value for the mt_rand-algorithm: here "now"
		$randval = mt_rand( 0, sizeof( $imgArray ) - 1 );
		if ($rand) saveRandomID($randval); //... and save it
	}	

	$file = $imagepath.$imgArray[ $randval ];
	//gets -> better tests for image info
	$imageinfo = @getimagesize($file);
	//if $imageinfo is not true repeat function and count smaller count all images
	if(!$imageinfo) {
		if($count < sizeof($imgArray)) $imageinfo = is_random_image($imgArray, $imagepath, $count, $rand);
	} else {
		$imageinfo["imagename"] = $imgArray[ $randval ];
	}
	return $imageinfo;

} //fnc-end
Erfahrung ist das, was man besitzt, kurz nach dem es gebraucht wurde.
Warning: I have no foggiest idea of English/German, but I do
-ha{p}{p}y day/night/dia/noche-
Ibis Fernandez
Posts: 67
Joined: Tue 22. Jun 2004, 19:54
Contact:

Post by Ibis Fernandez »

I have small script set up for generating thumbnails on the fly. Which I plan on using on the front page of my site so people can see a thumbnail of the image of the day first, when they click on it they will then go to the page with the actual image in full size. It's an animators community so basically the purpose of this is to have a daily challenge for animators to practice drawing, a different image every day.

Anyways, the problem im having is that this mod returns the full IMG src html tag, is there a way for me to somehow just be able to retrieve the image path so i can apply it to my link linke this....

Code: Select all

http://www.flashfilmmaker.com/thumbnail.php?im={RANDOM:image}
So that what I get when parsed is something like this:

http://www.flashfilmmaker.com/thumbnail ... s_0010.jpg
Ibis Fernandez
Cinestar Filmworks
http://flashfilmmaker.com
http://cinestarfilmworks.com
Author of Macromedia Flash Animation and Cartooning: A Creative Guide
Pappnase

Post by Pappnase »

hello neelix

would it be possible that yu chnage the script this way we can place it into the frontend_init folder? so there is no change of the source needed. ;-)
Ibis Fernandez
Posts: 67
Joined: Tue 22. Jun 2004, 19:54
Contact:

Very useful THANKS!!!!

Post by Ibis Fernandez »

I managed to pull of a dirty fix but it work great!

Check out http://flashfilmmaker.com on the left hand column, labeled "Todays's Challenge".

This thumbnail is generated on the fly, based on the picture of the day.

If clicked on it will take the artists to the full sized picture which they can use as a refence model to draw... one drawing a day, good way to keep up those pecil skills :)
Ibis Fernandez
Cinestar Filmworks
http://flashfilmmaker.com
http://cinestarfilmworks.com
Author of Macromedia Flash Animation and Cartooning: A Creative Guide
Neelix
Posts: 80
Joined: Wed 29. Sep 2004, 12:06
Location: España

Post by Neelix »

@Pappnase++:
would it be possible that yu chnage the script this way we can place it into the frontend_init folder? so there is no change of the source needed. ;-)
Sure - the most functions are duplicate then but easyer for update etc...
---
I will post the code here (asap)!
========
He we are...

Code: Select all

<?php
/** ---------------------------------------------------------------------------
 *
 *       -=[ Extended {RANDOM} ]=-
 *
 * gently extented the CORE::{RANDOM}-RT to the possibility to
 * create an RANDOM-Value only one times per interval -
 * the random-image will now show the whole interval
 *
 * @4phpwcms 1.2.6DEV (and before?-  not testet yet!)
 * @see request www.phpwcms.de/forum/viewtopic.php?t=5605
 *         by cyrano (1st), pappnase (4frontend_render)
 * @author Neelix
 * @licence "THE BEER-WARE LICENSE" (Revision 42):
 *				Neelix wrote this file.	As long as you retain this notice you
 *				can do whatever you want with this stuff. If we meet some day, and you think
 *				this stuff is worth it, you can buy me a beer in return Neelix.
 * @version 0.4-DEV
 * @see CORE::front.func.inc.php for original {RANDOM}
 *
 * @example generic {XRANDOM:[path]:[interval]}
 *    - [path] = search here for images
 *    - [interval] - using the old value
 *       interval >0   => interval in minutes
 *       interval =0   => like the core RT - new image on every call
 * @example live    {XRANDOM:picture/my_random_images:1440}
 *    This shows your random images which you have placed in a subfolder
 *    called "my_random_images" within the folder "picture" with an
 *    intervall of 1440 min = 24 hours.
 *
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 *
 * Install:
 *
 * -----[ COPY ]-----------------------------------------
 * this file to your render folder 'phpwcms_template/inc_script/frontend_render/'
 *
 * -----[ SQL ]------------------------------------------
 *
 *  -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
 *  if you use an DB-Tableprefix (DB_PREPEND)
 *  => insert this before phpwcms_random
 *   example: CREATE TABLE my_phpwcms_random (
 * -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
 *
 * CREATE TABLE phpwcms_random (
 * rid bigint(20) unsigned NOT NULL default '0',
 * datum datetime NOT NULL default '0000-00-00 00:00:00',
 * PRIMARY KEY  (datum)
 * ) TYPE=MyISAM COMMENT='save values for {XRANDOM}';
 *
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 *
 *    history: v0.1 - stand alone - without core connection
 *             v0.2 - using core {random} with fixed interval
 *                    (1time/day)
 *             v0.3 - extends interval in minutes
 *             v0.4 - build 4 frontend_render, rename to XRANDOM
 * ----------------------------------------------------------------------------
 */

class ClExtRandom
{
	var $_imgArray;			/** @private Array[String] */
	var $_imgPath;				/** @private String */

  /**
   * Konstruktor:
	*/
	function ClExtRandom()
	{
		$this->_imgArray = array();
		$this->_imgPath = '';
	} //fnc-end

  /**
	* returns an random image (html-tag) from the give path
	*	- it looks for image of following type:
	*	- gif, jpg, jpeg, png
	*
	* @param String $path look here for images
	* @param Integer $rand interval in min
	* @return String html-img-tag
	* @access public
	*/
	function getRandomImageTag($path,$rand)
	{
		$this->_imgPath = str_replace('//', '/', PHPWCMS_ROOT.'/'.$path.'/');
		$imageinfo = false;

		if (is_dir($this->_imgPath))
		{
			$handle = opendir( $this->_imgPath );
			while($file = readdir( $handle ))
			{
   			if ( $file != "." && $file != ".." )
				{
					if ( preg_match('/(\.jpg|\.jpeg|\.gif|\.png)$/', strtolower($file)) )
						 $this->_imgArray[] = $file;
				}
			}
			closedir( $handle );
		}

		if (count($this->_imgArray)) $imageinfo = $this->_isRandomImage(0, $rand);

		return ($imageinfo ? '<img src="'.$path.'/'.$imageinfo["imagename"].'" '.$imageinfo[3].' border="0" alt="'.$imageinfo["imagename"].'" />' : '');

	} //fnc-end

  /**
	* get the last id from DB
	* @param Integer $iInterval interval in min, optional default=1440
	* @return Integer get the last value .or. -1 if nothing found
	* @access privat
	*/
   function _getRandomID($iInterval=1440)
   {
		$sQuery = 'SELECT rid FROM '.DB_PREPEND.'phpwcms_random WHERE (NOW()<DATE_ADD(datum, Interval '.$iInterval.' minute));';

      $hResult = mysql_query($sQuery, $GLOBALS['db']);
      $row = mysql_fetch_object($hResult);

      return ($row?$row->rid:-1); //0 can be an valid value! => negativ's are save

   } //fnc-end

  /**
	* save the new id in the DB
	* - update is also possible, if you get an easy PK for the table; but now I get this way...
	* @param Integer $iRID new ID
	* @return Boolean sucessful or error
	* @access privat
	*/
   function _saveRandomID($iRID)
   {
		$sQuery1 = 'DELETE FROM '.DB_PREPEND.'phpwcms_random WHERE DATUM>0';
		$sQuery2 = 'INSERT INTO '.DB_PREPEND.'phpwcms_random(rid,datum) VALUES ('.$iRID.', NOW());';

      $hResult = mysql_query($sQuery1, $GLOBALS['db']);
	   $hResult = mysql_query($sQuery2, $GLOBALS['db']);

      return $hResult; //ok .or. err

   } //fnc-end

  /**
	* check 4 valid image
	* @param Integer $count Counter , optional default=0 (4error: call again max size of _imgArray)
	* @param Integer $rand Last Random ID, optional default=0
	* @return Array with image infos
	* @see PHP::getimagesize()
	* @access privat
	*/
	function _isRandomImage($count=0, $rand=0)
	{

		$count++;
		$rid=-1;			//init
		$randval=0;		//-=-

		//$rand==1 => use the value in the DB
		if ($rand)
		{
			$rid = $this->_getRandomID($rand);
			$randval = $rid;
		}
		//if normal || new date: $rid is set to init
		//if extended we need an new id ...
		if ($rid<0)
		{
      	mt_srand ((double) microtime() * 1000000); //this is the init/start-value for the mt_rand-algorithm: here "now"
			$randval = mt_rand( 0, count($this->_imgArray) - 1 );
			if ($rand) $this->_saveRandomID($randval); //... and save it
		}

		$file = $this->_imgPath . $this->_imgArray[ $randval ];

		//gets -> better tests for image info
		$imageinfo = @getimagesize($file);
		//if $imageinfo is not true repeat function and count smaller count all images
		if(!$imageinfo)
		{
			if($count < count($this->_imgArray)) $imageinfo = $this->_isRandomImage($count, $rand);
		}
		else
		{
			$imageinfo["imagename"] = $this->_imgArray[ $randval ];
		}
		return $imageinfo;

	} //fnc-end

};//end-class



//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//anybody out there? - the same old story, but here in CLASS-manner (I hope it's possible in older PHPs to...)
if (!strpos($content['all'],'{XRANDOM:')===false)
{
	$myRand = new ClExtRandom();
	$content['all'] = preg_replace('/\{XRANDOM:(.*?):(\d+)\}/e', '$myRand->getRandomImageTag("$1","$2")', $content['all']);
}

?>
tested only with 1.2.6DEV!!!! (yet)

:wink:
Erfahrung ist das, was man besitzt, kurz nach dem es gebraucht wurde.
Warning: I have no foggiest idea of English/German, but I do
-ha{p}{p}y day/night/dia/noche-
cyrano
Posts: 1598
Joined: Sat 31. Jan 2004, 18:33
Location: Stuttgart
Contact:

Post by cyrano »

hi all,

want to ask if someone knows how to change the script to run in seconds instead of minutes?

Thank you for a hint.
Gruß/ regards cyrano
--------------------------------------------------------
templates -> http://www.128.weitzelmedia.de
planepix -> http://www.planepix.de
XING -> https://www.xing.com/profile/Thomas_Weitzel3
pepe
Posts: 3954
Joined: Mon 19. Jan 2004, 13:46

Post by pepe »

Hallo cyrano....

mal ein ganz heißer tipp... 1 Minute = 60 Sekunden Vielleicht ist das ja ein guter Ansatz :lol: :lol: :lol:
cyrano
Posts: 1598
Joined: Sat 31. Jan 2004, 18:33
Location: Stuttgart
Contact:

Post by cyrano »

:-) danke pepe.

Das script arbeitet aber nur in minuten, das kleinste intervall ist eine minute, daher die frage.

:shock:
Gruß/ regards cyrano
--------------------------------------------------------
templates -> http://www.128.weitzelmedia.de
planepix -> http://www.planepix.de
XING -> https://www.xing.com/profile/Thomas_Weitzel3
Neelix
Posts: 80
Joined: Wed 29. Sep 2004, 12:06
Location: España

Post by Neelix »

Hola 8)

---[FIND]---
function _getRandomID($iInterval=1440)
{
$sQuery = 'SELECT rid FROM '.DB_PREPEND.'phpwcms_random WHERE (NOW()<DATE_ADD(datum, Interval '.$iInterval.' minute));';

---[replace]---
minute
---[with]---
SECOND

¡ tata ! :wink:
Erfahrung ist das, was man besitzt, kurz nach dem es gebraucht wurde.
Warning: I have no foggiest idea of English/German, but I do
-ha{p}{p}y day/night/dia/noche-
cyrano
Posts: 1598
Joined: Sat 31. Jan 2004, 18:33
Location: Stuttgart
Contact:

Post by cyrano »

boah ey, jetzt aus spanien. Danke. dachte nicht das es so einfach ist ;-)

DANKE. Gracias.

one more question neelix:

1. I put the php file in frontend_render folder
2. i put the code {XRANOM:picture/random:10} in a HTMl CP.

when i call the page i see no change of images when i wait for more than 10 sec, also 4 minutes?

does the script pulls itself the images from the folder or do i need a reload?
Gruß/ regards cyrano
--------------------------------------------------------
templates -> http://www.128.weitzelmedia.de
planepix -> http://www.planepix.de
XING -> https://www.xing.com/profile/Thomas_Weitzel3
Neelix
Posts: 80
Joined: Wed 29. Sep 2004, 12:06
Location: España

Post by Neelix »

Hola again...

und JA in Spanesien ist es auch 8) ...
cyrano wrote: when i call the page i see no change of images when i wait for more than 10 sec, also 4 minutes?
does the script pulls itself the images from the folder or do i need a reload?
Mein Denglish is not so good...
- lock to the release before - frontend_render folder is ok (I hope)
- there is no (auto-)refresh by timer or <meta> tag, if you don't reload the page, you don't see an new picture?!
- the script read all (gif|jpg|png|jpeg)pictures in the folder (e.g. picture/random) and get on per random, but only if you call the page again (and the script start with an new random access...)
Wenn es das war :wink:

(PS ok der Footer... gibt gleich nen neuen ;))
Erfahrung ist das, was man besitzt, kurz nach dem es gebraucht wurde.
Warning: I have no foggiest idea of English/German, but I do
-ha{p}{p}y day/night/dia/noche-
User avatar
StudioZ
Posts: 802
Joined: Fri 28. May 2004, 19:57
Location: Québec, Canada
Contact:

Post by StudioZ »

Hey! Hello Neelix ;)
Good to see you back again :) 8)

Cheers,

Yves
Image
PhpWCMS Evangelist, -- iRoutier.com Running phpWCMS 1.4.2, r354 -> Great Version!!!!
Locked