Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Display a new image for every visitor

images php coding issues or problems here.

Moderators: macek, egami, gesf

Display a new image for every visitor

Postby njbankhead » Fri Apr 13, 2012 12:06 pm

I am creating a digital art project.

For this I am aiming to get a webpage to show how many visits its had by displaying an image for every visit. So basically every time someone visits or refreshes the web page a new image will appear; 50 visits = 50 images and so on. I want the images to be taken from a folder on the server at random.

So far i have managed to get a random image on the server to be displayed every time the page is visited. Now I want the image to stay and another one to be added so it creates a collage of images. I've been looking everywhere for help and can't find anything.

I am fairly new to php but this is the code I have that displays and image from the server at random every time the page is visited/refreshed:

<?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);
}
}

?>

I also have very basic code for a visit counter:

<?php

session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1

?>

Any help would be hugely appreciated!

Thank you very much for your time :D
njbankhead
New php-forum User
New php-forum User
 
Posts: 1
Joined: Fri Apr 13, 2012 11:53 am

Re: Display a new image for every visitor

Postby JordanMRichards » Mon Apr 23, 2012 12:33 pm

You should store your page views in an sql database, that way you can retrieve them at any time.
I'm 14 and new to the forums I am a beginner in php also.

Just coded this bit up, it's untested, hope it helps ;)
Code: Select all
// images
$image1 = '50views.png';
$image2 = '100views.png';
$image3 = '150views.png';
$image4 = '200views.png';

//width and height per image
$eachwidth = 100px;
$eachheight = 100px;

$views = $_SESSION['views'];
if ($views>=50&&$views<100) {  // views for image 1
   echo "<img href='".$image1."' width='".$eachwdith."' height='".$eachheight."'></img>"
}else if ($views>=100&&$views<150) { // views for image 2
   echo "<img href='".$image2."' width='".$eachwdith."' height='".$eachheight."'></img>"
}else if ($views>=150&&$views<200) { // views for image 3
   echo "<img href='".$image3."' width='".$eachwdith."' height='".$eachheight."'></img>"
}else if ($views>=200&&$views<250) { // views for image 4
   echo "<img href='".$image4."' width='".$eachwdith."' height='".$eachheight."'></img>"
}


Hope this helps :)
User avatar
JordanMRichards
New php-forum User
New php-forum User
 
Posts: 84
Joined: Mon Apr 23, 2012 7:43 am


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron