Sunday, December 31, 2017

=== update

I updated my old Triple Equals Overrated ramble with a long postscript... while I'm not suggesting changing === as default for my company, or even the received wisdom of "avoid type coercion for comparisons), the people who like double equals aren't JUST dunderheaded cavepeople...

Thursday, December 28, 2017

Monday, December 25, 2017

pretty print json from unix shell

I've been digging using little .json files on the filesystem as a kind of DIY NoSQL database, but they're not always pretty to look at. Was looking for a pretty printer for 'em, thought I might have to do something in node or python but "json_pp" is already there in my Mac Terminal. The only weird thing is it only seems to read from STDIN (not from a file) so you have to do
json_pp < SOMEFILE.json

Sunday, December 24, 2017

asssteroidsss and sound generation

I used to make small games fairly regularly, often for Glorious Trainwrecks. Sadly this might be my first (and last, I suppose!) game of the year...

Pippin Barr (who has a great blog) made a set of games called Sibilant Snakelikes, older games as seen through a classic Snake game lens (his take on Shadow of the Colossus is especially great)

Since I was in middle school, I've used the idea of "move something on screen with an x/y position by only adjusting an xspeed and yspeed variable and adding them to x and y position every clock tick" - it's a very easy to program way to get a pleasing sense of motion in a game world. (The pinnacle of exploiting this was probably my Atari 2600 mashup JoustPong)

Atari's Asteroids used a roughly similar physics, albeit with more sophisticated rotations, so I wanted to see what happens if I further shoehorned it into the quantized world of Snake -

You can see the game and fiddle with its code here. One thing it's doing is using the P5.js sound libraries to make the notes - I really like this approach, having all the graphics and the sounds embedded in one file as text is a great throwback to the old 8-bit type bit computer magazines, vs having external graphics and sound files. Unfortunately, the library isn't super clearly documented, and mixed up with the functions that rely on external mp3s or wavs... If my understanding firms up I'll try to write more on it. (I'm tempted to make my own mini-sequencer from it...)

learn css grid there

An online course on css grid.

Thursday, December 21, 2017

php - sending mail/email

I've found it tough to reliably send mail from PHP, and the command line in general.

It feels like gmail (my usual testing recipient) really hates old school ways of sending email via scripts - like to the extent stuff doesn't even wind up in the recipient's SPAM box, it just goes away.

I'll still look for a better solution but right now the most reliable thing I've found is


<?php
// Pear Mail Library                                                                                                                                                             
require_once "Mail.php";

$from = '<kirkjerk@gmail.com>';
$to = '<kirkjerk@gmail.com>';
$subject = 'Hi testing mail!';
$body = "Hi,\n\nHow are you, how are thigns going?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'kirkjerk@gmail.com',
        'password' => 'MYPASSWORD'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

?>
As far as I can tell, much of the header stuff (like trying to munge the From) is ignored- plus you have to have your password in plain text in a script which stinks.  I'll try to update this entry if I refine this technique, like getting reply-tos working etc.

2019 UPDATE: I feel like at some point I discovered an "oh wait, is it that easy?" method for this which I then forgot... now I'm wondering if the linux command line "mailx" (vs the older "mail") might be the key that Google lets through (though don't try to forge the "from" very much!). And in fact the above PEAR method might not work with google as the smtp host.

Wednesday, December 20, 2017

php - imagemagick for thumbnail gallery making

Just here for reference is a script I used to make a simplistic thumbnail gallery with links to fullsize, full of copy and paste bits from a few sources. You need a folder named thumb/ where it will dump the smaller versions. It uses Imagemagick thumbnailImage to set a maximum size of 300x300 but preserve the aspect ration and then dumps HTML I copy and paste into the index. This is for a gallery archive for my real-world Wall o' Peeps




<?
$skips = "";
$files = array_diff(scandir("."), array('..', '.'));
foreach($files as $i => $justname){

    $UPPERCASE = strtoupper($justname);

    $UPPERCASE = strtoupper($justname);
    if(endsWith($UPPERCASE,".JPG") || endsWith($UPPERCASE,".JPEG") ){

        $filename = $justname;
        $image = new Imagick($filename); 
        autorotate($image);
        $d = $image->getImageGeometry(); 
        $w = $d['width']; 

    
            $image->thumbnailImage(300,300,true);

            $image->writeImage("thumb/".$filename);
            $d2 = $image->getImageGeometry(); 
            $w2 = $d2['width']; 
            $h2 = $d2['height'];
            print htmlspecialchars("<a href=\"$justname\"><img width=\"$w2\" height=\"$h2\" src=\"thumb/".$justname."\"></a>\n\n");
            print "<br><br>\n";
       

    } else {
    $skips = $skips . "  skip $justname<br><br>";
    }
}
print "<br><br>$skips";

function autorotate(Imagick $image)
{
    switch ($image->getImageOrientation()) {
    case Imagick::ORIENTATION_TOPLEFT:
        break;
    case Imagick::ORIENTATION_TOPRIGHT:
        $image->flopImage();
        break;
    case Imagick::ORIENTATION_BOTTOMRIGHT:
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_BOTTOMLEFT:
        $image->flopImage();
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_LEFTTOP:
        $image->flopImage();
        $image->rotateImage("#000", -90);
        break;
    case Imagick::ORIENTATION_RIGHTTOP:
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_RIGHTBOTTOM:
        $image->flopImage();
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_LEFTBOTTOM:
        $image->rotateImage("#000", -90);
        break;
    default: // Invalid orientation
        break;
    }
    $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
    return $image;
}


function startsWith($haystack, $needle)
{
     $length = strlen($needle);
     return (substr($haystack, 0, $length) === $needle);
}

function endsWith($haystack, $needle)
{
    $length = strlen($needle);

    return $length === 0 || 
    (substr($haystack, -$length) === $needle);
}

?>