473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Multiple image randomizer?

Hi, I am quite new with PHP so please bear with me.

I would like to update old code I have that I use for signatures. Currently it functions like so:

- Pulls one image from a folder at random, the file names are not directly associated with the code so it is possible to put new images without changing the code.
- Is able to display some text that used to be used, but was later disabled.

Problems:
- Current use requires multiple images.

Here is what the old code is like.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //
  3. // Load all background images from specified path...
  4. //
  5. $images_ary = array();
  6. $handle = @opendir('./images');
  7. while( $file = @readdir($handle) )
  8. {
  9.     if( $file <> '.' && $file <> '..' )
  10.     {
  11.         $tmpary = explode('.', $file);
  12.         $extension = strtolower($tmpary[count($tmpary)-1]);
  13.         if( in_array($extension, array('gif', 'jpg', 'jpeg', 'png')) )
  14.         {
  15.             $images_ary[] = $file;
  16.         }
  17.     }
  18. }
  19. @closedir($handle);
  20.  
  21. if( count($images_ary) <= 0 )
  22. {
  23.     die("Sorry, no images found!");
  24. }
  25.  
  26. //
  27. // Randomly select an image...
  28. //
  29. $random = time() % count($images_ary);
  30. $image_info = array(
  31.     'image'    => $images_ary[$random]
  32. );
  33.  
  34.  
  35. $image_text_ary = array();
  36. $image_text_ary[] = array(
  37.     array(
  38.         'x'     => 8,
  39.         'y'     => 6,
  40.         'color' => array(50, 100, 180),
  41.         'font'  => 9,
  42.         'text'  => ""
  43.     )
  44. );
  45.  
  46.  
  47. $random = time() % count($image_text_ary);
  48. $image_text = $image_text_ary[$random];
  49.  
  50. include('./includes/dynamic_gd_image.php');
  51. ?> 
What I would like it to do is display multiple images in it (like 5 images), I was thinking I probably have to rewrite the whole thing and draw a canvas and then make the program pull the images and I set the x/y coordinate so it can place it correctly.

If anyone has any suggestions to go about this please reply back. Just to be complete, I would like to retain the ability to randomly pick a image from a folder, the canvas should be 490 pixels x 130 pixels.

Thanks for your time!
Sep 10 '10 #1
3 1641
Thew
69
If you only want to randomize some pictures, i here have a javascript for you:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.  
  3. images = new Array(4);
  4.  
  5. images[0] = "<img src='1.jpg'>";
  6.  
  7. images[1] = "<img src='2.jpg'>";
  8.  
  9. images[2] = "<img src='3.jpg'>";
  10.  
  11. images[3] = "<img src='4.jpg'>";
  12.  
  13. index = Math.floor(Math.random() * images.length);
  14.  
  15. document.write(images[index]);
  16.  
  17. </script>
  18.  
Good luck with it ;)
Sep 10 '10 #2
My code already randomizes pictures listed in a folder. What I want is to have multiple images either from that folder or another folder stitched together into one image and displayed together.

Also I don't think JavaScript will work since it has to display an image in the format .jpg.
Sep 10 '10 #3
Canabeez
126 100+
Let me get the straight, you want to have multiple images in the directory, but show like 5 images on the client side? You need to use some images library like ImageMagic or GD, but that might overload your host if you'll have some traffic. I'd suggest you use the client side to draw some images, something like:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $images = array();
  3.  
  4.     $texts = array(
  5.         "Text 1",
  6.         "Text 2",
  7.         "Text 3",
  8.         "Text 4",
  9.         "Text 5",
  10.     );
  11.  
  12.     $handle = @opendir('./images');
  13.     while( $file = @readdir($handle) ){
  14.         if( $file != '.' && $file != '..' ){
  15.             $tmpary = explode('.', $file);
  16.             $extension = strtolower($tmpary[count($tmpary)-1]);
  17.             if( in_array($extension, array('gif', 'jpg', 'jpeg', 'png')) ){
  18.                 $images[] = array(
  19.                     "src" => $file,
  20.                     "title" => $texts[ mt_rand(0,4) ]
  21.                 );
  22.             }
  23.         }
  24.     }
  25.  
  26.     shuffle($images);
  27.  
  28.     echo "<script type=\"text/javascript\">var imagesData = " . json_encode(array_slise($images, 0, 4)) . ";</script>";
  29. ?>
  30. <div id="signaturesContainer"></div>
  31. <style type="text/css">
  32.     div.signatureOverlay{
  33.         position: absolute;
  34.         left: 10px;
  35.         top: 10px;
  36.  
  37.         font-family: Arial;
  38.         font-size: 10pt;
  39.         color: #000000;
  40.     }
  41. </style>
  42. <script type="text/javascript">
  43.     for(var i in imagesData){
  44.         var image = imagesData[i];
  45.  
  46.         var imgDiv = document.createElement('div');
  47.         div.style.position = 'relative';
  48.  
  49.         var img = document.createElement('image');
  50.         img.src = image.src;
  51.         img.title = image.title;
  52.  
  53.         var txt = document.createElement('div');
  54.         txt.className = 'signatureOverlay';
  55.         txt.innerHTML = image.title;
  56.  
  57.         imgDiv.appendChild(img);
  58.         imgDiv.appendChild(txt);
  59.         document.getElementById('signaturesContainer').appendChild(imgDiv);
  60.     }
  61. </script>
  62.  
This should give you the main idea of what I meen. Didn't test the code thought, so tell me if you're having any problems with it.
Sep 10 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Ryan | last post by:
I currently have two image submit buttons. The code generated looks like so: <input type="image" src="button_in_cart.gif" border="0" alt="Add" title="Add" name="btn_cart" value="buy"> <input...
1
by: Stijn Goris | last post by:
hi all, A user can upload maximum 10 image files at once. When the user only uploads 6 images, 4 fileboxes are left blank. I use function checkImage() //check the the extension. Must be a jpeg...
4
by: matt | last post by:
I have an image randomizer that was working fine. I added five images to it today, and none of the new five are ever called. Here's the script: <!-- Begin var theImages = new Array() ...
1
by: Sean | last post by:
i am writing a class that visual demonstrates the result of changing the quality of a jpeg. the intention is to do this by saving the file and a selected quality, then reloading it to get an idea of...
1
by: rajbala.3399 | last post by:
Hai all, I need to get multiple image buttonsand wheni click on the image button it should display corresponding image on window and if i click another image button it is also display on same...
1
by: FuzzyLogik | last post by:
I need the image to be in the CSS, because I plan to have multple stylesheets. The tricky part is I have 3 parts to the image, a top, the main bit, and right, Here is the image:...
4
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all I have the following code: I am trying to use the value of the image button to evaluate what plan the user has chosen, i.e. it request("submit1")="Basic", then the user chose basic plan...
3
by: Andreas Wöckl | last post by:
HI Group! I have to programmatically create a user input form with various Checkbox and RadioButton lists - Beside every List I have to place an image button that is able to reset the...
16
ribbo
by: ribbo | last post by:
What I need in a nutshell is a form that will allow me to upload multiple pictures (say 5) to my server while at the same time saving the names to the MySQL database. I would prefer it if the image...
15
by: gaurav13477 | last post by:
i fetch single image . how i fetch multiple image. please send the code. img1.php <?php $con=mysql_connect('localhost','root',''); $d=mysql_select_db("test"); $q1="select * from cat ";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.