Solved

use some images more often than others

Posted on 2011-09-18
6
266 Views
Last Modified: 2012-08-14
//a set of images that are used ($a1/($a1+$a2))% of the time
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));

//a set of images that are used ($a2/($a1+$a2))% of the time
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '8.gif'));


echo '<pre>' . print_r(array($a1Images, $a2Images), $NumberOfTotalImages), true) . '</pre>';
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif', '8.gif');

// SHOW HOW TO USE THE FUNCTION
print_r(image_array($images));
print_r(image_array($images, 4));
print_r(image_array($images, 713));

// DEFINE THE FUNCTION
function image_array($images, $num=1)
{
    // UNIQUE NAMES ONLY
    $images = array_unique($images);

    // GET A RANDOM COLLECTION
    shuffle($images);
    return array_slice($images, 0, $num);
}

Open in new window

0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:m4trix
ID: 36556592
You didn't actually ask a question here, but I assume you're looking to modify the code you have to weight images towards one of the two image arrays based on your weightings. The code below will do that. Note that since you didn't specify otherwise, images will be re-used if the list of images to use is shorter than required. In the future, please be clear about what you are actually asking for.

Here is how it works (based on wanting an array of 10 images with 80% from a1 [implying 8 images from a1, and 2 images from a2]):
1) if a1 has 10 images and a2 has 10 images, then 8 will be randomly selected from a1, and 2 will be randomly selected from a2, and the resulting array will be randomized and returned
2) if a1 has 5 images and a2 has 10 images, then all 5 images from a1 will be selected, then 3 more images randomly selected from a1, and 2 images will be randomly selected from a2. The resultant array will have 3 duplicates from a1

<?php
error_reporting(E_ALL);

// SET PERCENTAGES (80% a1 in this case)             
$a1 = 80;
$a2 = 20;

//a set of images that are used ($a1/($a1+$a2))% of the time
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used ($a2/($a1+$a2))% of the time
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '8.gif')); 

echo '<pre>';
// SHOW HOW TO USE THE FUNCTION
print_r(weighted_image_array($a1Images, $a2Images, 10));
echo "</pre>"; 
   
// DEFINE THE FUNCTION
function weighted_image_array($a1Images, $a2Images, $num=1)
{
    // UNIQUE NAMES ONLY
    $a1images = array_unique($a1Images['images']);
    $a2images = array_unique($a2Images['images']);
    
    // # OF IMAGES FROM EACH COLLECTION
    $a1Cnt = round($a1Images['weight']/($a1Images['weight']+$a2Images['weight']) * $num);
    $a2Cnt = $num - $a1Cnt;

    // GET A RANDOM COLLECTION
    $returnArr = array();
    
    // ADD a1 IMAGES (RANDOMLY) UNTIL THE REQUIRED AMOUNT ($a1Cnt) IS MET. IF THE # OF IMAGES IN a1 < $a1Cnt, THEN IT WILL DUPLICATE         
    $a1Total = 0;
    while ($a1Total < $a1Cnt) {
        shuffle($a1images);
        $returnArr = array_merge($returnArr, array_slice($a1images, 0, min($a1Cnt - $a1Total, count($a1images))));
        $a1Total += min($a1Cnt, count($a1images));
    }
    
    // ADD a2 IMAGES (RANDOMLY) UNTIL THE REQUIRED AMOUNT ($a2Cnt) IS MET. IF THE # OF IMAGES IN a2 < $a2Cnt, THEN IT WILL DUPLICATE     
    $a2Total = 0;
    while ($a2Total < $a2Cnt) {
        shuffle($a2images);
        $returnArr = array_merge($returnArr, array_slice($a2images, 0, min($a2Cnt - $a2Total, count($a2images))));
        $a2Total += min($a2Cnt, count($a2images));
    }
    
    // SHUFFLE THE RESULTANT ARRAY AND RETURN
    shuffle($returnArr);
    return $returnArr;
}
?>

Open in new window

0
 
LVL 3

Expert Comment

by:Duboux
ID: 36556664
I don't see your question anyway, so I'm assuming u have an array of image names of which you want some to show up more than others, during a random show ?
Maybe even some twice more than others XD

okay, multi-array:


<?php

$a1 = 1;
$a2 = 2;
$a3 = 3;

// Create readable image array ;)
$arrayImages = array();
$arrayImages[] = array(
                        "weight" => $a1,
                        "images" => array("1.gif", "2.gif", "3.jpg", "4.jpg", "5.gif", "6.jpg")
);
$arrayImages[] = array(
                        "weight" => $a2,
                        "images" => array("7.gif", "8.gif", "9.jpg")
);
$arrayImages[] = array(
                        "weight" => $a3,
                        "images" => array("10.gif", "11.gif", "12.jpg")
);


// Fill the jar...
$ImageJar = array();      /// create empty jar...
foreach ($arrayImages as $I) {      /// go through all the $arrayImages[] lines
    $amount = $I["weight"];
    if (!is_integer($amount)) { exit("Weight must be an integer..."); }
    for ($i = 1; $i <= $amount; $i++) {      /// Add this line of images to the Jar.. And keep repeating till it's done $amount times...
        $ImageJar = array_merge($ImageJar, $I["images"]);
    }
}

// Pic a random image...
$picImg = rand(0,count($ImageJar) - 1);
echo "The randomly choosen pic = ".$ImageJar[$picImg];
echo "<br />Here's the Jars contents:<br />". nl2br(print_r($ImageJar, true));

?>

Open in new window


Is that what u mean ?
0
 

Author Comment

by:rgb192
ID: 36558021
m4trix
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '9.gif'));

the output images repeat.  I do not want repeats.
If there are more images called than available, then just display all


duboux
your code only finds one random image
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Accepted Solution

by:
m4trix earned 500 total points
ID: 36558082
As I said before, you never specified what you were looking for. Nontheless, here is a version that does not repeat images:

<?php
error_reporting(E_ALL);

// SET PERCENTAGES (80% a1 in this case)             
$a1 = 80;
$a2 = 20;

//a set of images that are used ($a1/($a1+$a2))% of the time
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used ($a2/($a1+$a2))% of the time
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '8.gif')); 

echo '<pre>';
// SHOW HOW TO USE THE FUNCTION
print_r(weighted_image_array($a1Images, $a2Images, 10));
echo "</pre>"; 
   
// DEFINE THE FUNCTION
function weighted_image_array($a1Images, $a2Images, $num=1)
{
    // UNIQUE NAMES ONLY
    $a1images = array_unique($a1Images['images']);
    $a2images = array_unique($a2Images['images']);
    
    // # OF IMAGES FROM EACH COLLECTION
    $a1Cnt = round($a1Images['weight']/($a1Images['weight']+$a2Images['weight']) * $num);
    $a2Cnt = $num - $a1Cnt;

    // GET A RANDOM COLLECTION
    $returnArr = array();
    
    // ADD UP TO $a1Cnt IMAGES FROM a1 TO THE RETURN ARRAY      
    shuffle($a1images);
    $returnArr = array_merge($returnArr, array_slice($a1images, 0, $a1Cnt));
    
    // ADD UP TO $a2Cnt IMAGES FROM a2 TO THE RETURN ARRAY      
    shuffle($a2images);
    $returnArr = array_merge($returnArr, array_slice($a2images, 0, $a2Cnt));
    
    // SHUFFLE THE RESULTANT ARRAY AND RETURN
    shuffle($returnArr);
    return $returnArr;
}
?>

Open in new window

0
 
LVL 3

Expert Comment

by:Duboux
ID: 36559118
Before I put any more time into this, I will wait for a detailed description of what you really need.
0
 

Author Closing Comment

by:rgb192
ID: 36565095
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now