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.
<html>
<head>
<title>Random imgur Loader</title>
<style type='text/css'>
#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;
}
</style>
</head>
<body>
<div id='container' style='text-align: center;'>
<?php
$gcode1=generateCode(5);
$url3="http://i.imgur.com/".$gcode1.".jpg";
?>
<div id="bg">
<img src="<?=$url3?>" alt="">
</div>
<?php
$pagepath=$_SERVER["PHP_SELF"];
if ($_GET['numimg']=='') {
$numimg=20;
} else {
$numimg=$_GET['numimg'];
}
?>
<h1 style='font-family: verdana;'><?=$numimg?> random imgur images</h1>
<table border=0><tr>
<?php
function generateCode($length=6) {
$source='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$code='';
for ($i=0; $i<$length; $i++) {
$code .= $source[(rand() % strlen($source))];
}
return $code;
}
$ii=1;
while ($ii<=$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("<td style='border: 2px solid $bcode' ><a target='_blank' href='$url2'><img title='$imgcode' width='160px' height='160px' src='$url'></a><td>");
echo(str_repeat(' ',4096));
if ($ii % 5 == 0) {
print("</tr><tr>");
}
flush();
$ii++;
}
endwhile;
?>
</tr></table>
<?php print("<p><a href='$pagepath'>Get $numimg more!</a></p>");?>
</div>
</body>
</html>
myclass::myfunction(arguments)
or similar. See Model-View-Controller \$\endgroup\$