PHP random image

Hello everyone,
Im trying to use (for the first time) random images generated
by PHP. I
installed a Technorama extension and then followed thru the
steps to place a
random image in the page, but there is nothing there when I
browse the page.
Please forgive my ignorance, but I have never done any php
stuff before.
And yes, the files are uploaded to my server. (which does
have php enabled).
The practice page I am using is
www.fionahayward.com/interiordesign/practice.php
I have the effect working with javascript on this page
www.fionahayward.com/interiordesign but am trying to use php
incase people
have javascript turned off.
Maybe its something to do with how Ive set up Server
Behaviours or something
(only guessing)
Thanks for listening
Fiona

1) create a php file named rotate.php in the folder where the
images are.
2) paste this code (between the [code ] and [/code]
markers... into that file and save it.
[code]
<?php
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
// --------------------- END CONFIGURATION
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] )
file_exists( $folder.$imageInfo['basename'] )
$img = $folder.$imageInfo['basename'];
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
$fileList[] = $file;
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[
$imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
?>
[/code]
4) in whichever file you want the random images to appear,
reference the image so:
src="path/to/that/image/folder/rotate.php"
I tend to use root-relative pathing, but that part is up to
you.
NOTE: I've found that random image references are quirky in
IE browsers - they seem to want the image width and height to
display images that are dynamically selected... I've used this
script to display images that are all the same size and groups that
aren't. When the image size is unknown, I use width:100%
height:100% in my css)
email me if you have any issues with using the script. I use
it a lot with WordPress sites, but it isn't a WP only solution.
syncbox AT gmail DOT com (do the obvious)
HTH

Similar Messages

  • PHP Random Image extension

    I am trying to use the PHP random image generator but I don't
    know where to specify the size I want all the pictures to come up
    in the code. I created a "php" test page and inserted said code
    where I want the images to display but they are too large and throw
    the entire page off. Is there a way to tell it that all pictures
    should be called up at 240(width) x 350(height)? Any help would be
    greatly appreciated. The reason I am doing this is because there
    are about 200 images (of students) in this folder and coding each
    picture seperately is too time consuming.
    Regards,
    RM (using DW MX2004)

    Disregard....I figured it out. As it turns out the best
    solution would be to have your original pictures saved with the
    desired dimensions. I was able to point the PHP to a directory that
    contained pictures with said dimensions and it worked like a charm.
    regards.

  • PHP Random Images (Mr. Powers or other PHP Experts)

    Hello everyone,
    This is the script from David Power's book using arrays and the rand function (Disclosure).
    Basically it changes the images randomly on browser load or refresh.
    However, I am trying to add an additional, not sure what to call it, a string or another caption for each images.
    Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page
    when it is clicked on for additional info about the image.
    I tried adding another caption and adding a like to it but getting syntax error.
    Thanks everyone!
    Here is the code:
    <?php
    $images = array(
    array('file' => 'image1',
    'caption' => 'Caption 1'),
    array('file' => 'image2',
    'caption' => 'Caption 2'),
    $i = rand(0, count($images)-1);
    $selectedImage = "graphics/{$images[$i]['file']}.png";
    $caption = $images[$i]['caption'];
    ?>
    And here is where the images are written to the page:
    <div id="stage">
    <img src="<?php echo $selectedImage; ?>" alt="Random image" />
    <p id="caption"><?php echo $caption; ?></p>
    </div>
    Thanks everyone!
    WE

    Thanks for the helpful link!
    I found the solution to the problem it was very simple and I feel a little stupid asking the question in the first place but, that's how learning works.
    We sometimes stumble upon knowledge.
    The problem is I was adding the second string outside of the array.
    Here is the right way:
    array('file' => 'image1',
    'caption' => 'Hello!',
    'link1' => '<a href="http://www.msnbc.com">Read more</a>',<--Instead of putting the link here
    link2' => '<a href="http://www.npr.org">Idea Center</a>'
    <-- I was putting the link here out side the array and not properly ending the string.
    $i = rand(0, count($images)-1);
    $selectedImage = "graphics/{$images[$i]['file']}.png";
    $caption = $images[$i]['caption'];
    $link1 = $images[$i]['link1'];
    $link2 = $images[$i]['link2'];
    ?>
    Output here:
    <div id="stage">
    <img src="<?php echo $selectedImage; ?>" alt="Random image" />
    <p><?php echo $caption; ?></p>
    <?php echo $link1; ?>
    <?php echo $link2; ?>
    </div>
    WE

  • Random Image PHP Scipt Issue

    I am using a
    PHP random image
    script on a site in which I have 2 different images on each
    templated page (random images throgh a site). The two images call
    to 2 different folders with the random image script in them (to get
    2 distinct images). Thus, 2 different random images. My issue is
    that it seems that most browsers at some point do not call for a
    new image but use what was there in the last page as it is the same
    name:
    http://ansano.com/asl/images/random/top/random_image.php
    or
    http://ansano.com/asl/images/random/bottom/random_image.php
    Any idea of how to force a browser to call the PHP script? Or
    how to change it so it works site wide?
    Thanks!!

    I've now been sitting looking for this for a while. I don't seem to be able to get it to work.
    Do I need to host to a folder first and then to the FTP server after?
    I don't seen to be able to find the new page (TEST PAGE) in the folder I used to publish the site to before I went online. Tried searching for it in finder but I don't get any results on (TESTPAGEfiles).
    I can see what you are talking bout I only publish to a local folder but I want to publish via the FTP option in iWeb.
    Thank you very much for helping me

  • Suggestions for simple random image script

    Would like to randomly display one of five new JPG banner each time page is loaded.
    Your experience would be appreciated.
    Thanks

    Since no one replied, I am posting solution I found.for future inquiries.
    Simple PHP Random Image Script
    This can be done in one line of text directly in your HTML and a collection of images.
    The first step is to gather together the images you wish to have randomly rotated.
    Change all of their file names to a numerical order, starting with the number 1.
        * 1.jpg
        * 2.jpg
        * 3.jpg
        * 4.jpg
    Be sure each image has the same extension (either all jpg, png, or gif),
    and that there are no gaps in the numbers.
    Also, place these images in their own folder to keep everything organized.
    Write the Code
    In the HTML of your page, insert the following code where you want an image to display:
    <img src="path/<?php echo rand(1,n);?>.jpg" alt="Random Image" />
    The “rand” operator will display a randomly generated number between 1 and ‘n’.
    Change ‘n’ to the total amount of pictures you have arranged numerically
    (or the highest numbered picture you have).
    In the example above I only have 4 pictures, therefor I would change it to: rand(1,4);

  • Using PHP to display semi-random images?

    Hello,
    I'm looking for help using PHP and MySQL to generate semi-random images on my web site. Here's what I'm trying to do: I have 60 images, but I only want to display 16 at a time (for examlpe http://artisdead.net/antelopegardens.php).
    The way I imagine this is, the design has 16 image placeholders. I want Placeholder 1 (the first image) to randomally pull record ID 1, 2, or 3 from the SQL database, while Placeholder 2 pulls record ID 4-6, etc. It's important that one of the first 3 images displays first, while one of the second 3 images displays next, etc.
    I've found various ways to randomize all the images, but I'm not understanding how to constrain the randomization the way I'm looking to. Any help would be greatly appreciated. Thanks in advance.

    Alamo_Melt wrote:
     The way I imagine this is, the design has 16 image placeholders. I want Placeholder 1 (the first image) to randomally pull record ID 1, 2, or 3 from the SQL database, while Placeholder 2 pulls record ID 4-6, etc. It's important that one of the first 3 images displays first, while one of the second 3 images displays next, etc.
    Before offering a possible solution, I should point out that your plan has a minor flaw. If you display only 16 images, and want the random choice to come from incremental sets of three, the maximum number you can have in your set is 48. The final 12 images will never be selected. For 60 images, you need 20 placeholders. The alternative is to have a total set of 64 images, and make the random choice come from incremental sets of four images.
    With that caveat, here's how I would do it. The primary keys of your records might not be consecutive, particularly if records are deleted and new ones added, so it might be a good idea to have a separate column with consecutive numbers from 1 to the the maximum. To select random numbers from incremental groups of three, use the following:
    $id = array();
    $min = 1;
    $max = 3;
    for ($i = 0; $i < 16; $i++) {
      // generate random number from current set
      $id[$i] = mt_rand($min, $max);
      // increment the minimum and maximum numbers for the next set
      $min += 3;
      $max += 3;
    // join the selected values as a comma-separated string
    $vals = implode(',', $id);
    $sql = "SELECT image FROM images WHERE id IN ($vals)";
    // excecute the SQL query

  • Random Image not displaying in Live View.

    My page is at:
    www.andrewjamesartist.co.uk
    There's a random image script on the front page.
    <img src="randomimages/rotate.php" alt="A Sample Image from the work of Andrew James" />
    That's how it is placed in the page.
    It shows in preview in browser and it's OK live but it doesn't show in LiveView.
    I use a testing server set up with virtual hosts.
    Martin

    My page is at:
    www.andrewjamesartist.co.uk
    There's a random image script on the front page.
    <img src="randomimages/rotate.php" alt="A Sample Image from the work of Andrew James" />
    That's how it is placed in the page.
    It shows in preview in browser and it's OK live but it doesn't show in LiveView.
    I use a testing server set up with virtual hosts.
    Martin

  • Randomizing image/text on refresh

    Hello. I'm new...quite new to DW and web work, but have a small, general understanding of it. I'm an animator and am in the midst of creating my own portfolio site. I don't understand code, at least uber-extreme code. I'm attempting to have specific quotes on my homepage from clients and would like to have them change, or have a new text/quote appear on refresh or upon a new visit to the site (just so it's not the same one everytime).
      Would I need to just render out a .swf file in AE or is there a means of doing this within DW w/o using crazy code? Thanks in advance, Andy

    HTML has no way to do something like this.  You would indeed have to use Flash, or JavaScript, or some server-scripting to accomplish that task.
    It's quote easy to do with something like PHP, for example, by declaring a random variable in the page, e.g.,
    <?php
    $imageVar = rand(1,5) /* this will generate a random number between 1 and 5 */
    ?>
    and then using that random variable in the image's source attribute -
    <img src="images/random/image<?php echo $imageVar; ?>.jpg" />
    If you are getting the idea that you will have to come to grips with code sooner rather than later, then you are on the right track.  DW is very punishing to those who won't take the time to learn the underlying technologies....
    If you Google "random image" and "Dreamweaver" you may also be able to find extensions that do the same thing for you (in JavaScript).  I would recommend that you visit http://www.projectseven.com and search there - I do know that they have such a thing, and can wholeheartedly endorse their products as being the best of show....

  • Need Random Image Script with Links

    I am needing a script for random images on my homepage. I
    need the image to have its own link depending on what image is
    displayed. I would like a php script (non database) if
    possible

    See comments on DW forum about random includes and this
    particular markup
    shown below.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "newhorizonhosting.com" <[email protected]>
    wrote in message
    news:e29on1$buf$[email protected]..
    >I figured it out by doing a random include
    >
    > <?
    > srand(time());
    > $random = (rand()%3);
    > print("$random");
    > require_once("./$random.php");
    > ?>

  • Need Random Image Script with Link

    I am needing a script for random images on my homepage. I
    need the image to have its own link depending on what image is
    displayed. I would like a php script (non database) if
    possible

    Not sure about the PHP but there are some good scrips on
    www.kaosweaver.com
    Paul Whitham
    Certified Dreamweaver MX2004 Professional
    Adobe Community Expert - Dreamweaver
    Valleybiz Internet Design
    www.valleybiz.net
    "newhorizonhosting.com" <[email protected]>
    wrote in message
    news:e28rpm$8is$[email protected]..
    >I am needing a script for random images on my homepage. I
    need the image to
    >have its own link depending on what image is displayed. I
    would like a php
    >script (non database) if possible

  • Using a random image generator in a frameset [was: "I'm a photographer, not a web designer!"]

    I've been designing my own website for some years now using dreamweaver.  I love the ease of use and simplicity it offers, however I know nothing of designing web pages.  I know what I want but I just don't exactly always know how to get there.  I am currently trying to have a random image generator on my landing page, which is a frameset.  I've tried several codes that I've found online and have had no success, at all mind you, with any of them.  I'm sure that I am doing something very simple wrong with my design but just can't figure it out.  Unfortunately I am a photographer which makes me a visual person, so yes, if you can help me you'll have to draw a picture for me, or at least hold my hand and walk me through the steps.  Sorry, I know I'm high maintenance.
    My site is all frameset based.  I have a 'mainFrame' that all other links in the other frames load to.  So I am assuming that my code for the image generator should go in the body of the mainFrame.  I have even copied and pasted all the code from some of the help I've found, downloaded a cfm to try, all to no avail.  Con someone spell it out for me, or better yet, draw me a picture.
    [Subject line edited by moderator for clarity]

    My site is all frameset based.
    That's a tragedy.  Frames, once popular in the 90's are almost never used anymore.  In fact, the W3C saw fit to drop frameset support from HTML 5.
    Why Frames are Evil:  http://apptools.com/rants/framesevil.php
    Instead of Frames or Framesets, consider using Templates (DWTs) or Server-Side Includes (SSIs)
    Guidance  on when to use DW Templates, Library Items and SSIs -
    http://www.adobe.com/devnet/dreamweaver/articles/ssi_lbi_template.html
    Below are several DHTML image rotation/slideshow scripts.  Just pick one and follow the instructions.
    http://www.dynamicdrive.com/dynamicindex14/index.html
    Good luck,
    Nancy O.
    Alt-Web Design & Publishing
    Web | Graphics | Print | Media Specialists
    www.alt-web.com/
    www.twitter.com/altweb
    www.alt-web.blogspot.com

  • Alt tags, kaosweaver advanced random images

    Hi. In Dreamweaver 8.0
    I have installed Advanced Random Images from Kaosweaver: http://www.kaosweaver.com/extensions/details.php?id=5
    I would like to do a Sequential Slide Show with 8 images. I would like to add alt tags onto each of these images, but the Kaosweaver will not let me do this with the Sequential Slide Show.
    Does anyone know a way/trick to work around this?
    Thank you.

    You would have to direct these questions to Paul Davis, the author of kaosweaver stuff.
    However, it sure looks to me as if it does this (snippet taken from the ARI demo on the kaosweaver site) -
    // Advanced Random Images End
        </script><img src="/assets/images/extdemo/intensa3/neptune_main.gif"
    alt="The solid blue is wonderful" title="The solid blue is wonderful" width="300" height="300">
        <!-- KW ARI Image -->
    </p>

  • If I try and follow a link from another website to open another I get awclick.php (GIF Image 1x1) pixels in a new tab instead of the web page I want.

    I was trying to open a website as a link from another website and I get a new tab that says
    awclick.php(GIF Image 1x1 pixels)
    I can open the website without clicking on a link to it by just typing in the address but as this is a cash back site I am linking from it doesn't help!

    You're welcome

  • Random images display strong yellow/green cast

    system - 24" intel iMac, 3ghz, 2gb, 500bg hd, NVIDIA GeForce 8800 GS with 512MB memory, OS 10.5.4. Just purchased.
    System runs fine, and I love the machine. Problem is random images, jpegs, raw, illustrator files, display an awful yellowish tint, both in the preview and when opened in PS or preview. Just random, sometimes a whole folder of images, sometimes just a few. I, of course tested some of the same images on another mac, and they viewed correctly. If I save a copy to the desktop, the icon looks correct, but when opened it looks like puke. Also happens in front row when displaying certain album covers and video trailer art.
    Anyone else run into this?
    -h

    Hello Double H Media
    Welcome to Apple Discussions!
    Unfortunately the OP and others appear to have the same issue possibly related to the GeForce 8800 GS, see the following thread!
    http://discussions.apple.com/thread.jspa?messageID=7224138&#7224138
    Dennis

  • When opening a folder with "items as icons" view, a millisecond fast random image appears in place

    Hi,
    since I updated to Yosemite (but cannot assure it's related or started exactly that moment) I've been experienced a weird issue.
    When I open a folder to navigate it, and it's set on "show items as icons", I see all the icons, for just less than a second, replaced by random images.
    It's like if while the proper folder/file icon it's loading, that space is temporarily occupied by the impression of another one (or parts of it), coming from my computer. Sometimes is a photo, sometimes another folder icon not related, sometime is the icon of a software.
    The picture then goes away (it stays less than a second) and the icon for the file/folder is then the appropriate one.
    I have a Macbook Pro 15-inc, mid 2010.
    Is it a known issue? If yes, how can I solve, if possible, this?
    Hope can be helpful to others too.
    Many Thanks

    First off, I do not recommend looking at your time machine while it is running.  You can tell if it is running by making sure its menubar icon is shown in Apple menu -> System Preferences -> Time Machine.  If the icon is moving, it is running.   You can manually backup, which I recommend over automatic backup, as automatic backup can get in your way.   Spotlight, if it is indexing (check the menu in the upper right that looks like a magnifying glass), will also slow matters down, and adding things you don't want Spotlight to search to your Apple menu -> System Preferences -> Spotlight -> Privacy pane will help.  Note this affects areas you can search in Time Machine as well.  If searching content of files is of little interest to you, I find Find Any File much quicker for finding by file name.   Backup when you aren't doing anything else, as otherwise, you may find yourself backing up files that haven't had the opportunity to close.
      Time Machine also works best if your destination drive is at least twice as capacious as your source drive.   Otherwise it is spending a lot of time trying to find places for the archived files, and barely is more archival than a simple clone.

Maybe you are looking for