Users browsing this thread: 1 Guest(s)
JavaScript -> Auto-updating Image
#16
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.
Thanked by: puggsoy
#17
Looking good! PHP seems pretty handy, I'll have to check it out when I can.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#18
(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:
[Image: 6a0120a85dcdae970b017742d249d5970d-800wi]

OH MAN, I FINALLY FINISHED SOMETHING PROGRAMMING RELATED! Big Grin
It's not exactly a big job, but I feel pretty great now Smile

Source code is here:
See the script in action here Cool
[Image: dilbert.php]
Thanked by: puggsoy
#19
I LOVE YOU. Thank you thank you thank you, you're awesome Big Grin

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 Tongue 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.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#20
(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 Tongue 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
Thanked by: puggsoy
#21
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.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#22
(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
Thanked by: puggsoy
#23
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.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#24
(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.
Thanked by:
#25
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.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#26
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*
Thanked by: puggsoy
#27
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.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Thanked by:
#28
(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
Thanked by: puggsoy
#29
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.
B A N D C A M P - T W I T T E R - T U M B L R - Y O U T U B E - G 1 5
Call me aggressive, call me obscene,
but you've always called me sir when you've invaded my dreams.
Thanked by: puggsoy
#30
(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 Nerd
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.
Thanked by: puggsoy


Forum Jump: