Liam
Liam

Reputation: 93

Change default image to randomly selected image from directory?

I'm scratching my head over this, on the site I'm working on if the user doesn't upload an avatar it will randomly use a default image. I would like it to randomly select an image from a directory (say 1-10 images).

Here is the code it uses to get the default avatar and default avatar thumb:

if ( !defined( 'BP_AVATAR_DEFAULT' ) )
    define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'bp-core/images/mystery-man.jpg' );

if ( !defined( 'BP_AVATAR_DEFAULT_THUMB' ) )
    define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . 'bp-core/images/mystery-man-50.jpg' );

I think the biggest problem I have is that I need the random avatar image that's selected to be the same as the random thumb that's selected.

Any insight would be great :)

Thanks

Upvotes: 0

Views: 119

Answers (1)

Abdul Jabbar
Abdul Jabbar

Reputation: 5971

I don't know how many avaters are there but suppose four then

$pics = array ('abc.jpg','def.jpg','ghi.jpg','xyz.jpg');
$picid = $pics[array_rand($pics,1)];
define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'bp-core/images/'. $picid);
define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . 'bp-core/images/'. $picid );

if more then 4 then you should rename them and keep file name as 1.jpg, 2.jpg and code can be then

$picid = rand(0, 100); //100 = No. of images
define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'bp-core/images/'.$picid.'.jpg' );
define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . 'bp-core/images/'. $picid.'.jpg' );

Upvotes: 1

Related Questions