Jump to content

Simple Function


zhx

Recommended Posts

Hello, I'm running a blog powered by Wordpress. In my header, there is a simple function that returns a random masthead, like this:

 

function header_graphic() {
echo "/images/";
$num=rand(0,1);
echo "masthead".$num.".jpg";

 

This (obviously) returns masthead0.jpg, masthead1.jpg, randomly. My question is, is there a simple way I could modify this so that I could create my mastheads in a variety of graphics formats? For example, make the function randomly return one of masthead0.jpg, masthead1.png, or masthead2.gif? JPG doesn't suit all my graphics the best...

Link to comment
Share on other sites

<?php

function header_graphic() {
echo "/images/";
$num=rand(0,1);
        $fileType = rand(1,3);

            if ($fileType == "1"){ $type = ".jpg";}
            if ($fileType == "2"){ $type = ".gif";}
            if ($fileType == "3"){ $type = ".png";}
         
echo "masthead".$num.$type;

?>

Link to comment
Share on other sites

I think that's the right idea but if I have, for example, masthead2.png, wouldn't we run into the problem that that function could possibly return masthead1.png, which doesn't (in this example) exist? Or am I reading it wrong?

Link to comment
Share on other sites

Why limit the extension or name? Just create a directory with the images you want and nothing else.. and then:

 

 

<?php
function change_header($dir) {
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $array[] = $file;
        }
    }
    closedir($handle);
}
$n = rand(0,count($array)-1);
return $dir.$array[$n];
}
echo change_header("images/");
?>

 

 

 

............. If you need to keep it in the /images and with other files you can limit it to the masthead name:

 

<?php
function change_header($dir) {
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        	if (preg_match("/masthead/", $file)) {
            	$array[] = $file;
            }
        }
    }
    closedir($handle);
}
$n = rand(0,count($array)-1);
return $dir.$array[$n];
}
echo change_header("images/");
?>

Link to comment
Share on other sites

Wow, that would work great, only I see now that my problem runs a little deeper than this. See, when I created this Wordpress database, my URL was different. It has since changed, but if I use $dir, it seems to be trying to look for /images in the old URL.

 

Say my old URL was www.oldsite.com, and my new URL www.newsite.com, the error message I'm getting is:

 

Warning: opendir(images/): failed to open dir: No such file or directory in /home/oldsite/public_html/wp-content/themes/theme1/header.php on line 5

images/ #header { background: url("http://www.newsite.com/wp-content/themes/theme1

Fatal error: Call to undefined function: header_graphic() in /home/oldsite/public_html/wp-content/themes/theme1/header.php on line 32

 

How can I update my database to refer to the correct URL/directory (/home/newsite/public_html/wp-content/themes/theme1/images/), or is that way out of the scope of this forum?

Link to comment
Share on other sites

Build some multi D arrays

$multiimage['jpg'][0] = 1;
$multiimage['jpg'][1] = 1;
$multiimage['jpg'][2] = 1;
$multiimage['jpg'][3] = 1;

$multiimage['png'][0] = 1;
$multiimage['png'][1] = 1;
$multiimage['png'][2] = 1;

switch $rndimagtype
{
case 1:
$type = "jpg";
break;

case2:
$type = "png";
break;
default:
$type = "jpg";
}

if (!ISSET($multiimage[$type][$rndnum])
{
function change_header();
}

Link to comment
Share on other sites

<?php
function change_header($dir, $abs) {
if ($handle = opendir($abs)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        	if (preg_match("/masthead/", $file)) {
            	$array[] = $file;
            }
        }
    }
    closedir($handle);
}
$n = rand(0,count($array)-1);
return $dir.$array[$n];
}
change_header("/relative/path/to", "/absolue/path/to");
?>

Link to comment
Share on other sites

Complete Function:


function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage['jpg'][0] = 1;
$multiimage['jpg'][1] = 1;
$multiimage['jpg'][2] = 1;
$multiimage['jpg'][3] = 1;

$multiimage['png'][0] = 1;
$multiimage['png'][1] = 1;
$multiimage['png'][2] = 1;

$num=rand(0,5);
$imgtype=rand(1,2);

//Add More cases for more types and adjust limits on $imgtype in same way
switch $imgtype
{
case 1:
$type = "jpg";
break;

case2:
$type = "png";
break;
default:
$type = "jpg";
}

if (!ISSET($multiimage[$type][$num])
{function header_graphic()}
else
{
echo "/images/";
echo "masthead".$num.".$type;
}
}

 

Link to comment
Share on other sites

<?php
function change_header($dir, $abs) {
if ($handle = opendir($abs)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        	if (preg_match("/masthead/", $file)) {
            	$array[] = $file;
            }
        }
    }
    closedir($handle);
}
$n = rand(0,count($array)-1);
return $dir.$array[$n];
}
change_header("/relative/path/to", "/absolue/path/to");
?>

 

Seems that even with an absolute link, it's still saying that the directory is not implemented in /home/oldsite/public_html/wp-content/themes/theme1/header.php

Link to comment
Share on other sites

That's a server issue, not PHP.

 

Put a file in the images folder, and view it in the browser with:

 

<?php
echo getcwd()."/";
?>

 

Use whatever it outputs as the absolute path.

 

If the /images is on a different server completely, it will not work.

Link to comment
Share on other sites

Complete Function:


function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage['jpg'][0] = 1;
$multiimage['jpg'][1] = 1;
$multiimage['jpg'][2] = 1;
$multiimage['jpg'][3] = 1;

$multiimage['png'][0] = 1;
$multiimage['png'][1] = 1;
$multiimage['png'][2] = 1;

$num=rand(0,5);
$imgtype=rand(1,2);

//Add More cases for more types and adjust limits on $imgtype in same way
switch $imgtype
{
case 1:
$type = "jpg";
break;

case2:
$type = "png";
break;
default:
$type = "jpg";
}

if (!ISSET($multiimage[$type][$num])
{function header_graphic()}
else
{
echo "/images/";
echo "masthead".$num.".$type;
}
}

 

 

I tried this, and this is the error I get:

 

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/oldsite/public_html/wp-content/themes/theme1/header.php

 

Where is it expecting a (?

Link to comment
Share on other sites

That's a server issue, not PHP.

I figured it was a SQL thing. If you use $dir, where does your script pull this URL from?

 

Put a file in the images folder, and view it in the browser with:

 

<?php
echo getcwd()."/";
?>

 

Use whatever it outputs as the absolute path.

 

If the /images is on a different server completely, it will not work.

 

Sorry, what? You're dealing with a moron.

Link to comment
Share on other sites

I think this line:

if (!ISSET($multiimage[$type][$num])

 

is missing a ')' at the end.

 

if (!ISSET($multiimage[$type][$num]))

 

It says it's on line 15, which, in the context of my header.php page, is this line:

switch $imgtype

 

Nevertheless, I tried to put a ) where you said, and I get the same error.

Link to comment
Share on other sites

Full Proof Function Tested it out  It also will load a default if it fails 15 times

function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage["jpg"][0] = 1;
$multiimage["jpg"][1] = 1;
$multiimage["jpg"][2] = 1;
$multiimage["jpg"][3] = 1;

$multiimage["png"][0] = 1;
$multiimage["png"][1] = 1;
$multiimage["png"][2] = 1;

$num = rand(0,5);
$imgtype = rand(1,2);

//Add More cases for more types and adjust limits on $imgtype in same way
switch ($imgtype)
{
case 1:
	$type = "jpg";
break;
case 2:
	$type = "png";
break;
default:
$type = "jpg";
}

//Incase we fail a lot
$deafult = "0.jpg";
$faillimit = 15;
if (!ISSET($multiimage[$type][$num]))
{
$failcount++;
if ($failcount <= $faillimit)
{
	header_graphic();
}
else
{echo "/images/masthead".$default;}
}
else
{
echo "/images/";
echo "masthead".$num.".".$type;
}
}
header_graphic();

Link to comment
Share on other sites

Full Proof Function Tested it out  It also will load a default if it fails 15 times

function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage["jpg"][0] = 1;
$multiimage["jpg"][1] = 1;
$multiimage["jpg"][2] = 1;
$multiimage["jpg"][3] = 1;

$multiimage["png"][0] = 1;
$multiimage["png"][1] = 1;
$multiimage["png"][2] = 1;

$num = rand(0,5);
$imgtype = rand(1,2);

//Add More cases for more types and adjust limits on $imgtype in same way
switch ($imgtype)
{
case 1:
	$type = "jpg";
break;
case 2:
	$type = "png";
break;
default:
$type = "jpg";
}

//Incase we fail a lot
$deafult = "0.jpg";
$faillimit = 15;
if (!ISSET($multiimage[$type][$num]))
{
$failcount++;
if ($failcount <= $faillimit)
{
	header_graphic();
}
else
{echo "/images/masthead".$default;}
}
else
{
echo "/images/";
echo "masthead".$num.".".$type;
}
}
header_graphic();

 

I got this working, but for no good reason, messes up the formatting on my menu bar, which is directly underneath my masthead. It also makes my front page load noticeably slower (about three full seconds to load main page).

Link to comment
Share on other sites

Try this... It is kind of hard to explain how to create a php file in your images directory on that other post.....

 

Heres a function-less basic script:

 

<?php
//The URL you go to when you view the images directory
$http_path = "http://www.yourdomain.com/images/";
//The servers path to your image directory
$server_path = "/www/yourdomain.com/public/images/";

if ($handle = opendir($server_path)) {
while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
		$array[] = $file;
    	}
}
closedir($handle);
$n = rand(0,count($array)-1);
echo "<img src=\"".$http_path.$array[$n]."\">";
}

?>

Link to comment
Share on other sites

I moved it outside and did a bit of rework tell me if it helps

<?php
function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage["jpg"][0] = 1;
$multiimage["jpg"][1] = 1;
$multiimage["jpg"][2] = 1;
$multiimage["jpg"][3] = 1;

$multiimage["png"][0] = 1;
$multiimage["png"][1] = 1;
$multiimage["png"][2] = 1;

//Incase we fail a lot
$deafult = "0.jpg";
$faillimit = 15;
do
{
if($failcount >$faillimit)
{
	echo "/images/masthead".$deafult;
}
else
{
	$failcount++;
	$num = rand(0,3);
	$imgtype = rand(1,2);
//Add More cases for more types and adjust limits on $imgtype in same way
	switch ($imgtype)
	{
		case 1:
			$type = "jpg";
		break;
		case 2:
			$type = "png";
		break;
		default:
		$type = "jpg";
	}
}

}
While(!ISSET($multiimage[$type][$num]) && $failcount <= $faillimit);
if ($failcount <=$faillimit)
{
echo "/images/";
echo "masthead".$num.".".$type;
}
}
header_graphic()
?>

Link to comment
Share on other sites

Try this... It is kind of hard to explain how to create a php file in your images directory on that other post.....

 

Heres a function-less basic script:

 

<?php
//The URL you go to when you view the images directory
$http_path = "http://www.yourdomain.com/images/";
//The servers path to your image directory
$server_path = "/www/yourdomain.com/public/images/";

if ($handle = opendir($server_path)) {
while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
		$array[] = $file;
    	}
}
closedir($handle);
$n = rand(0,count($array)-1);
echo "<img src=\"".$http_path.$array[$n]."\">";
}

?>

 

This works by itself...but how would I make this operate as "function header_graphic()" so I don't have to change the calls to this function? Or is there a simpler way to implement this in the context of my page?

Link to comment
Share on other sites

I moved it outside and did a bit of rework tell me if it helps

<?php
function header_graphic() {
//Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there)
$multiimage["jpg"][0] = 1;
$multiimage["jpg"][1] = 1;
$multiimage["jpg"][2] = 1;
$multiimage["jpg"][3] = 1;

$multiimage["png"][0] = 1;
$multiimage["png"][1] = 1;
$multiimage["png"][2] = 1;

//Incase we fail a lot
$deafult = "0.jpg";
$faillimit = 15;
do
{
if($failcount >$faillimit)
{
	echo "/images/masthead".$deafult;
}
else
{
	$failcount++;
	$num = rand(0,3);
	$imgtype = rand(1,2);
//Add More cases for more types and adjust limits on $imgtype in same way
	switch ($imgtype)
	{
		case 1:
			$type = "jpg";
		break;
		case 2:
			$type = "png";
		break;
		default:
		$type = "jpg";
	}
}

}
While(!ISSET($multiimage[$type][$num]) && $failcount <= $faillimit);
if ($failcount <=$faillimit)
{
echo "/images/";
echo "masthead".$num.".".$type;
}
}
header_graphic()
?>

 

Works, but again, messes up my formatting for no apparent reason, and pages load slooooow.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.