Code:
$url = "http://thedilbertstore.com/images/periodic_content/dilbert/dt".date("ymd")."dhct.jpg";
One liner for the URL, lol. Compare it to the GIF version:
Code:
$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');
$baseIndex = 202256;
// Subtract UNIX timestamps and divide by 86,400 to get the days (decimal), then floor() it to remove the decimal.
$curIndex = $baseIndex + floor(($curDate - $baseDate) / 86400 );
$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, everything beforehand is used in the URL directory listing.
for($i = 0; $i < $dirSize - 2; $i++)
{
// Add "$dirsize - (i + 1)" 0s to the current digit directory to pad it out accordingly
$url .= $paddedIndex[$i] . str_repeat("0", $dirSize - ($i + 1)) . "/";
}
$url .= "{$curIndex}/{$curIndex}.strip.gif";
Will make the image generation/caching code tonight if I can stop faffing about.
Looking good! PHP seems pretty handy, I'll have to check it out when I can.
(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!
It's not exactly a big job, but I feel pretty great now
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);
}
?>
See the script in action here
I LOVE YOU. Thank you thank you thank you, you're awesome
I also noticed that you specified the timestamp to be the American one. Thanks for that, otherwise I'd get my Dilbert earlier than everyone else that'd just be weird
Question though, how do I pass a parameter for the larger JPG version?
Nice job man, hopefully other Dilbert nerds will see this and can enjoy the awesomeness of a daily image of this great comic.
(11-09-2013, 10:49 PM)puggsoy Wrote: [ -> ]I also noticed that you specified the timestamp to be the American one. Thanks for that, otherwise I'd get my Dilbert earlier than everyone else that'd just be weird Question though, how do I pass a parameter for the larger JPG version?
Well the problem is that it tries to get a comic that doesn't exist if I don't set it to that!
http://phazepages.net/code/dilbert/dilbe...format=jpg (or use "jpeg")
I consider the JPG version ugly though, it has a lot of artifacts (not to mention it's somehow bigger in filesize than the GIF despite it). Sure easier to retrieve the URL though :v
Really? When I used the JavaScript code it got tomorrow's one (since I'm GMT+12). Maybe that's only with the JPG though.
Also yeah, the GIF is more desirable than the JPG. Some people might find the latter useful if they want it a bit bigger, but as you said it has artefacts. What's more it's 800 pixels wide and would be resized in my sig by the forum, which wouldn't look too good. Luckily the GIF is only 680 pixels, so thanks a lot for going to the trouble to get that as well.
(11-09-2013, 11:09 PM)puggsoy Wrote: [ -> ]Really? When I used the JavaScript code it got tomorrow's one (since I'm GMT+12). Maybe that's only with the JPG though.
Also yeah, the GIF is more desirable than the JPG. Some people might find the latter useful if they want it a bit bigger, but as you said it has artefacts. What's more it's 800 pixels wide and would be resized in my sig by the forum, which wouldn't look too good. Luckily the GIF is only 680 pixels, so thanks a lot for going to the trouble to get that as well.
vOv
It's just as well that I screwed up and was working on getting the GIF version's URL, huh :v
Looks like we got a glitch. It's getting the strip of the 4th, which is missing the last 2 speech bubbles. The JPG version gives me a page full of warnings.
By my estimation this happened about 26 minutes ago, when it just passed midnight in New York. The Dilbert site itself is still on the comic of the 9th, so it appears that it changes according to a different time zone, maybe on the American west coast time.
(11-10-2013, 12:27 AM)puggsoy Wrote: [ -> ]Looks like we got a glitch. It's getting the strip of the 4th, which is missing the last 2 speech bubbles. The JPG version gives me a page full of warnings.
By my estimation this happened about 26 minutes ago, when it just passed midnight in New York. The Dilbert site itself is still on the comic of the 9th, so it appears that it changes according to a different time zone, maybe on the American west coast time.
Dammit! Changed to Los Angeles, hopefully that will prevent that happening.
Awesome, thanks!
Tiny fidget, Los Angeles is a bit behind. It probably won't break, but it would be nice to have it switch at the exact same time. I checked and apparently Dilbert updates according to
CST.
EDIT: OK, so it broke with the new hour. I don't get it.
I am... confused. I'll try testing it later to pinpoint the exact problem.
Edit: Well I found the problem for the gif comics. Sunday comics have "sunday" in the filename and a unique ID for some stupid reason. JPGs were a simple fix in comparison, I forgot the "s" : "d" part. For GIFs I need to compute another ID difference.
Ok, I think it should actually work uninterrupted, now.
Code:
// 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.
}
*master of shitty hacks*
You're a saint, my man. Thanks a huuuuuuge bunch!
I would assume that the reason Sunday comics differ is because they are somewhat larger in height. I can't think of a reason why a completely different ID would be necessary, but I guess that's just easier for whatever system the Dilbert site uses. Perhaps there's some sort of feature for grabbing all Sunday comics or something.
(11-10-2013, 02:59 PM)puggsoy Wrote: [ -> ]You're a saint, my man. Thanks a huuuuuuge bunch!
I would assume that the reason Sunday comics differ is because they are somewhat larger in height. I can't think of a reason why a completely different ID would be necessary, but I guess that's just easier for whatever system the Dilbert site uses. Perhaps there's some sort of feature for grabbing all Sunday comics or something.
i'm phaze (no problem).
Maybe! But hopefully that's the last gotcha that I need to deal with for Dilbert comics :v
Not a bad bit of code there, part of me wants to think there must be a more elegant solution to the sunday exception, but if it works it works.
I have to admit that the bug with it loading everything but the text on the last panel gave me a good chuckle, my brain was short-circuiting over "images don't work like that" logic.
(11-10-2013, 06:30 PM)PatientZero Wrote: [ -> ]Not a bad bit of code there, part of me wants to think there must be a more elegant solution to the sunday exception, but if it works it works.
I have to admit that the bug with it loading everything but the text on the last panel gave me a good chuckle, my brain was short-circuiting over "images don't work like that" logic.
I wish there was a more elegant solution that was obvious, it would've made programming it easier
Easier solution on the Dilbert site itself would've been to have a "daily.gif/jpg" that got updated when the CMS he's using had a new comic added to it, but shrug.
Yeah, I think the Dilbert site sends some generic image when that happens.