Category Archives: Coding

Find an Image’s Dimensions and MIME Type with PHP

What’s the easiest way to figure out the width and height of an image, as well as the format of the file, in PHP? Given that the existence of a core function for practically everything is one of the language’s strengths, it’s unsurprising that it’s pretty easy.

The getimagesize() function returns an array containing the width and height and the content type.

//Basic usage
$info = getimagesize($filename);
$width = $info[0];
$height = $info[1];
$format = $info['mime'];

//Alternate one-liner suggested by the manual
list($width, $height, $type, $attr) = getimagesize($filename);

This function is really useful for handling image uploads, since you can’t necessarily trust the MIME type that $_FILES reports. The function actually inspects the file to ascertain the type, rather than relying on what the client reports. Also, you may want to make sure that the uploaded images match the dimensions you have in mind.

Here’s how I like to handle it:

$imgdata = getimagesize($_FILES['image_upload']['tmp_name']);

if ($imgdata[0] > 640 || $imgdata[1] > 480) {
	$errors[] = "Your image is too big!";
}

if (!in_array($imgdata['mime'], array( 'image/gif', 'image/png', 'image/jpeg', 'image/pjpeg' ))) {
	$errors[] = "Your file should be a PNG, JPG or GIF!";
}
Minecraft Icon

Checking the Status of a Minecraft Server with PHP

Have you ever needed to have a script check whether a Minecraft server was online, or retrieve its listing information, like the “Message of the Day” (server description) or the number of active players? Maybe you run your own server, and want to display…

Animating a CSS Sprite With JavaScript

Take a look at this demo, and guess how the animation is achieved. It’s not an animated GIF. It’s a PNG sprite that is brought to life with a little bit of JavaScript. The sprite image is a long strip containing each frame of…

Bobby-Tables.com: A guide to preventing SQL injection

SQL injection: still one of the more common exploits against web applications, and yet it is one of the easier ones to guard against. Unfortunately, bad tutorials often teach newbie developers bad habits that enable injection attacks, rather than using more secure methods of…

BrowserQuest: A Massively Multiplayer Game in HTML5 and JavaScript

Imagine a massively multiplayer game using HTML5 features, such as Canvas and WebSocket, that works in any modern browser. Ridiculous, you say? Then you haven’t seen BrowserQuest yet. The clever demonstration, featured on the Mozilla Hacks blog, works on both desktops and mobile devices,…

Thinking Async

I’ve written about loading JavaScript asynchronously in the past, as it’s a great way to decrease load times and prevent hang-ups when third-party scripts don’t load properly. But Chris Coyier has went and compiled the definitive guide. It covers the basic concepts and reasons…

Git: The Simple Guide

Have you been trying to wrap your head around Git, the version control software that everyone is talking about? It works a little differently than tools like Subversion, so you’ll have to unlearn some habits if you’re familiar with other version control packages. A…

github-icon

Alternatives to GitHub

GitHub is great for open source projects, but the lack of free private repositories can be limiting for projects that you would rather not be shared with the entire world. While it does make sense to pay for a tool if you use it…

JSFiddle: A Playground for Web Developers

JSFiddle is a sort of interactive pastebin site that I’ve been finding useful lately. It features three panes for entering HTML, CSS and JavaScript, and a fourth where the resulting output is rendered. If you save the workspace, it generates a URL like http://jsfiddle.net/fLP64/…

HTML5 Data Attributes

Have you ever looked at the HTML snippet for Twitter’s Tweet button and wondered what those data-something="loremipsum" attributes were? I did too, and after a bit of Google searching, I found a post by John Resig (the creator of jQuery) that explains that they’re…