Page 1 of 1

{RANDOM_IMG_URL} Replacement Tag

Posted: Wed 27. Jul 2005, 10:47
by tinoo
Hi Folks

This is my first Replacement Tag. Well, it's not really my tag. It's actually
a modification of the {RANDOM}-tag... Safe this piece of code into a file
(for example reptag_random_img_url.php) and copy it to phpwcms_template\inc_script\frontend_render. Instructions are inside the
code...

Because i'm a php-noob, no support is given... ;-)

Code: Select all

<?php

/********************************************
titel:          {RANDOM_IMG_URL} Replacement-Tag
description:    {RANDOM_IMG_URL:image-path}
		This Replacement-Tag is based on {RANDOM} tag, but creates only the
		path informations of random pictures, like "picture/random-title/header2.jpg".
		Use this tag at places like table background where no <img>-tag is required.


example:	{RANDOM_IMG_URL:picture/random-pics} or
		<table style="background-image: url({RANDOM_IMG_URL:picture/random-pics});"

		The directory random-pics is a subdirectory of picture.
		The complete path to the random pictures is then:
		www.yourdomain.com/picture/random-pics/

notice:		copy this file to phpwcms_template\inc_script\frontend_render
                
author:         Martin Heggli
last modified:  27.07.2005 created
********************************************/

function get_random_image_url($path) {
	// returns an random image url from the give path
	// it looks for image of following type:
	// gif, jpg, jpeg, png

	$imgArray = array();
	$imgpath = str_replace("//", "/", PHPWCMS_ROOT."/".$path."/");
	$imageinfo = false;

	if(is_dir($imgpath)) {
		$handle = opendir( $imgpath );
		while($file = readdir( $handle )) {
   			if( $file != "." && $file != ".." ) {
				if( preg_match('/(\.jpg|\.jpeg|\.gif|\.png)$/', strtolower($file)) ) $imgArray[] = $file;
			}
		}
		closedir( $handle );
	}
	
	if(sizeof($imgArray)) $imageinfo = is_random_image($imgArray, $imgpath);
	//return $imageinfo;
	return ($imageinfo) ?	"".$path."/".$imageinfo["imagename"]: "";

}
     
    // -------------------------------------------------------------
    $content["all"] = preg_replace('/\{RANDOM_IMG_URL:(.*?)\}/e', 'get_random_image_url("$1");', $content["all"]);
    // -------------------------------------------------------------
?>

very nice

Posted: Wed 27. Jul 2005, 17:06
by hendrik250874
It's like you did that especially for me ;-)
You have no idea how weird this is. I had my solution ready; something where I generate a random number and put it in a $GLOBAL and then echo it somewhere. And I think, how about checking the forum once more. And then this.

Thanks again.

Re: very nice

Posted: Wed 27. Jul 2005, 17:51
by tinoo
hendrik250874 wrote:It's like you did that especially for me ;-)
Maybe i did... At least i did it for me and the comunity... ;-)

Posted: Thu 1. Dec 2005, 18:00
by kobos
do you have physic powers?

this is EXACTLY what im looking for! i couldnt't actually ask for more

Posted: Fri 20. Jan 2006, 15:11
by update
Hello
I've installed this random_img_url-script and it is working really fine. Now I try to apply it to the main table tag of the page so the background of the main table is switching.
Unfortunately it isn't working :(

If I apply the tag as recommended in the html for the head, it is working (style="and so on...") displaying a background for the head table.. Applied as a class in the backend Template creation it isn't rendered...

My plan: have a ramdomly switching background for the head, main and footer part of the page.
If there is anybody out there with an idea how to solve this, please help and give me some advice.
Many thanks in advance
Claus

Posted: Fri 20. Jan 2006, 16:17
by kobos
This hack just gives a random url, it doesnt display images, use the {RANDOM:image_path} tag

Make sure the images are all the same dimensions! Also have different folders for each section

Posted: Fri 20. Jan 2006, 17:02
by update
Hi kobos
many thanks for your reply.
This hack is displaying pictures (e.g.g background pictures) in a random manner - I have tried it.
But what I was going to try:
have it as a style definition in the very first table so it can display a backghround picture all over the page - randomly!
If I put it in my head table then it is only displaying the background picture within the head area.

Code: Select all

.
.
.
//-->
</script>

</head>

<body text="#000000" link="#FF9900" vlink="#FF9900" alink="#FF9900" style="margin:0px 0px 0px 0px;">
<table border="0" cellspacing="0" cellpadding="0" width="760" class="main_bg" align="center">
<tr>
<td colspan="5">...............
the stylesheet for main_bg contains

Code: Select all

.main_bg {
	background-image: url({RANDOM_IMG_URL:../newpics/pagebackheads});
		background-repeat: no-repeat;
	background-position: left top;
	background-color: #FFFFFF;
}
It would probably be working if I could insert it into the beforementioned table tag like this

Code: Select all

<table border="0" cellspacing="0" cellpadding="0" width="760" style="background-image: url({RANDOM_IMG_URL:picture/random-pics});"
But I can only apply a class without changing the script code :(

Perhaps there is a possibility but I don't see it

Claus

Posted: Fri 20. Jan 2006, 18:33
by kobos
this works...

Code: Select all

<div style="background-image: url({RANDOM_IMG_URL:images/bg})">
<p> okey dokey doooo</p>
</div>
UPDATE:

it works yet doesnt seem to work randomly

Posted: Fri 20. Jan 2006, 18:44
by update
Kobos,
it seems to be a rendering sequence problem: if applied in a class, everything is displayed but the picture.
As I understand your solution the div is applied against the body bg?
I will try too.
But my Template is table based and for applying a renderable tag it probably has to be inserted directly?? into the table
Claus

Posted: Fri 20. Jan 2006, 21:16
by update
Ok,
to whom it may concern ;)

this is how I hot-knitted the solution for getting the {RANDOM_IMG_URL} Replacement Tag working for the background of the main table in page layout:

I simply inserted this code into the class input field for 'main' in the page layout section

Code: Select all

main_bg" style="background-image: url({RANDOM_IMG_URL:picture/random-pics});
placing the " was the trick, because the phpwcms page layout generator then wrote

Code: Select all

class="main_bg" style="background-image: url({RANDOM_IMG_URL:picture/random-pics});"
and after the first reload there was it:

Code: Select all

class="main_bg" style="background-image: url(picture/random-pics/dieselpumpe.jpg);"
And it works randomly like nothing now.
Puha! Soo simple
Claus :lol:

Posted: Fri 20. Jan 2006, 21:46
by update
Has anybody an idea how to substitute the "random-pics" with the alias for the page level in dependance of where I move around in a site? Something like context sensitive...
Greetings
Claus

Posted: Sat 21. Jan 2006, 18:39
by update
Hi,
I managed to get it working - one little error still.

What does it do? It randomly displays a picture from a given path

Code: Select all

example:   {GET_RANDOM_ALIAS_IMG_URL:all_needed_pictures}

 or
      <table style="background-image: url({GET_RANDOM_ALIAS_IMG_URL:all_needed_pictures});"
In this dir "all_needed_pictures" I then define some subdirs _dir1 _dir2 _dir3

The underscore is becuse I reused pepes "get_alias" script (should then work with different languages too)...

I then mxed this script with tinoos script "reptag_random_img_url"

Here it is

Code: Select all

<?php

/********************************************
titel:          {RANDOM_IMG_URL} Replacement-Tag
description:    {RANDOM_IMG_URL:image-path}
      This Replacement-Tag is based on {RANDOM} tag, but creates only the
      path informations of random pictures, like "picture/random-title/header2.jpg".
      Use this tag at places like table background where no <img>-tag is required.


example:   {RANDOM_IMG_URL:picture/random-pics} or
      <table style="background-image: url({RANDOM_IMG_URL:picture/random-pics});"

      The directory random-pics is a subdirectory of picture.
      The complete path to the random pictures is then:
      www.yourdomain.com/picture/random-pics/

notice:      copy this file to phpwcms_template\inc_script\frontend_render
               
author:         Martin Heggli
last modified:  27.07.2005 created
********************************************/

// copyright by Manfred Peperkorn :: strip-prefix.php
// find alias-name - strip prefix - give back name without prefix
//    alias:    de_aliasname-example
//    strip:    de
//    give back: _aliasname-example

function get_random_image_url($path) {
// returns an random image url from the give path
// it looks for image of following type:
// gif, jpg, jpeg, png


   $alias = $GLOBALS['content']['struct'][($GLOBALS['aktion'][0])]['acat_alias'];
   $search = "_";
   $aliasNOprefix = stristr($alias,$search);
   //echo $aliasNOprefix;

   $imgArray = array();
   $imgpath = str_replace("//", "/", PHPWCMS_ROOT."/".$path."/".$aliasNOprefix."/");
   $imageinfo = false;

   if(is_dir($imgpath)) {
      $handle = opendir( $imgpath );
      while($file = readdir( $handle )) {
            if( $file != "." && $file != ".." ) {
            if( preg_match('/(\.jpg|\.jpeg|\.gif|\.png)$/', strtolower($file)) ) $imgArray[] = $file;
         }
      }
      closedir( $handle );
   }
   
   if(sizeof($imgArray)) $imageinfo = is_random_image($imgArray, $imgpath);
   //return $imageinfo;
   return ($imageinfo) ?   "".$path."/".$aliasNOprefix."/".$imageinfo["imagename"]: "";

}
     
    // -------------------------------------------------------------
    $content["all"] = preg_replace('/\{GET_RANDOM_ALIAS_IMG_URL:(.*?)\}/e', 'get_random_image_url("$1");', $content["all"]);
    // -------------------------------------------------------------
?>
I saved it as reptag_get_random_alias_img_url.php

This is working and displays the pictures related to the defined aliases from the pagelayout (aliases in this examle: _dir1 _dir2 _dir3)

But it also displays the name of the dir/alias in the upperleft corner of the page right before the page itself is displayed :(

Code: Select all

_test<!DOCTYPE HTML PUBLIC .......
Since I'M no programmer at all I wasn't able to iron it out, so please have a look and give me a helping hand.

Many thanks
Claus

Posted: Mon 9. Oct 2006, 14:53
by frankie44
Hi, Tinoo!

Years later, but... Thanx for this helpful RT! I was fiddling around for hours to get random background images working, now I solved this problem in a couple of minutes thanks to your little pieces of code! Great!

Many greetings - Gabi

Problem

Posted: Sat 30. Dec 2006, 02:57
by Rombert
I can't get this code (random_img_url) to work in my css file. Could anyone have a look at it, since I'm not very experienced with php? Am I putting in the wrong code, or doesn't this code work in css? If the latter is correct, how can I solve my problem in the css file?

Code: Select all

/*===========================*/
/*== CSS VOOR HEADER-BLOCK ==*/
/*===========================*/

#navigatie { display: inline; margin-right: 10px; font: Arial;}
#navigatie a, #navigatie a:link, #navigatie a:active, #navigatie a:visited {
	border: 1px white; color: black; font: 14px bold; font-family: Arial; }
div#navigatie span a:first-letter { text-transform: capitalize }
#navigatie a:hover { display: inline; border: 1px black; text: underline; font: 14px bold; font-family: Arial; }
#list_top_ul li a#list_top_active_link, #list_top li a#list_top_home_active_link {
	background: white;
	border-bottom: 1px solid white; }
#list-top_ul span {	color: #a84122; }

#headerBlock {
	width: 770px;
	margin:0;
	padding:0;border:0;margin:0;
	padding-bottom: 2px;
	border: 0;
	border-top: 15px solid white;
	b/order-bottom: 1px dotted #aaa;
	text-align: right;
	background: url({RANDOM_IMG_URL:../../img/layout/top}) right no-repeat ; 

}
Cheers for helping...