WCAG colour checker

About five years ago I created a small PHP class that would check the contrast of a foreground and background colour and give a report on whether it was accessible in accordance with the WCAG recommendations (so either AA or AAA compliant).

I’ve recently had cause to dig out that script which gave me the opportunity to also create a version in javascript.

Usage is very simple… Say I have these two colours:

vivid red – rgb(207, 46, 46) – #cf2e2e

and

cyan blueish grey – rgb(171, 184, 195) – #abb8c3

If I used then in combination, with say the red being the background colour and the grey being the text colour, the PHP code would look something like:

var_dump(WCAG::accessibility('abb8c3', 'cf2e2e'));

The result of which would show the contrast ration and if it passes WCAG 2.0 for AA, AA with large fonts, AAA, and AAA with large fonts… No surprise really that the combination above would fail!

array(2) {
'ratio' => string(4) "2.54"
'2.0' => array(4) {
'aa' => bool(false)
'aa-18pt' => bool(false)
'aaa' => bool(false)
'aaa-18pt' => bool(false)
}
}

But try that with the red background and white text and we’d see a different result:

// var_dump(WCAG::accessibility('fff', [207, 46, 46]));

array(2) {
'ratio' => string(4) "5.14"
'2.0' => array(4) {
'aa' => bool(true)
'aa-18pt' => bool(true)
'aaa' => bool(false)
'aaa-18pt' => bool(true)
}
}

You can supply the colours in full or short hex format – with or without the # – or as a RGB array.

The javascript version is just as simple to use. So why not check out the repository with the code, fork is and contribute if you’d like – examples are included in the repo.

Did you like this? Share it:

Filter an array of objects

Quite often I might have an array of objects, be it from a db query or some json object, and I want to filter that list in a particular way.  Lots of times I would find myself doing the same old thing; creating a new array, looping and looping until all I had left was what matched my filter.

I’m sure you’ve been there and done it a thousand times, too.

Well, this little function should help that task out a lot!

Say I had an array of people objects and wanted only those people who’s name was Bob and was aged 35, I could do something as simple as:

$filtered = ofilter($items, ['name' => 'Bob', 'age' => 35]);

Or maybe something a little tricker; I wanted to get anyone whose age was between 18 and 35 (inclusive):

$filtered = ofilter($items, ['age' => function($age) { return ($age >= 18 && $age < = 35); }]);

Pretty easy, eh?

Here’s the code – it’s a GitHub gist, so feel free to fork and improve!
Continue reading “Filter an array of objects”

Did you like this? Share it:

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.

<?php

function ratio($a, $b)
{
    $gcd = function($a, $b) use (&$gcd) {
        return ($a % $b) ? $gcd($b, $a % $b) : $b;
    };
    $g = $gcd($a, $b);
    return $a/$g . ':' . $b/$g;
}

echo ratio(1920, 1080);  // 16:9
echo "\n";
echo ratio(640, 480); // 4:3

Did you like this? Share it:

A PHP extension to control GPIO on the RaspberryPi – my BrightonPHP talk

A while ago now (back in February!) I did a talk about writing an extension using Zephir to interact with the RaspberryPi’s GPIO.  The extension itself wraps around the wiringPi library and it makes it really easy to write PHP scripts that send output and gather input from the GPIO.

Here are the slides (use left/right arrow keys to navigate):

Full-sized version of the slides

Did you like this? Share it:

JW Player thumbnail creator now on Packagist and via Composer

Just a quick note to say that my thumbnail and VTT creator for JW Player, a script to help extract thumbnails from a video and present them in JW Player a a toolbar preview, has now got the ability to be included in your project via composer. Just use:

composer require amnuts/jwplayer-thumbnail-preview-generator

If you want, you can also browse for it on Packagist.

Did you like this? Share it:

phpiwire updated – now with PWM!

I’ve pushed a few changes to phpiwire can now use the PWM functions in wiringPi.  You can use either the hardware PWM pins or any of the GPIOs by using software PWM.  A couple new examples of how to do this have also been added to the repository.

With software PWM comes the requirement to include pthreads, so that’s also linked when you compile the zephir extension.

I’d recommend you do a full clean first before compiling with:

zephir fullclean
zephir install
Did you like this? Share it:

Phpiwire: a PHP extension (written using Zephir) for controlling the Raspberry Pi GPIO

So it’s been a while since my lightening talk on Zephir and I realized I hadn’t really done anything to try to learn it a little more in-depth.  Coincidentally, I was recently going through a drawer and realized that one of my Raspberry Pi’s was in there just begging to be put to some use (seriously, I could practically hear it weep).  So I thought that the only thing to do was to attempt to write an extension using Zephir so that I could control the GPIO via PHP.  I mean, isn’t it obvious? Continue reading “Phpiwire: a PHP extension (written using Zephir) for controlling the Raspberry Pi GPIO”

Did you like this? Share it:

Opache gui update and now on Packagist

I’ve just pushed to github a big overhaul of the opcache gui script. It comes with a sweeping set of changes which also includes using react.js to power some of the interface to really enhance the real-time updates. (OK, it was really just an excuse for me to learn a little about react.js but I think it was worth it!)

I’ve also put the opcache gui and the php error log gui up on packagist – my first packages on there!  Check ’em out:

https://packagist.org/packages/amnuts/opcache-gui
https://packagist.org/packages/amnuts/phperror-gui

Did you like this? Share it:

Create video thumbnail previews for JW Player

JWPlayer is a fantastic video player for the web. One of the great features it provides is the ability to show thumbnail previews of the video as you slide along the toolbar timeline, much the same way that YouTube and the like allow.

Generating the thumbnail images is not quite so easy unless you have ffmpeg installed and know a bit about the command line options. I got a little frustrated myself having to come up the with the command line magic whenever I needed it, and so the JW Player thumbnail preview generator was born!

It’s a simple command line tool, written in PHP, that allows you to whip up the thumbnails and generate the required VTT file with a command as simple as:

php thumbnails.php -i "/input/video.mp4"

OK, so you still need to have ffmpeg installed on your machine – not much I can do about that one, sorry! – but at least generating the files is now much easier!

You can generate the individual thumbnails or have them coalesced into  sprite sheet, and generate a random poster image. Full instructions are on my GitHub account; so go, fork, improve, and feel free to give any feedback.

Did you like this? Share it:

Effective, clean UI for viewing the PHP error log

Example usage

Recently at work I found myself trawling through the PHP error log file and found that all the lines of text were starting to blur after a while. Thinking there had to be a better way over viewing the log file, and following on from my single-file opcache viewer gui, I decided to knock out something for viewing the error log file as well.

With this single-file error log gui it will only show the error once and then tell you how many times it appears in the log. It will allow you to narrow down your search of errors by being able to hide the different types or sort in different ways, and it will also allow you to search results based on particular file paths.

I’ve pushed the code up to my GitHub account, so please feel free to fork and improve and/or give feedback.

Did you like this? Share it: