Viewing posts tagged with gd

Sparklines via PHP

Around 5 years ago I built a simple sparkline generation script in PHP. It wasn't very good, the lines were shaky even with antialias. During last weekend I thought I'd rebuild it from scratch to see how much better it would be second time around.

A few hours later and it was complete, the lines were thick, antialiased and almost identical in quality to the Google Image Charts version. I also added browser caching using the ETag header.

The key bit of logic that I failed to think of 5 years ago was to create the original sparkline larger than the desired size, then scale down by the same factor. The resizing naturally makes the lines smoother than imageantialias and imagesetthickness ever could.

Sparkline Examples

It's as easy as this to use:

<img src='sparkline.php?size=80x20&data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&back=fff&line=5bb763&fill=d5f7d8' />

I use sparklines in a lot of my work related projects, as it's an intuitive way to display stats that only require a quick glance. Normally I'd use the Google Image Charts but has been depreciated since April 2012, and will become fully defunct in April 2015. Whilst there is the newer Google Charts library which uses HTML5, SVG, and JavaScript I prefer to use the image-only method for a leaner project with less bloat.

Check out the code on Github

20-Jun-2014 at 12:26pm

Minecraft Skin Preview via PHP

I've expanded on my Minecraft Avatar script I built a while back so that you can now display a skin preview for a given username, like so:

Minecraft Skin Preview

It's as easy as this to use:

<img src='skin.php?u={username}&s={size}' />

Just like the facial avatar, this script grabs their skin from the Minecraft server, and then creates the 4 different views, including facial accessories such as hats or glasses. If the username does not exist, then it uses the default skin, which is stored in a base64 encoded variable to save an extra cURL request.

View the code on Github

16-Sep-2013 at 6:57pm

Thumbnail Creation via PHP

I got a bit tired of numerous security holes and sheer bulkiness of other thumbnail generation scripts, so decided to build my own from scratch, for local images only. Simply upload, create a writable cache directory and parse the image through the PHP script like so:

<img src='thumb.php?src=./images/photo.jpg&size=400x300' />

It's super simple and only 155 lines in total. You can specify the width and height, or just the width so the height will get calculated automatically and vice versa. It also allows you to set a maximum width/height, which is best suited for photo thumbnails.

Features:

  • File based cache
  • Automatic deletion of expired cache files
  • Optional browser based cache via the IF_MODIFIED_SINCE HTTP header
  • Multiple size types
  • Options whether to crop, trim, zoom and set alignment
  • Set sharpness based on a percentage midpoint between two values, so you can specify the strength of the sharpness rather than just turning it on or off

View the code and usage instructions on Github

18-Jul-2012 at 1:00pm

Minecraft Avatar via PHP

Here's a little something I built for fun a while back, when I was more active on Minecraft. I wanted a real simple way to generate a user's Minecraft avatar from their skin, and be able to display at any size.

Minecraft Avatar

It's as easy as this to use:

<img src='face.php?u={username}&s={size}' />

What the script does is grabs their skin from the Minecraft server, copies the face and facial accessories only and merges them into an 8 x 8 image, then resizes to the specified size. If the username does not exist, then it uses the default skin, which is stored in a base64 encoded variable to save an extra cURL request.

View the code on Github

23-Feb-2012 at 4:21pm