Tag Archives: PHP

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:

PHP South Coast 2015

I’ve just arrived back from the inaugural PHP South Coast Conference.  It was a really great event that seemed to go without a hitch, had a fantastic range of speakers and a nice bunch of people that attended.  So first of all, a really big thank you! to PHP Hampshire, BrightonPHP and PHP Dorset for putting on a top-notch event – you really wouldn’t have known it was their first one as everything seemed so professional! Continue reading PHP South Coast 2015

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:

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:

Zend OPcache – some info and a GUI

I’ve just started to use Zend OPcache in place of APC in a few places and so far it’s great!

Because documentation seems a little scarce right now, I’ve decided to jot down the methods available (mainly so I don’t forget).

opcache_reset
Should be pretty obvious given the name – resets the opcache

opcache_get_configuration
This will return an array with three indices, one for the directives that have been set, one for version information and one for any blacklisted paths. For example:

Array
(
    [directives] => Array
        (
            [opcache.enable] => 1
            [opcache.enable_cli] => 1
            [opcache.use_cwd] => 1
            [opcache.validate_timestamps] => 1
            [opcache.inherited_hack] => 1
            [opcache.dups_fix] => 
            [opcache.revalidate_path] => 
            [opcache.log_verbosity_level] => 1
            [opcache.memory_consumption] => 134217728
            [opcache.max_accelerated_files] => 4000
            [opcache.max_wasted_percentage] => 0.05
            [opcache.consistency_checks] => 0
            [opcache.force_restart_timeout] => 180
            [opcache.revalidate_freq] => 2
            [opcache.preferred_memory_model] => 
            [opcache.blacklist_filename] => 
            [opcache.max_file_size] => 0
            [opcache.error_log] => 
            [opcache.protect_memory] => 
            [opcache.save_comments] => 1
            [opcache.load_comments] => 1
            [opcache.fast_shutdown] => 1
            [opcache.enable_file_override] => 
            [opcache.optimization_level] => 2147483647
        )

    [version] => Array
        (
            [version] => 7.0.2-dev
            [opcache_product_name] => Zend OPcache
        )

    [blacklist] => Array
        (
        )

)

opcache_get_status
This will return that holds information about whether the opcache is turned on or not, whether it’s in a restart phase, the memory usage and hit statistics, and any files that have been cached along with hit and memory details. For example:

Array
(
    [opcache_enabled] => 1
    [cache_full] => 
    [restart_pending] => 
    [restart_in_progress] => 
    [memory_usage] => Array
        (
            [used_memory] => 9395880
            [free_memory] => 121987104
            [wasted_memory] => 2834744
            [current_wasted_percentage] => 2.1120488643646
        )

    [opcache_statistics] => Array
        (
            [num_cached_scripts] => 353
            [num_cached_keys] => 1546
            [max_cached_keys] => 7963
            [hits] => 27055
            [start_time] => 1365412384
            [last_restart_time] => 0
            [oom_restarts] => 0
            [hash_restarts] => 0
            [manual_restarts] => 0
            [misses] => 509
            [blacklist_misses] => 0
            [blacklist_miss_ratio] => 0
            [opcache_hit_rate] => 98.153388477725
        )

    [scripts] => Array
        (
            [/http/includes/libs/ZF-1.12.2/Zend/Loader/Autoloader.php] => Array
                (
                    [full_path] => /http/libs/ZF-1.12.2/Zend/Loader/Autoloader.php
                    [hits] => 175
                    [memory_consumption] => 63320
                    [last_used] => Mon Apr  8 16:21:14 2013
                    [last_used_timestamp] => 1365434474
                    [timestamp] => 1343660895
                )

            [/http/www-includes/libs/ZF-1.12.2/Zend/Db/Adapter/Oracle.php] => Array
                (
                    [full_path] => /http/libs/ZF-1.12.2/Zend/Db/Adapter/Oracle.php
                    [hits] => 17
                    [memory_consumption] => 60600
                    [last_used] => Mon Apr  8 16:21:14 2013
                    [last_used_timestamp] => 1365434474
                    [timestamp] => 1325795702
                )

            [/http/libs/ZF-1.12.2/Zend/View/Helper/Placeholder/Container.php] => Array
                (
                    [full_path] => /http/libs/ZF-1.12.2/Zend/View/Helper/Placeholder/Container.php
                    [hits] => 175
                    [memory_consumption] => 2744
                    [last_used] => Mon Apr  8 16:21:14 2013
                    [last_used_timestamp] => 1365434474
                    [timestamp] => 1325795702
                )
                ...
        )

)

GUI

APC did have a GUI which was quite handy to see what had been cached, settings and memory usage but Zend OPcache doesn’t (currently as far as I could see) have anything similar.

It is possible to get information from looking at the output of phpinfo() which contains all of the key information.

However, I’ve just pushed the start of a GUI to GitHub. If you want to make changes and improvements then please feel free! It’s only had a very little amount of work done on it so far so is pretty raw.

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:

Sorting an array of objects by one or more object property

Quite often I find myself having an array of objects and needing to sort that array of objects by property (either one property or multiple)…

Imagine, for example, getting a large result set from your database and ordering in the query just takes too long. Or perhaps you’re getting results from a web service and that service doesn’t return the results in the order you’d like to use. Have you ever found yourself in that situation, too? On looking at the usort documentation one day I came across a comment by someone called Will Shaver that did almost what I wanted. With a little adaptation for my own use (being able to change the sort order, for example), it has become one of my favourite functions to use for sorting.

loading gist...

Now a few cools things about the function:

  1. It uses anonymous/lambda functions (or closures, whatever your prefer to call them), and that’s just plain fun
  2. You can sort on more than one property and because the sorting is recursive, it’ll sort the second property within the confines of the first, the third within the confines of the second, and so on. Think sorting in SQL
  3. You can sort in ascending or descending order for any of the properties
  4. It retains key associations so you could use this on an associative array of objects
  5. If the parameter you want to sort on is an array itself then you can use any value (by specifying it’s key) in that array as the sorting value
Did you like this? Share it:

PHP Team Development book review

Book coverSo I’ve finally finished the book!  OK, I finished it a couple weeks ago but haven’t had a chance to post up a review yet.  Of course, I had every intention of finishing it a lot earlier considering I was flying for nine hours to the States and then another few hours on to Mexico – and the journey back again! – but that really was just wishful considering I was travelling with my two year old son.  Oh well! 🙂

On with the book review…

The book, as the title makes it plainly obvious, is about developing your team in relation to working with PHP.  It’s aimed at, well, pretty much anyone who has an interest in developing or working in a team, be it managers who need to set up and manage teams or developers working within a team who want to improve their work flow and procedures, or anyone in between.  It does this by giving an overview on several subjects, but doesn’t go as far as to tell you that you must do x, y or z.  This is understandable, though, as every team is different and the book acknowledges this.

Continue reading PHP Team Development book review

Did you like this? Share it:

PHP Team Development (book)

Book coverThe other day I had a new book sent to me called PHP Team Development, written by Samisa Abeysinghe and published by Packt Publishing. Unfortunately, it arrived at work when I was on holiday so I haven’t been able to have a look at it yet. :-/ However, I’m back today and have the book in my hands (well, not literally, of course, else typing would be much more difficult), so am looking forward to diving in to it.

Hopefully have a bit of a review posted up here some time soon!

Did you like this? Share it: