Solved

use some images more often than others

Posted on 2011-09-18
6
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

920 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

11 Experts available now in Live!

Get 1:1 Help Now