So i recently created a banner for use in my forum signature for Vaping Underground. It’s dynamic; it displays how long it’s been since i’ve been quit smoking in years, months, and days, plus how much money i’ve saved in that time (with a rough accounting for how much i’ve also spent on vaping gear), and how many cigarettes i’ve avoided in that time also. There’s also a random quote at the bottom just for fun. Here’s what it looks like:
Somebody asked me how i did it, so i posted the code there. I cobbled it together from code i found across the internet. Eventually i ended up replacing all the code i found with snippets from php.net. Note that this is really only for vaping enthusiasts.
This is probably very inefficient code. It’s… been a while since i’ve coded anything at all. As the Department of Homeland Security says, “If you see something, say something.” Feel free to alter it – i’ve licensed it under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
And here we go.
<?php
/*
********************************************************************************************
* x jeremy jarratt coded this terribly didn't he - https://transmothra.com/ - @transmothra
* PRAISE DOBBS! HAIL ERIS! ALL HAIL DISCORDIA!
* This work is licensed under the
* Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
********************************************************************************************
*/
// EDIT THESE NEXT FEW LINES
// set your timezone or suffer the consequences
date_default_timezone_set(‘America/New_York’);
// set your quit date
$quitdate = “2010-08-16”; // change this to your quit date
// how much is a pack of smokes worth where you live?
$cigprice = 5.00; // omit the dollar sign
// how many packs per day did you smoke?
$packsperday = 2.5; // decimals ok
// how much do you spend on vaping per month?
$vapecostmonthly = 50; // omit the dollar sign
// set your font by filename – UPLOAD YOUR FONT FILE TO THE SAME DIRECTORY
$font = ‘./trebuc.ttf’;
$fontb = ‘./trebucbd.ttf’;
// number of pixels for the left margin
$left_margin = 5;
// HEY LET’S CALCULATE A BUNCH OF CRAP (in general, leave this alone)
$prettydate = date_create_from_format(“Y-m-j”, $quitdate); // no, not a girl/boy. just a nicer-looking date format.
$now = time(); // this is really NOW, for comparison to your quit date
$your_date = strtotime($quitdate);
$datediff = $now – $your_date; // compare your quit date to right now (output in seconds)
$datetime1 = new DateTime($quitdate);
$datetime2 = new DateTime();
$interval = date_diff($datetime1, $datetime2); // just another time comparison
$howlong = $interval->format(‘%y years, %m months, %d days’);
$vapedays = floor($datediff/(60*60*24)); // convert time since quitting to days (rounded down to eliminate any remainder)
$vapecostdaily = $vapecostmonthly/30; // divide monthly cost by 30 for a daily cost
$vapecost_ttl = $vapecostdaily*$vapedays; // multiply daily vape cost by total days since quitting
$vapesaved = $vapedays*$cigprice*$packsperday-$vapecost_ttl; // days x cost x ppd, minus vaping costs
$cigsavoided = $packsperday*20*$vapedays; // ppd x 20 smokes per pack x total days since quitting
$packs = $cigsavoided/20; // total number of packs avoided since quitting
// format numbers: decimal places, decimal separator, thousands separator
$vapesaved = number_format($vapesaved, 2, ‘.’, ‘,’);
$vapecost_ttl = number_format($vapecost_ttl, 2, ‘.’, ‘,’);
$cigsavoided = number_format($cigsavoided, 0, ‘.’, ‘,’);
$packs = number_format($packs, 0, ‘.’, ‘,’);
$vapedays = number_format($vapedays, 0, ‘.’, ‘,’);
// put in a bunch of quotes or whatever you want; space is limited!; rotated on a random basis
$randomquote = array(
‘How can I be free in the shadow of a chapel? (anon)’,
‘We are a way for the cosmos to know itself. (C.Sagan)’,
‘What can be asserted w/o evidence can also be dismissed w/o evidence. (C.Hitchens)’,
‘I\’d take the awe of understanding over the awe of ignorance any day. (D.Adams)’,
‘I love deadlines. I love the whooshing noise they make as they go by. (D.Adams)’,
);
$quotation = $randomquote[rand(0, sizeof($randomquote) – 1)];
// DON’T EDIT UNLESS YOU KNOW WHAT YOU’RE DOING
$image = ‘signature_bar.png’; // create a banner image with this name; 468 pixels wide by 60 pixels tall
$im = imagecreatefrompng($image);
// set colors (RGB)
$main_color = imagecolorallocate($im, 51, 102, 153);
$black = imagecolorallocate($im, 0, 0, 0);
$orange = imagecolorallocate($im, 255, 180, 90);
$blue = imagecolorallocate($im, 180, 200, 255);
$cyan = imagecolorallocate($im, 200, 215, 255);
$cyan2 = imagecolorallocate($im, 215, 235, 255);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 160, 0);
$white = imagecolorallocate($im, 235, 235, 255);
$grey = imagecolorallocate($im, 216, 216, 216);
$yellow = imagecolorallocate($im, 255, 216, 64);
/* OUTPUT FOR TESTING/DEBUGGING – TO TEST, COMMENT OUT EVERYTHING BELOW THIS BLOCK TO THE END
echo “now = “.$now.”
“.
“quitdate = “.$quitdate.”
“.
“your_date = “.$your_date.”
“.
“datediff = “.$datediff.”
“.
“howlong = “.$howlong.”
“.
“vapedays = “.$vapedays.”
“.
“vapesaved = “.$vapesaved.”
“.
“cigsavoided = “.$cigsavoided.”
“.
“packs = “.$packs.”
”
;
*/
// THE MESSAGE
$line1 = ‘Vaping since ‘.$prettydate->format(‘j F Y’).’; ‘.$howlong;
$line2 = ‘Cigarettes avoided: ‘.$cigsavoided.’ in ‘.$packs.’ packs’;
$line3 = ‘Money spent on better things: $’.$vapesaved.’ + $’.$vapecost_ttl.’ shamelessly vaporized’;
// the spacing
$line1vert = 16;
$line2vert = 32;
$line3vert = 48;
$line4vert = 64;
// THE MAGIC: set font size, font angle, horizontal position, vertical position, color, fontface, text
// Add the text – drop shadow
imagettftext($im, 12, 0, $left_margin-2, $line1vert+2, $black, $fontb, $line1);
imagettftext($im, 10, 0, $left_margin-2, $line2vert+2, $black, $fontb, $line2);
imagettftext($im, 9, 0, $left_margin-2, $line3vert+2, $black, $font, $line3);
imagettftext($im, 8, 0, $left_margin-1, $line4vert+2, $black, $fontb, $quotation);
// Add the text
imagettftext($im, 12, 0, $left_margin, $line1vert, $blue, $fontb, $line1);
imagettftext($im, 10, 0, $left_margin, $line2vert, -$cyan, $fontb, $line2);
imagettftext($im, 9, 0, $left_margin, $line3vert, -$white, $font, $line3);
imagettftext($im, 8, 0, $left_margin, $line4vert, -$cyan2, $fontb, $quotation);
// LEAVE THESE ALONE DAMN YOU
header(‘Content-Disposition: filename=signature_bar.png’);
header(‘Content-Type: image/png’);
imagepng($im);
imagedestroy($im);
?>
