Home > Programming, Servers & Scripts >

Handy Little Script To Randomize Photos On a LP (11)


08-20-2011 01:11 PM #1 polarbacon (Moderator)
Handy Little Script To Randomize Photos On a LP

Well The title says it all.....I set up a lp the other day in which I needed to randomly display an image and I came across this little script and its sooooooo damn easy to use I just had to share it.....

Step 1: Cut and pastes the code into a file called "random.php"
Step 2: Put that file in a folder on your server.
Step 3: Add images to that folder that you want to display.
Step 4: When you want the image to be displayed on a LP just use and image tag like you normally would...

<img src="http://yoursite.com/the_folder/random.php" />

It really doesnt get much easier than that.....all you need to do to add/subtract images is add/remove them from the folder....

here is the code....

[PHP]

<?php
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$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);
}
}
?>

[/PHP]


go make money


08-20-2011 01:25 PM #2 mattias (Member)

Nice share. Just added to my code swipe file.


08-20-2011 02:26 PM #3 bsb85my (Member)

Is this really working? Because I can't get it works.


08-20-2011 03:53 PM #4 polarbacon (Moderator)

Quote Originally Posted by bsb85my View Post
Is this really working? Because I can't get it works.
the only thing you may need to do is when you reference the file in the image tag

it may need to follow this

[PHP]<img src="http://yoursite.com/image_folder/random.php" />[/PHP]

other than that

I can tell you that I have deployed this on 2 dif websites.....

posting "it doesn't work" isn't to helpful in helping you....post your site code....and I will def try to help


08-20-2011 04:48 PM #5 vidivo (Member)

This is a good script to have but whats the use for it if you cant properly track it using that code? I mean it would be kinda cool to have it rotate on a website, but wouldnt you want to know which image gets the highest ctr or conversion?

To do that you can easily track using Besmirs p202 code / tutorial..


08-20-2011 06:01 PM #6 polarbacon (Moderator)

Quote Originally Posted by vidivo View Post
This is a good script to have but whats the use for it if you cant properly track it using that code? I mean it would be kinda cool to have it rotate on a website, but wouldnt you want to know which image gets the highest ctr or conversion?

To do that you can easily track using Besmirs p202 code / tutorial..
true....but

sometimes you don't need to track and its more about the user experience.....


08-21-2011 05:29 AM #7 zeno (Administrator)

Probably easier to split test the entire webpage with image/link changed to see which one converts better, if your goal is tracking.


08-22-2011 12:53 AM #8 Mr Green (Administrator)

I smell dating landers!


08-22-2011 04:25 AM #9 zeno (Administrator)

Quote Originally Posted by Mr Green View Post
I smell dating landers!
Fetish? Do you sniff the screen or go for the rough, smokey wood notes of the print versions?


08-22-2011 06:21 AM #10 ppvnewbie (Member)

Here is another simple script I used for some client work Not the same as our famous unicorn uses but another sweet script to keep in your arsenal. It displays images from a pre-defined folder and shows them with an imagefade between.

Step 1. Save this code as imagefade.php and upload it to your webserver:

[PHP]
<?php
// Global Variables
$image_dir = "$_SERVER[DOCUMENT_ROOT]/images"; // directory on server
$image_relative_path = '/images'; // path to images relative to script
$file_types = array('jpg','jpeg','gif','png');
$image_time = '5000'; // seconds each image will display (4000 = 4 seconds)

if($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$ext_bits = explode(".",$file); // finds file extensions
foreach($ext_bits as $key => $value){
if(in_array($value,$file_types)){
$image_rotation .= '<li><img src="'.$image_relative_path.'/'.$file.'" /></li>';
}
}
}
}
closedir($handle);
}
?>
<!-- Style Your Image -->
<div style="display:block;width:540px;height:450px;">
<ul id="image_rotate" style="list-style-type: none; list-style-image: none; list-style-position: outside;padding:0;margin:0;">

<?php
echo $image_rotation;
?>

</ul>
</div>
[/PHP]Step 2. Ad this to your page between the <head></head> tags (change yoursite to your actual landing page url):

HTML Code:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://www.yoursite.com/js/jquery.innerfade.js"></script>

<script>
$(document).ready(function() { 
    $('#image_rotate').innerfade({ 
        speed: 'slow', 
        timeout: '6000', 
        type: 'sequence', 
        containerheight: '362px'
    });
});
</script>
Step 3. Download this file and save it to your defined folder from step 2 (in my example it would be saved in a folder called "js")

Download: jquery.innerfade.js.zip (remove the zip extensions afterwards, was only for upload purposes)

Step 4. Whereever you want to include the images use this code:

[PHP]
<?php
include('http://www.yoursite.com/imagefade.php');
?>
[/PHP]


08-22-2011 01:53 PM #11 polarbacon (Moderator)

very nice.....


Home > Programming, Servers & Scripts >