{GET_RANDOM_ALIAS_IMG_URL}

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
User avatar
update
Moderator
Posts: 6430
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

{GET_RANDOM_ALIAS_IMG_URL}

Post by update »

Hi,
I finally solved it I think:

Code: Select all

<?php
/********************************************
titel:          {GET_RANDOM_ALIAS_IMG_URL} Replacement-Tag
description:    {GET_RANDOM_ALIAS_IMG_URL:image-path}
	This Replacement-Tag is based on {RANDOM} tag. It started as a mixup of
	tinoos adapted random code and pepes strip-prefix.php. It creates the
	path info of random pictures related to your point of view in a website
	(like "picture/Image/structure- aliasname/pic1.png".
	This tag can be used at places like table background where no <img>-tag is required.
---------------------------------------------
example:	{GET_RANDOM_ALIAS_IMG_URL:picture/Image} or
		<table style="background-image: url ({GET_RANDOM_ALIAS_IMG_URL:picture/Image});"
		or even <img src="{GET_RANDOM_ALIAS_IMG_URL:picture/Image}"> in an "HTML" contentpart.
		<a href="link-to-somewhere"><img src="{GET_RANDOM_ALIAS_IMG_URL:picture/Image}" border="0"></a>
---------------------------------------------
	The directories "alias1" "alias2" ... are subdirectories of picture/Image
	(you have to create them according to your given aliases "alias1" "alias2"...
	in admin/site structure).
	The complete path to the random pictures is then:
	www.yourdomain.com/picture/Image/alias1/
	www.yourdomain.com/picture/Image/alias2/
	and so on.
	The advantage of defining the alias-dirs in "picture/Image" is that this dir is used by fckeditor
	and you then can upload pictures per FTP and/or per fckeditor.
	It will deliver the pictures of the appropriately defined dir according to where
	you are in the site.
---------------------------------------------
notice:	name this file get_random_alias_img_url.php and copy it to
		phpwcms_template/inc_script/frontend_render
---------------------------------------------
authors:	Claus Szypura	(adapting and mixing up the code)
last modified:  23.01.2006 created/mixed up
********************************************/
function get_random_image_url_based_on_alias($path)
{
// returns a random image url from the given path
// it looks for image of following type: gif, jpg, jpeg, png
	$alias = $GLOBALS['content']['struct'][($GLOBALS['aktion'][0])]['acat_alias'];
	$mypath = ($path."/".$alias);
	$imgArray = array();
	$imgpath = str_replace("//", "/", PHPWCMS_ROOT."/".$mypath."/");
	$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) ?   "".$mypath."/".$imageinfo["imagename"]: "";
}
	$content["all"] = preg_replace('/\{GET_RANDOM_ALIAS_IMG_URL:(.*?)\}/ie', 'get_random_image_url_based_on_alias("$1");', $content["all"]);
?>
I don't know if the copyright notices are ok this way, but this is my first "programming" :oops:
I've tested it and it seems to be ok now :D
Claus

edit:

you can even do it like this (use a "html" cp:
<img src="{GET_RANDOM_ALIAS_IMG_URL:newpics}">
or
<a href="#"><img src="{GET_RANDOM_ALIAS_IMG_URL:newpics}" border="0"></a>

edit:
cleaned it up.
Still missin: a definition for a default picture in case the appropriate dir isn't existing. I think I will go and get myself a php book :!:
Post Reply