Screen Shot for Thumnbails


Conn Warwicker



Portfolio

Scripts and Websites I've made using PHP and MySQL mainly.

Snippets

Code snippets and mini tutorials

Contact

Contact me if you have any questions or queries



Home / Snippets / dynamic_avatar

This section is new and undergoing snippet addition, as of 13/6/11

Dynamic Avatar



Or in other words, a random image.

Quickest way probably to do this is to create a directory and place a series of images in it, naming them with sequential numbers, eg: "1.jpg", "2.jpg", "3.jpg", etc...

Next create a blank PHP file (in the same directory), call it something like "image.php" and put this in it:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");   
header("Cache-Control: no-store, no-cache, must-revalidate");   
header("Cache-Control: post-check=0, pre-check=0", false);  
header("Pragma: no-cache");  
header('Content-type: image/jpeg'); 

$rand = rand(1,24); // Where the rand is between 1 and however images you have in the directory

$im = imagecreatefromjpeg($rand.'.jpg'); 

imagejpeg($im); 

imagedestroy($im); 

?>


Then you can use it as a normal image, e.g.

<img src="image.php" alt="Image" />


You could also use it on forums and other websites as an avatar, however it is unlikely that they will let you link to a PHP file (unless their security is poor), so to get around that we need to go into the .htaccess file (which should be in your public directory) and add this in:

RewriteEngine On
RewriteBase /

RewriteRule image.jpg image.php


Now if you goto "image.jpg" in the web browser, it will load up the image from "image.php".

See below for example ("avatar.jpg") (Click to refresh)

Image

Leave a Comment


Comment

All fields are required

Your Name


Your Email Address


Your Comment


Human Validation

What animal is this above:






Comments


No Comments Have Been Made