PHP

Posts about the server side scripting language PHP

Steganography With Images In PHP

15th January 2021 - 18 minutes read time

Steganography is the practice of placing a secret message inside another message and only by looking at the original message in a different way can the hidden message be seen. This might be as mundane as writing a page of text and hiding a message in the text using the first letter of every sentence. Only by collecting the first letter of each sentence together can the hidden message be seen and read.

Modern steganography is defined as hiding a message (or file) inside a file. For example, you might look at an image and see an image, but if a message has been hidden you would also be able to extract it if you know where to look.

Color Sorting In PHP: Part 6

2nd January 2021 - 17 minutes read time

I finished my last post on sorting colors using PHP looking at incorporating all three dimensions into the display of the colors. This lead to some interesting displays of colors in discs using different shapes or lengths as an indication of the color in question. It was still lacking actually rendering out the third dimension in any meaingful way though.

As I am essentially looking at three dimensional data I thought about displaying the data as a cube in a 3D engine. I could then map the three dimensions of colors (red, green, blue) into the three dimensions of the 3D engine (x, y, z). This means creating a 3D scene with random color data and rendering that scene out. Funnily enough, there aren't a lot of people writing 3D rendering engines in PHP so I looked into writing a very basic version that would just show point data.

Extracting Data From An MP3 With PHP

28th December 2020 - 27 minutes read time

I was looking at a media player on my phone the other day and was watching the waveform of the music shown on screen. This was a simple indication of the progress through the track and appeared to show quiet and loud sections of the track I was listening to. This got me thinking about how to extract this information as the media player I was watching must do this in real time in order to actually play the data, not just render this representation of the music.

After a bit of research into the MP3 file format I found that the file format is complex, but straight forward enough to process, so I decided to try and extract the data using PHP. I have seen a few techniques to extract the data from MP3 using PHP, but these largely involve applications like ffmpeg to convert the audio format into something like a WAV file, which is then processed.

Adding Arguments And Options To Deployer Tasks

15th November 2020 - 15 minutes read time

I have been adding to my custom Deployer scripts for a number of months and I have now been using it to do more than just deploy my sites. Since it acts as a connection to my website server I have been using Deployer to perform other tasks like creating backups and clearing Drupal caches without having to log into the server to do it. What has helped me here is that I have set out my deployment tasks in a very modular fashion, so although my deployment runs a database backup, I there is nothing to stop me running the database backup command on it's own without doing a full deployment.

Creating A Game With PHP Part 3: Snake

10th November 2020 - 12 minutes read time

So far in this series of posts we have looked at detecting key presses from the user and creating a game of tic tac toe using PHP and the command line. The next step is to create a game that has graphics and real time user input. As we are using the command line we don't have much space to work with so the graphics we create aren't going to be very detailed. The simplest action game I could think of is snake. It has a few simple rules, can have very basic graphics and doesn't involve any physics or other mechanics that would effect the game as a whole. In fact the game snake dates back to the 1976 game Blockade, which was created using just text strings.

Creating A Game With PHP Part 2: Tic Tac Toe

9th November 2020 - 12 minutes read time

Following on from my last post about creating a command line game in PHP we now have a mechanism to listen to keypresses. The next step from here is to create a simple game. After thinking about a game that would fit into the command line I decided that something simple like tic tac toe (also called noughts and crosses) would be a good starting point. The game board is small and the conditions for winning are pretty simple to understand.

Creating A Game With PHP Part 1: Detecting Key Input

8th November 2020 - 6 minutes read time

I was watching a documentary about old computers on YouTube recently and it showed a video of an early computer game creating using the command line. This wasn't a text based adventure game, but a game creating using text for the graphics running as a program on the command line. This got me thinking that creating something like this should be possible using PHP. If it was possible on a 30 year old computer then surely it's possible to get PHP to do it, right? I thought it might be interesting to create a series of posts showing how to put this together.

New Features And Breaking Changes PHP8

29th October 2020 - 19 minutes read time

PHP8 will be released on November 26th 2020 and there is lots being written about the upcoming features in this release has now been released! As this is a major version there will be new features as well as breaking changes so it's important to be aware of what is changing. This will make it easier to think about how PHP8 will effect your applications and what actions you will need to take to ensure you can upgrade without incident.

I thought I would go through a few of the main changes to see whats going to be in the next PHP release.

Running PHP8

A good first step is to look at how to install PHP8 so that you can check it out for yourself. If you are using Ubuntu then the simplest way to install PHP8 is to use the existing ondrej/php PPA library. This can be installed using the following commands.

Committing The Composer Vendor Directory

13th September 2020 - 21 minutes read time
When installing composer dependencies those dependencies are downloaded and stored in the 'vendor' directory. There are options available to install this into a different location than the composer.json file, but it's generally found in the same directory. The vendor directory contains quite a bit of code, which is especially the case if your project contains quite a lot of dependencies. More often than not though, this directory will not contain any code that you have actually written. It will contain the necessary third party libraries that allow your code to work correctly.

Map A Value Between Scales

14th August 2020 - 3 minutes read time

A common technique when creating graphics or visual representations of data is to map a value between two scales. This is useful when working on a set of values and you need to map them to a different set of values in order to show them on a graph.

The maths involved here is essentially figuring out the relationship between the value (v) in our initial scale (x, y) and multiplying this by the maximum range of the second scale (a, b).

  1. a + (b - a) * ((v - x) / (y - x))

Taking the value of 0.5, which is in the scale 0 to 1. Mapping this to the scale 0 to 100 means we make the following calculations.

0 + (100 − 0) × ((0.5 − 0) ÷ (1 − 0))

This results in the value of 50.