|
Recently I wrote a script for my avatar (refresh it a couple times) which allows you to randomly grab an image out of a directory and display it. Although the tutorial is fairly easy, you need to have an understanding of PHP. In order to fully appreciate this tutorial, you will need access to a web server with support for PHP as well as the GD library. This is the PHP graphics library. I am also assuming that your web server runs Apache. To figure out if you have the GD extension installed on your web server, create a php file with the following: <?php phpinfo(8); The 8 will display all of your installed modules. Just do a find for 'gd'. Note: I left off the closing ?> because we aren't outputting any HTML. Ok, assuming that you have the GD extension installed, let's get started. The first thing you need to do is setup your folder structure. Mine looks like this: -avatar_tutorial --images Pretty simple. Now drop a couple images into the images folder. In your top folder (avatar_tutorial in my case), create a new php file. You can name it whatever you want. Mine is called avatar.php. <?php $dir = 'images/'; // obviously the directory where your images are coming from
// we want to be able to detect the extension of the file function file_ext($f) { $ext = substr($f, strrpos($f, '.') + 1); // substr returns part of a string and strrpos is used to find the last occurence of a character in a string if(strlen($ext) > 2) { // because directories contain a "parent directory" link, this is an easy way to get around it return $ext; } else { return null; } }
function createimg($f) { global $dir; // this grabs the $dir var from above so we can use it
$ext = file_ext($f);
if($ext) { header("Content-type: image/$ext"); // this tells the browser that we're loading an image }
if($ext == 'png') { // if image is a png $img = imagecreatefrompng($dir.$f); imagealphablending($img, false); // disable so we can enable alpha saving imagesavealpha($img, true); // so that alpha is saved imagepng($img); } else if($ext == 'jpg') { // if image is a jpeg $img = imagecreatefromjpeg($dir.$f); imagejpeg($img, null, 100); // note the null, 100 is so tha the jpeg is saved with good quality } else if($ext == 'gif') { // if image is a gif $img = imagecreatefromgif($dir.$f); imagegif($img); }
if($ext) { imagedestroy($img); // free up the memory used to create the image } }
$dir_contents = scandir($dir); // scandir is used to make an array of the contents of a directory
function randimg() { global $dir_contents; // grabs the $dir_contents var from above so we can use it // the reason it isn't IN the function is so that the scan only has to take place once
$rand_img = $dir_contents[rand(0, count($dir_contents) - 1)]; // grabs a random image from the dir contents
if(file_ext($rand_img)) { // refer to the if statement of the file_ext. this assumes it's > 2 createimg($rand_img); } else { randimg(); // otherwise it isn't a valid extension / image and it will re-initiate the function } }
randimg(); // initiate the function Hopefully the comments in the code explain it enough. Now we come to a problem. On some websites (like proboards), you can't by default display a .php file. There is an easy way to solve this, assuming you're using Apache (just assume you are - you'd know if you weren't). In your main directory, create a file called ".htaccess". These files provide rules for how files should be handled. Inside the file, put in this. RewriteEngine On
RewriteRule avatar.jpg avatar.php [NC] change "avatar.php" to the name of your script. Now, when you load avatar.jpg, it will actually load the php script. The [NC] just means that it's caps-insensitive. And that's that!
|
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.
|
|
|
|
very good tutorial ben.
alot of beginners should be able to grasp the basics of GD with this script, again a futher add to this you need to make sure that mod_rewrite is compiled with apache before the htaccess file will run the rewrite. It generally is 99.9% of the time but if your unsure you're best asking your hosting provider as they may be able to compile it for you.
|
|
|
|
|
Thanks.
Ah, right.. I should have remembered to add that disclaimer, being as I had to compile apache myself to include the module >.< (stupid mac install didn't even have the GD extension, i had to compile that myself too)
But you're right - most of the time, the host will compile the module in order to keep you as a customer.
|
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.
|
|
|
|
But you're right - most of the time, the host will compile the module in order to keep you as a customer. you will be suprised the amount of hosts that for some strange reason don't have mod_rewrite compiled with apache. usually if you find a host thats just started up is when you get this thou lol.
|
|
|
|
|
Awesome tut. Just tried this on my domain, didn't work at first. But got some help from elsewhere and found my obvious mistake. Thanks
|
|
|
|
You sir are officially my hero. I was able to get this up and running with NO previous php knowledge, whatsoever. Thanks
|
|
|
|
Sorry for the double post, but will this work if I change the path to the images folder to an external directory? Like photobucket?
Thanks.
|
|
|
|
Sorry for the double post, but will this work if I change the path to the images folder to an external directory? Like photobucket? Thanks. It looks like it -should-, but I'm not entirely sure.
|
|
|
|
|
Warning: scandir(http://s92.photobucket.com/albums/l14/Perishingflames/website%20stuff/) [function.scandir]: failed to open dir: not implemented in /home/pfmbg/public_html/Assorted/Avatar/avatarcopy.php on line 42 Warning: scandir() [function.scandir]: (errno 25): Inappropriate ioctl for device in /home/pfmbg/public_html/Assorted/Avatar/avatarcopy.php on line 42
|
|
|
|
Oh, crap, I just realized this one works by scanning the dir instead of requiring manual listing of files. MY bad. It won't work then unfortunately.
|
|
|
|
|
Whoops, was supposed to be i92, but didn't work anyways. Well that's a bummer, using up my precious bandwidth
|
|
|
|
Whoops, was supposed to be i92, but didn't work anyways. Well that's a bummer, using up my precious bandwidth Well, either way, it'd need to use your bandwidth... because it says it's loading the image from your server to the user's computer.
|
|
|
|
|
Hm, I see. Oh well. 15GB a month from a free, reliable host is a pretty good deal, so I should be less spoiled Anyways, in terms of paid hosting, what site do you recommend? Please, no bluehost, hostgator, hostmonster, dreamhost, or 1and1. Or any site that oversells, or that matter. Looking for at least 30GB transfers and 3-5GB storage for a reasonable price.
Last Edit: Jun 27, 2009 4:44:29 GMT by perishingflames
|
|
|
|
I use hostgator, so there goes my suggestion. <.< Best bet is to create another thread and ask.
|
|
|
|
|
You could modify the script and use fopen(), file(), or probably even file_get_contents() to make it load from an external source.
|
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.
|
|
|