Random imgur image loader Just a little thing I made to load 20 random images from imgur. I looked at the way that imgur references images on its site, and I felt like I could probably generate a random string of letters and numbers that would, on occasion, produce a valid image URL. So I threw this together in PHP, because I am trying to learn PHP. It takes a while to get the 20 that it does, way longer for more. I would like to speed it up, and my current project in PHP is to learn more about classes etc, but I really have no idea where to start.

I would love some feedback! I know this looks real amateur hour, but I am a real amateur, so go easy on me!

BEWARE: not everything on imgur is worksafe, so if you decide to try this code out on your own server, the images returned are truly random with no filter, so no telling what you might see.

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Random imgur Loader&lt;/title&gt;

    &lt;style type='text/css'&gt;
        #bg {
            position:fixed; 
            top:-50%; 
            left:-50%; 
            width:200%; 
            height:200%;
            z-index: -10;
        }
        #bg img {
            position:absolute; 
            top:0; 
            left:0; 
            right:0; 
            bottom:0; 
            margin:auto; 
            min-width:50%;
            min-height:50%;
            z-index: -10;
            opacity: 0.4;
        }
        #container { 
            width: 760px; 
            margin: 0 auto; 
        }
        .imgcell {
            border: 1px solid black;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id='container' style='text-align: center;'&gt;

&lt;?php
$gcode1=generateCode(5);
$url3="http://i.imgur.com/".$gcode1.".jpg";
?&gt;
&lt;div id="bg"&gt;
    &lt;img src="&lt;?=$url3?&gt;" alt=""&gt;
&lt;/div&gt;
&lt;?php
$pagepath=$_SERVER["PHP_SELF"];
if ($_GET['numimg']=='') {
    $numimg=20;
} else {
    $numimg=$_GET['numimg'];
}
?&gt;
&lt;h1 style='font-family: verdana;'&gt;&lt;?=$numimg?&gt; random imgur images&lt;/h1&gt;
&lt;table border=0&gt;&lt;tr&gt;
&lt;?php

function generateCode($length=6) {
    $source='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $code='';

    for ($i=0; $i&lt;$length; $i++) {
        $code .= $source[(rand() % strlen($source))];
    }

return $code;
}

$ii=1;
while ($ii&lt;=$numimg):
    $gcode=generateCode(5);
    $url="http://i.imgur.com/".$gcode.".jpg";
    $url2="http://www.imgur.com/".$gcode;
    $headerfile=get_headers($url2, 1);
    $http_code=$headerfile[1];
    $imgheader=get_headers($url, 1);
    $imgcode=$imgheader["Content-Type"];
    #echo $imgcode;

    if ($imgcode == 'image/gif') {
        $bcode='red';
    } else {
        $bcode='gray';
    }

    if($http_code!="HTTP/1.1 404 Not Found") {
        print("&lt;td style='border: 2px solid $bcode' &gt;&lt;a target='_blank' href='$url2'&gt;&lt;img title='$imgcode' width='160px' height='160px' src='$url'&gt;&lt;/a&gt;&lt;td&gt;");
        echo(str_repeat(' ',4096));
        if ($ii % 5 == 0) {
            print("&lt;/tr&gt;&lt;tr&gt;");
        }
        flush();
        $ii++;
    }
endwhile;
?&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;?php print("&lt;p&gt;&lt;a href='$pagepath'&gt;Get $numimg more!&lt;/a&gt;&lt;/p&gt;");?&gt;
&lt;/div&gt; 
&lt;/body&gt;
&lt;/html&gt;
Definitely separate your PHP from HTML as Alex suggested. You probably don't need to go the full MVC route for something so simple, but simply generating your PHP variables then outputing your HTML would make your code a lot more readable/manageable. 

I like your idea of generating a random string and checking it, but Flambino's right, it will never be reliable (by design) - also, imgur probably hates you ;) A simpler approach would be to consume imgur's RSS feed: http://feeds.feedburner.com/ImgurGallery?format=rss

I was bored one morning, so I added something similar to the login page of one of my projects. It pulls one random image from lolcats' RSS feed and inserts it into the page. Here's the code that pulls the image:

$feed = $this-&gt;get('feed_parser'); // This is just a SimplePie object
$feed-&gt;set_feed_url('http://feeds.feedburner.com/lolcats/rss');
$feed-&gt;init();
$items = $feed-&gt;get_items();
$item = $items[array_rand($items)]; // Gets one random image, but can modify for more
$item = $item-&gt;get_content();


$this->get('feed_parser') is just a fancy way of getting a SimplePie object from the PIMPLE container - you could just instantiate a SimplePie object yourself (if you want code you can't test). After running this code, $item would be a PHP array (or some collection class) containing the details of one image. In my case, I then exposed this as JSON and a REST API endpoint for use by JavaScript, but you could just as easily have PHP output the appropriate HTML.