Tag Archives: snippet

Calculate aspect ratio

This is a handy little function I’ve used a few times to calculate the aspect ratio of an image or video.  Simply give it the width and height and it’ll give you the aspect ratio.

Did you like this? Share it:

Same height for elements

A while ago I wrote a jquery plug-in that allows you to easily make selected elements all the same height. Well, it turns out that it could be a little better because it didn’t take in to consideration margins, padding and such. So here’s a slightly more robust version.

Did you like this? Share it:

Handy little function

Quite often I find myself wanting to run the same script by either cli or through a browser. But I don’t want to fill my echo statements with <br /> tags if I’m on cli because that’d just look ugly, but at the same time I don’t just want to use \n when outputting in the browser because everything would be on the same line.

This handy little function helps to do simple output that will be readable in the browser as well as the command line:

$_ = function($str) {
    if (PHP_SAPI == 'cli') {
        echo $str;
    } else {
        echo nl2br(str_replace("\t", str_repeat('&nbsp;', 6), $str))."\n";
        flush();
    }
};

Then when I want to echo something I just do:

$_("This is a test\n");
$_("\tTime:" . time() . "\n\n");

Simple but handy.

Did you like this? Share it:

Easy table sorting with jQuery

I came across a jQuery plug-in the other day to sort tables, and it works great and is exceptionally simple to implement (and as anyone who’s flicked through this blog knows, I like the simple things in life… Don’t need any more gray hairs popping up, you know!).

The plug-in is called tablesorter (found at tablesorter.com), by Christian Bach.
Continue reading Easy table sorting with jQuery

Did you like this? Share it:

Simple pleasures with jQuery

Somethings it’s the simple things in life that make you really happy.  For me yesterday, that was thanks to jQuery.  Have you ever wanted to have a check box that, when the user checks it it also checks a lot of other checkboxes?  Yeah, of course you have!  I wanted to do that yesterday.  Now, it’s not the first time I’ve had to do that kind of functionality, but it always came with a bunch of javascript that seemed over the top for what was wanted.  With jQuery it just took a line or two of code!

    $(document).ready(function() {
        $('#selectall').click(function() {
        	$(":checkbox", $('#checkboxlist')).attr('checked', $(this).is(':checked')); 
        });
    });

And with that, on and off go the other checkboxes.

Marvelous!

Did you like this? Share it:

Force a file download

Here’s a small function that will allow you to force a file download.

/**
 * Force a file download via HTTP.
 *
 * File is required to be on the same server and accessible via a path.
 * If the file cannot be found or some other error occurs then a
 * '204 No content' header is sent.
 *
 * @param string $path Path and file name
 * @param string $name Name of file when saved on user's computer,
 *                     null for basename from path
 * @param string $type Content type header info (e.g., 'application/vnd.ms-excel')
 * @return void
 * @access public
 */
/* public static */ function download($path, $name = null, $type = 'binary/octet-stream')
{
    if (headers_sent()) {
        echo 'File download failure: HTTP headers have already been sent and cannot be changed.';
        exit;
    }
    
    $path = realpath($path);
    if ($path === false || !is_file($path) || !is_readable($path)) {
        header('HTTP/1.0 204 No Content');
        exit;
    }

    $name = (empty($name)) ? basename($path) : $name;
    $size = filesize($path);

    header('Expires: Mon, 20 May 1974 23:58:00 GMT');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Cache-Control: private');
    header('Pragma: no-cache');
    header("Content-Transfer-Encoding: binary");
    header("Content-type: {$type}");
    header("Content-length: {$size}");
    header("Content-disposition: attachment; filename=\"{$name}\"");
    readfile($path);
    exit;
}

Very easy to use, too! Here are some examples of how you might call the function:

download('./myfile.txt');

download(__FILE__, 'a file for you.php');

download('/home/you/files/spreadsheet.xml', 'ssheet_' . date('Ymd'), 'application/vnd.ms-excel');
Did you like this? Share it: