Jump to content

Recommended Posts

Hi there... I'm an old html junkie; have learned a little perl and a little PHP and have gone thru MANY CMS systems before I settled on PHP-Fusion. I'm pretty good at manipulating other peoples code (mainly for cosmetics, screen output etc.), but I'm VERY new at trying to manipulate PHP. Here's my problem. Fusion is now up to version 7.0 though still RC1... on version 6.0 there was a mod for an avatar gallery which functions very well. Though my fav avatar gallery method exists in phpBB2 and 3. I have taken the code from phpBB2 and am trying to modify it to work on PHP-Fusion. What I have so far works as far as displaying ALL the avatars in the sub-directory. Select box displays the sub-dir names just fine; problem is when I choose another sub-dir and hit the 'Go' button, the directory won't change... I haven't even gotten to the actual avatar selection yet which is a future problem to solve but I have to be able to change dir's first. Here is the code and I will highlight (couldn't change color in code block) where I think I need more logic:

$images="";
$name="";


$avatarfolder= IMAGES."avatars/avatar_gallery";
$dir = opendir($avatarfolder);

$avatar_images = array();
while( $file = readdir($dir) )
{
	if( $file != '.' && $file != '..' && !is_file($avatarfolder . '/' . $file) && !is_link($avatarfolder . '/' . $file) )
	{
		$sub_dir = opendir($avatarfolder . '/' . $file);

		$avatar_row_count = 0;
		$avatar_col_count = 0;
		while( $sub_file = readdir($sub_dir) )
		{
			if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
			{
				$avatar_images[$file][$avatar_row_count][$avatar_col_count] = $sub_file; 
				$avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));

				$avatar_col_count++;
				if( $avatar_col_count == 5 )
				{
					$avatar_row_count++;
					$avatar_col_count = 0;
				}
			}
		}
	}
}

closedir($dir);

ksort($avatar_images);
reset($avatar_images);


if( empty($category) )
{
	list($category, ) = each($avatar_images);
}
reset($avatar_images);

	opentable($locale['AVA_001']);
	echo "<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>\n";
	echo "<tr><td class='tbl2' colspan='5'><b>Avatar Gallery 2</b></td>\n";
	echo "</tr><tr>\n";
	echo "<td class='tbl2' align='center' valign='middle' colspan='5' height='28'><span class='small'>\n";
	echo "<form name='inputform1' action='".FUSION_SELF."' method='post'>
	<input type='hidden' name='avatardirname' value='".$category."'>\n";
	echo "<span class='large'>Select Category: <select name='avatargallery'>\n";
while( list($key) = each($avatar_images) )
{
	$selected = ( $key == $category ) ? 'selected="selected"' : '';
	if( count($avatar_images[$key]) )
	{
		echo "<option value='$key'$selected>$key</option>";
	}
}
	echo "</select>    <input type='submit' class='liteoption' value='Go' name='avatargallery'>";
	echo "</span><hr></td></tr><tr>\n";

 

Maybe you guys can tell what I need to add or change here...  ???

Link to comment
https://forums.phpfreaks.com/topic/118147-solved-probably-simple-problem-for-you/
Share on other sites

Hello php_dave and thanks for your reply.

 

OK.... guess I can't re-edit my original post... here is the rest of the code that follows what is in my first post and it's the code portion that retrieves and displays the actual avatar images...

	$s_colspan = 0;
for($i = 0; $i < count($avatar_images[$category]); $i++)
{
	$avatar_row[$i] = array();

	$s_colspan = max($s_colspan, count($avatar_images[$category][$i]));

	for($j = 0; $j < count($avatar_images[$category][$i]); $j++)

	{
		$avatar_column[$i][$j] = array(
		$images = $avatarfolder . '/' . $category . '/' . $avatar_images[$category][$i][$j], 
		$name = $avatar_name[$category][$i][$j]
		);
	echo "<td class='row1' align='center'><img src='$images' alt='$name' title='$name' /><br /><input type='radio' class='row2' name='avatarselect' value='$name.gif'><hr width='85%'></td>\n";
		$avatar_option_column[$i][$j] = array(
		$avatar_images[$category][$i][$j]);
	}
		echo "</tr>\n";
		echo "<tr>\n";
}

	echo "<td class='tbl2' colspan='5' align='center' height='28'>\n
	<input type='submit' name='submitavatar' value='Select' class='mainoption' />
	   
	<input type='reset' name='cancelavatar' value='Reset' class='liteoption' />\n";
echo "</td></tr>\n";
echo "</form>\n";
echo "</table>";

closetable();

}

require_once THEMES."templates/footer.php";
?>

 

Like I said the page displays fine but I need help figuring out how to 'handle the form submits'; where in the code they would go etc. Before I posted this I tried your code snippet; inserting it in different places within the code, but it still doesn't work. All of this code you see was originally a 'function' within the phpBB2 system so basically I'm trying to build a hybrid avatar gallery script here. :)

Sorry mate - is difficult to pick out without testing the code but I think:

 

Add

$subdir = (isset($_POST['submit')) ? "/".$_POST['avatargallery'] : "";

above

$images="";
$name="";

 

This will check if the the submit button had been pressed and then assign the value of your combo box to $subdir - if the submit button has not been pressed then it assigns "";

 

if you than change

$avatarfolder= IMAGES."avatars/avatar_gallery";

to

$avatarfolder= IMAGES."avatars/avatar_gallery".$subdir;

 

Do you have a link to this so I can see it working - I also have used Fusion quite a bit and have a testbed so if you want to pass me the code then I would be able to test it properly.

 

Hope this helps anyway

 

Cheers

Dave

Hey Dave,

OK; here we go. I tried the changes you suggest and they don't work no matter how I configure them...

I am going to attach 3 zip files of all the code involved here and maybe you can see where I'm going wrong (bearing in mind that I have never tried to code php before)...

 

And also I have created an account for you on the website where I'm trying to do this... the Avatar Gallery link at the bottom left, main menu will only be visible to you.

Login credentials:

Username: phpTester

Password: testingcode

http://twilightasunder.whisperwillow.com/

 

This is really going to be a nice avatar gallery if I can get it to work correctly.

;D

Thanks in advance,

Terry

 

 

 

[attachment deleted by admin]

looking at the code its a bit of mess. I have cleaned the code up a bit:

define('AVATAR_FOLDER', IMAGES . 'avatars/avatar_gallery/');

function LoadAvatarCategories()
{
    $ignore = array('.', '..');

    if ($handle = opendir(AVATAR_FOLDER))
    {
        while (false !== ($file = readdir($handle)))
        {
            if(!in_array($file, $ignore) && !is_file(AVATAR_FOLDER . '/' . $file))
            {
                $avatarCategories[] = $file;
            }
        }

        closedir($handle);

        return $avatarCategories;
    }

    return false;
}

function loadAvatarImages($dir)
{
    if ($handle = opendir(AVATAR_FOLDER . $dir))
    {
        while (false !== ($file = readdir($handle)))
        {
            if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $file) )
            {
                $avatar_images[] = $file;
            }
        }

        closedir($handle);

        return (isset($avatar_images)) ? $avatar_images : false;
    }

    return false;
}

$avatarCategories = LoadAvatarCategories();

$category = (isset($_POST['avatargallery']) && (trim($_POST['avatargallery']) != '')) ? $_POST['avatargallery'] : $avatarCategories[0];

$avatar_images = loadAvatarImages($category);

        echo "<form name='inputform1' action='".FUSION_SELF."' method='post'>\n";
	echo "<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>\n";
	echo "  <tr>\n    <td class='tbl2' colspan='5'><b>Avatar Gallery 2</b></td>\n";
	echo "  </tr>\n  <tr>\n";
	echo "    <td class='tbl2' align='center' valign='middle' colspan='5' height='28'><span class='small'>\n";
	echo "      <span class='large'>Select Category: \n      <select name='avatargallery'>\n";

        foreach($avatarCategories as $avatarCategory)
    	{
    		$selected = ( $avatarCategory == $category ) ? 'selected="selected"' : '';

            echo "        <option value='$avatarCategory'$selected>$avatarCategory</option>\n";
    	}

	echo "      </select>    <input type='submit' class='liteoption' value='Go'>";
	echo "</span><hr>\n      </td>\n    </tr>\n    <tr>\n";

        if(is_array($avatar_images) && !empty($avatar_images))
        {
            $i = 1;
            foreach($avatar_images as $file)
            {
                $image = AVATAR_FOLDER . $category . '/' .  $file;
                $name = array_shift(explode('.', $file));

                echo "      <td class='row1' align='center'>\n".
                     "        <img src='$image' alt='$name' title='$name' /><br />\n".
                     "        <input type='radio' class='row2' name='avatarselect' value='$name.gif'>\n".
                     "        <hr width='85%'>\n".
                     "      </td>\n";

    			if( $i%5 == 0 )
    			{
    			    echo "    </tr>\n    <tr>\n";
    			}

                $i++;
            }

            echo "    </tr>\n    <tr>\n      <td class='tbl2' colspan='5' align='center' height='28'>
        <input type='submit' name='submitavatar' value='Select' class='mainoption' />  
        <input type='reset' name='cancelavatar' value='Reset' class='liteoption' />
      </td>
    </tr>";
        }
        else
        {
            echo "    <tr><td class='row1' align='center'><span style=\"color:red;font-weight:bold\>No Images</span></td></tr>\n";
        }
  echo '</form>
</table>';

After some testing it appears to work fine (changing image galleries).

Hey wild; you are totally AWESOME !!! ... works GREAT! Now it's just a matter of trying to code the function to save the selected avatar info to the db... gonna try and see what I can come up with. The original gallery code for version 6.0, the guy was using a javascript function that used 'savepfad' ... doing a little research tells me that there is a bad vulnerability using that, so I will see what I can do. Thanks a million for straightening out the code mess for me.

 

;D

Well, I'm back again. I suppose you could say, begging for help :P ... I have tried so many different ways to code a 'file save & update db' function that my head is swimming with variable names, fopens' and all sorts of stuff; but none of it works. I will post the complete script again w/my changes (cosmetic mainly); here's what the end result function should do. The user avatars are saved in 'images/avatars/avatarfilename.ext' in the format: imagename[7].gif where [7] is the 'user_id'. That same info gets UPDATED to the database. Here's the code:

 

require_once "../../maincore.php";
require_once THEMES."templates/header.php";

include INFUSIONS."avatar_gallery/infusion_db.php";

// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."avatar_gallery/locale/".$settings['locale'].".php")) {
// Load the locale file matching the current site locale setting.
include INFUSIONS."avatar_gallery/locale/".$settings['locale'].".php";
} else {
// Load the infusion's default locale file.
include INFUSIONS."avatar_gallery/locale/English.php";
}


if (!iMEMBER) { redirect("index.php"); }


define('AVATAR_FOLDER', IMAGES . 'avatars/avatar_gallery/');

function LoadAvatarCategories()
{
    $ignore = array('.', '..');

    if ($handle = opendir(AVATAR_FOLDER))
    {
        while (false !== ($file = readdir($handle)))
        {
            if(!in_array($file, $ignore) && !is_file(AVATAR_FOLDER . '/' . $file))
            {
                $avatarCategories[] = $file;
            }
        }

        closedir($handle);

        return $avatarCategories;
    }

    return false;
}

function loadAvatarImages($dir)
{
    if ($handle = opendir(AVATAR_FOLDER . $dir))
    {
        while (false !== ($file = readdir($handle)))
        {
            if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $file) )
            {
                $avatar_images[] = $file;
            }
        }

        closedir($handle);

        return (isset($avatar_images)) ? $avatar_images : false;
    }

    return false;
}

$avatarCategories = LoadAvatarCategories();

$category = (isset($_POST['avatargallery']) && (trim($_POST['avatargallery']) != '')) ? $_POST['avatargallery'] : $avatarCategories[0];

$avatar_images = loadAvatarImages($category);

opentable($locale['GAL_001']);
        echo "<form name='inputform1' action='".FUSION_SELF."' method='post'>\n";
	echo "<table width='100%' cellpadding='6' cellspacing='2' class='tbl-border'>\n";
	echo "<tr>\n";
	echo "<td class='tbl2' colspan='5'><b>".$locale['GAL_002']."</b></td>\n";
	echo "  </tr>\n  <tr>\n";
	echo "    <td class='tbl2' align='center' valign='middle' colspan='5' height='28'>\n";
	echo "      <span class='large'>".$locale['GAL_003']." \n      <select name='avatargallery'>\n";

        foreach($avatarCategories as $avatarCategory)
    	{
    		$selected = ( $avatarCategory == $category ) ? 'selected="selected"' : '';

            echo "        <option value='$avatarCategory'$selected>$avatarCategory</option>\n";
    	}

	echo "      </select>    <input type='submit' class='mainoption' value='Go'>";
	echo "</span><hr class='mid-hr'>\n      </td>\n    </tr>\n    <tr>\n";

        if(is_array($avatar_images) && !empty($avatar_images))
        {
            $i = 1;
            foreach($avatar_images as $file)
            {
                $image = AVATAR_FOLDER . $category . '/' .  $file;
                $name = array_shift(explode('.', $file));
                $ext = strrchr($image, '.');

                echo "      <td class='row1' align='center'>\n".
                     "        <img src='$image' alt='$name' title='$name$ext' /><br />\n".
                     "        <input type='radio' class='row2' name='avatarselect' value='$name$ext'>\n".
                     "      </td>\n";

    			if( $i%5 == 0 )
    			{
    			    echo "    </tr>\n    <tr>\n";
    			}

                $i++;
            }

            echo "  </tr>\n    <tr>\n<td class='tbl2' colspan='5' align='center'><hr class='mid-hr'></td></tr><tr>\n";
            echo "<td class='tbl2' colspan='5' align='center'>\n";
            echo "<table><tr><td align='center' valign='bottom' width='50%'>\n
	 <input type='submit' name='avatar' onClick='saveavatar();' value='Select' class='mainoption' /></td>\n
	 <td align='center' valign='bottom' width='50%'><a class='mine' href='../../index.php'>\n
	 <img src='".BASEDIR."images/avcancel.gif' width='76' height='28' border='0' alt='' /></a></td></tr></table>\n";
            echo "</td></tr>";
        }
        else
        {
            echo "    <tr><td class='row1' align='center'><span style=\"color:red;font-weight:bold\>No Images</span></td></tr>\n";
        }
  echo "</form>\n
</table>";

closetable();


require_once THEMES."templates/footer.php";
?>

 

The script as it stands now, can be viewed at the 'test url' I gave in an earlier post; the category(directory) and images functions work flawlessly. I now need to be able to save the selected image and update the database. Thanks in advance for any and all help.

;D

I noticed the following line, in your code above

<input type='submit' name='avatar' onClick='saveavatar();' value='Select' class='mainoption' /></td>\n

Where is the saveavatar function? You wont be able to call PHP functions based on an event that happens on the page, like you can with javascript.

 

However, to apply the selected avatar for the current user it should be relatively straight forward. When a user selects a radio and submits the form, the chosen avatar will be held in the $_POST['avatarselect'] variable (avatarselect is the name given to ur radio buttons). Heres some example code:

// check that an avatar is selected
if(isset($_POST['avatarselect']) && !empty($_POST['avatarselect']))
{
    // get the avatar category
    $avatar_category = $_POST['avatargallery'];
    // get the selected avatar
    $avatar_image    = $_POST['avatarselect'];

    // build the path to the avatar
    $avatar_path = AVATAR_FOLDER . $avatar_category . '/' . $avatar_image;

    // check that the choosen avatar exists
    if(file_exists($avatar_path))
    {
        // avatar exists save it to the database!
        // prepare the query
        $qry = "UPDATE your_user_table SET avatar_image_column = '$avatar_path' WHERE userid_column='$your_user_identify_variable";

        // run the query                // stop the script an display an error, if theres a MYSQL error
        $rslt = mysql_query($qry) or die('ERROR!<br /><pre>' . $qry . '</pre>' . mysql_error());

        // avatar updated! Dispaly a message
        echo 'Your avatar has been updated!';
    }
    else
    {
        echo 'Choosen avatar doesn\'t exist!';
    }
}

I'd place the above code after the following line

define('AVATAR_FOLDER', IMAGES . 'avatars/avatar_gallery/');

 

Note: You'll need to only change one line and that'll be the SQL query

$qry = "UPDATE your_user_table SET avatar_image_column = '$avatar_path' WHERE userid_column='$your_user_identify_variable";

As I don't know the table structure for your site I can't provide the exact query you'll need. You'll have to modify it to your needs.

 

Hi wild,

  I really appreciate all your help on this project of mine..... with your last code post I thought I might have it whipped but not quite, though I'm close. This is what I have now:

 

$error = ""; $set_avatar = "";

define('AVATAR_FOLDER', IMAGES . 'avatars/avatar_gallery/');
if(isset($_POST['user_avatar']) && !empty($_POST['user_avatar'])) {
    if (!$error) {
if (!$userdata['user_avatar'] && !empty($_POST['user_avatar']['name']) && !is_uploaded_file($_POST['user_avatar']['tmp_name'])) {
	$newavatar = $_POST['user_avatar'];
	$avatarext = strrchr($newavatar['name'],".");
	$avatarname = substr($newavatar['name'], 0, strrpos($newavatar['name'], "."));
	if (preg_check("/^[-0-9A-Z_\[\]]+$/i", $avatarname) && preg_check("/(\.gif|\.GIF|\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG)$/", $avatarext) && $newavatar['size'] <= 30720) {
		$avatarname = $avatarname."[".$userdata['user_id']."]".$avatarext;
		copy($newavatar['tmp_name'], IMAGES."avatars/".$avatarname);
		$set_avatar = ", user_avatar='".$avatarname."'";
	}
}
}

echo $newavatar;    [color=maroon]This produces the correct filename of the selected avatar...[/color]
echo $avatarext;      [color=maroon]This produces nothing...[/color]
echo $userdata['user_avatar'];    [color=maroon]And this produces nothing...[/color]

}

 

Question: why can't I put color codes in a 'code area' ?? Anywayz, say the avatar filename is Jack.gif...

this is what's supposed to happen... first the current useravatar (if any) gets deleted; (haven't added that code yet), then the 'user_id" gets inserted in the avatar fiename; Jack[1].gif ... my user id is 1. Then the avatar (with that name), gets written to images/avatars/Jack[1].gif... Then the db gets updated with that filename (file is not stored in db)....... The code above which I took from an 'include file' which handles 'profile updates', was basically aimed at avatar uploads..... I changed the $_FILE ref's to $_POST... the 'copy' was ...... move_uploaded_file....

 

I think I'm on the right track here just picking thru things by trial and error (lotz of errors at first), and I may end up with a working MOD.....  :)

 

Why do you have to rename the selected avatar file to username[X].gif (X being a number). I've not used PHP Fusion, is this some sort of standard thing? It will be much easier if you just updated the database with the selected avatar, rather having to rename/move files, which just over complicates things really.

Hey wildteen88; I agree with you 100% however; if I'm to have my MOD accepted and used I must work within the framework of the CMS design and that's the way they designed it to work. I would love to be able to just store the avatar image as 'blob' in the database; would be so much simpler. But I have to do it the way it's already designed...

 

Code sample from 'forum viewthread' that displays users avatar in their post...

 

if ($data['user_avatar'] && file_exists(IMAGES."avatars/".$data['user_avatar'])) {
		echo "<img src='".IMAGES."avatars/".$data['user_avatar']."' alt='".$locale['567']."' /><br /><br />\n";

 

The thing I'm not sure about (in my last reply), are the variables 'name' and 'tmp_name'; is that the way variables ( i.e. $_POST['user_avatar'] ) are given an alter ego ? At any rate, the script runs thru the code but doesn't act on it (doesn't copy the file, but also produces no errors) ...  ???

I would love to be able to just store the avatar image as 'blob' in the database; would be so much simpler. But I have to do it the way it's already designed...

I didn't mean save the image in the database, but the file path to the image (this is how its done already looking at your code snippet and is lso how my code works too, when a user selects an avatar two variables are passed which are $_POST['avatargallery'] and $_POST['avatarselect'].

 

$_POST['avatargallery'] holds the selected avatar category (eg Diablo2, Mario etc)

$_POST['avatarselect'] holds then name of the selected image.

 

With these two variables the path to the image is constructed like so:

$avatar_path = AVATAR_FOLDER . $avatar_category . '/' . $avatar_image;

 

You then use $avatar_path in your query to update the 'user_avatar' column. My code does not re-upload the selected image.

Hello again wild,

  I realize that this stuff is trivial to you as you are obviously an adept php programmer but, it is a GREAT learning curve for me. I'm learning so much !! I have made some progress of a sort...

 

if (!iMEMBER) { redirect("index.php"); }


define('AVATAR_FOLDER', IMAGES . 'avatars/avatar_studio/');

if(isset($_POST['avatarselect']) && !empty($_POST['avatarselect'])) {

if (!$userdata['user_avatar'] && !empty($_POST['avatarselect']) && !is_uploaded_file($_POST['avatarselect'])) {
	$newavatar = $_POST['avatarselect'];
	$avatarext = strrchr($newavatar,".");
	$avatarname = substr($newavatar, 0, strrpos($newavatar, "."));
	if (preg_check("/^[-0-9A-Z_\[\]]+$/i", $avatarname) && preg_check("/(\.gif|\.GIF|\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG)$/", $avatarext) && $newavatar['size'] <= 30720) {
		$avatarname = $avatarname."[".$userdata['user_id']."]".$avatarext;
		$saveavatar = IMAGES.'avatars/'.$avatarname;
		$tmp = fopen($saveavatar,'w+');
		fclose($tmp);
		$set_avatar = ", user_avatar='".$avatarname."'";
	}
}

}

 

You can see I've renamed my mod to 'avatar_studio'; anyway this code actually NOW writes the filename to images/avatars/filename....... BUT it's just an empty filename with '0' bytes length. OOPPsss..... darn...gotta go DR's appointment !!!! Sorry

:(

While you where away I have managed to get your PHP Fusion modification working with PHP Fusion 7 RC2. In fact AvatarGallery6 does work with RC2 (only a few changes where actually needed).

 

As the code for AvatarGallery6 is a bit messy, I replaced it with the code I have already provided you, took a bit of debugging but I got there in the end.  Attached is version7 (version6 is included too, if you wan to run version6 go to your-site.com/infusions/avatar_gallery/avatar_gallery6.php)

 

[attachment deleted by admin]

Hey dude!!

  Man I don't know what to say... you don't know how much I appreciate this! You have done for me what would've taken me weeks or longer to figure out! I think I would have eventually gotten there though it probably would've been a lot'z messier than what you have accomplished in such a short period of time.

 

  I need to ask you about credits, copyrights and so forth; I've never attempted anything like this before and I would like to submit it for consideration as an official infusion on php-fusion's dev site. I would like to include your name and whatever else I need to in the credits so that they know I didn't create this all from scratch myself. And I have changed all the appropriate variables and files so that it is now Avatar Studio version 1.00 ..... Several developers have created different versions of the 'avatar gallery' over the years and I want to call mine Avatar Studio.

 

  So give me some direction here on credits and so forth.

;D        ;)

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.