(11-09-2013, 07:56 PM)puggsoy Wrote: Looking good! PHP seems like a pretty nice language, I'll have to check it out when I can.
PHP has its... quirks. This image describes PHP best if it were a tool:
OH MAN, I FINALLY FINISHED SOMETHING PROGRAMMING RELATED!
![Big Grin Big Grin](https://www.vg-resource.com/images/custom_smilies/biggrin.png)
It's not exactly a big job, but I feel pretty great now
![Smile Smile](https://www.vg-resource.com/images/custom_smilies/smile.png)
Source code is here:
Code:
<?php
date_default_timezone_set('America/Chicago');
$filename = ""; // Filename of the image
$format = ""; // Image format
$url = ""; // Image URL
/*
Daily Dilbert strip image retriever & generator, created by Phaze.
Do what you want with it, but if you're going to edit it, please inform me since I'm curious about how you changed it.
Also, not misrepresenting who made it would be much appreciated.
On a side note, you would not BELIEVE how much this crashed Apache when I worked on it.
It even tried to delete itself and the entire folder in order to go out in a blaze of glory. Notepad++ won out, though.
*/
// I dunno if anyone will ever know this is a thing but I imagine it might be handy for some folk.
if(isset($_GET['src']))
{
$source_code = file_get_contents(__FILE__);
die("<!DOCTYPE HTML>\n".
"<html>\n".
" <head>\n".
" <link rel=\"stylesheet\" type=\"text/css\" href=\"dilbert.css\" />\n".
" <title>Dilbert comic image server source code</title>\n".
" </head>\n".
" <body>\n".
" <pre>\n".
htmlentities($source_code)."\n".
" </pre>\n".
" </body>\n".
"</html>");
}
// Is it Sunday? Better watch out, this arbitrarily changes filenames!
$isSunday = strtolower(date("l")) == "sunday"? true : false;
$sunday = ""; // Filename modifier string, need bigger scope.
// Generate the comic URL. Did we specify a format to select and is it JPG?
if(isset($_GET['format']) && (strtoupper($_GET['format']) == "JPG" || strtoupper($_GET['format']) == "JPEG"))
{
$sunday = $isSunday? "s" : "d";
$filename .= "dt".date("ymd").$sunday."hct.jpg";
$url = "http://thedilbertstore.com/images/periodic_content/dilbert/".$filename;
$format = "jpg";
// Man this is way more complex than it needs to be. I'll just try changing the timezone (see above) instead of doing this hacky shit.
// The main problem is that because I am testing this in Europe, I hit a new day before the Dilbert site updates its comic.
//$url .= date("ym") . str_pad(date("d") - 1,2, "0", STR_PAD_LEFT);
}
else // No? We usin' GIF, then
{
$baseIndex = 202256;
$url = "http://www.dilbert.com/dyn/str_strip/"; // base URL
$dirSize = 9; // Number of 0s in the most significant digit for the Dilbert GIF images.
$baseDate = strtotime("2013-11-08"); // UNIX timestamp, reference date for getting future strips.
$curDate = strtotime("now"); // Current time, I just felt like making a comment because gaps bug me.
$format = "gif";
// Subtract UNIX timestamps and divide by 86,400 to get the days (decimal), then floor() it to remove the decimal.
$indexDiff = floor(($curDate - $baseDate) / 86400 ); // Number of comic indexes since the base index.
// Thank you for arbitrarily using a different index and filename for Sunday. God fucking dammit. Now I feel like Dilbert.
if($isSunday)
{
$baseIndex = 196050; // SUNDAY BASE ASSUMING DIRECT CONTROL.
$indexDiff = ($indexDiff % 7) - 1; // THIS HURTS YOU, DILBERT.
$sunday = ".sunday"; // YOU HAVE BECOME AN ANNOYANCE, DILBERT.
}
$curIndex = $baseIndex + $indexDiff;
$dirDiff = $dirSize - strlen($baseIndex); // Get the current number of unused significant directory digits.
$paddedIndex = str_repeat("0", $dirDiff) . $curIndex; // Sort-of hacky but it feels like the easiest way to deal with the 0'd directories.
// The two least significant digits of the index don't matter, but everything beforehand is used in the URL directory listing.
for($i = 0; $i < $dirSize - 2; $i++)
{
// Add "$dirsize - (i + 1)" zeros to the current digit directory to pad it out accordingly.
// It basically works out that a decreasing number of zeros are added to each dir digit.
$url .= $paddedIndex[$i] . str_repeat("0", $dirSize - ($i + 1)) . "/";
}
$filename .= $curIndex.".strip".$sunday.".gif";
$url .= "{$curIndex}/".$filename;
}
//Consult the 'cache' to see if the current strip has already been downloaded.
if(!file_exists($filename))
{
// Enumerate the directory to delete any leftover files that are the same format as the requested image.
// I really don't want to end up with > 1,000 Dilbert comics on my webserver since my host might complain.
if ($handle = opendir('.'))
{
while (false !== ($entry = readdir($handle)))
{
// Get the file extension. Is the format of the file GIF? Delete any GIF files. Ditto for JPG if selecting JPG.
if (substr($entry, strlen($entry) - 3, 3) === $format)
{
unlink($entry);
}
}
closedir($handle);
}
copy($url, $filename);
}
// Render the image, based on format
if($format == "jpg")
{
header('Content-Type: image/jpeg');
$img = imagecreatefromjpeg($filename);
imagejpeg($img);
imagedestroy($img);
}
elseif($format == "gif")
{
header('Content-Type: image/gif');
$img = imagecreatefromgif($filename);
imagegif($img);
imagedestroy($img);
}
?>
![Cool Cool](https://www.vg-resource.com/images/custom_smilies/cool.png)
Ref:
1 (main)
2 (alt. form design), 3 (alt. form colors)
Love:
Kami | Previous[0][1] | Vic | Virtuaboy | Hiyna | NICKtendo DS
1 (main)
2 (alt. form design), 3 (alt. form colors)
Love:
Kami | Previous[0][1] | Vic | Virtuaboy | Hiyna | NICKtendo DS
![[Image: 3CczX.gif]](http://i.imgur.com/3CczX.gif)
![[Image: qjGOacY.png]](http://i.imgur.com/qjGOacY.png)