{RANDOM_IMG_URL} Replacement Tag

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
tinoo
Posts: 311
Joined: Thu 16. Jun 2005, 11:16
Location: Zürich, Switzerland
Contact:

{RANDOM_IMG_URL} Replacement Tag

Post 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"]);
    // -------------------------------------------------------------
?>
Greetz, tinoo
hendrik250874
Posts: 32
Joined: Mon 16. Feb 2004, 13:10
Location: Belgium
Contact:

very nice

Post 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.
tinoo
Posts: 311
Joined: Thu 16. Jun 2005, 11:16
Location: Zürich, Switzerland
Contact:

Re: very nice

Post 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... ;-)
Greetz, tinoo
kobos
Posts: 52
Joined: Mon 21. Nov 2005, 21:50

Post by kobos »

do you have physic powers?

this is EXACTLY what im looking for! i couldnt't actually ask for more
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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
kobos
Posts: 52
Joined: Mon 21. Nov 2005, 21:50

Post 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
Image
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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
kobos
Posts: 52
Joined: Mon 21. Nov 2005, 21:50

Post 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
Image
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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:
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post 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
frankie44
Posts: 13
Joined: Thu 13. Apr 2006, 19:28

Post 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
Rombert
Posts: 1
Joined: Sat 30. Dec 2006, 02:50

Problem

Post 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...
Post Reply