03
Mar 10

Finding corrupted images

As zoe kindly pointed out, two Wednesdays have passed since I last posted. I blame the Olympics (I was in Vancouver for the first week, and I was still recovering from it the second week Go Canada!). You can see my photos on flickr.com as I post them here. Apparently you can only post 100 mb/month, so it might take a while.

DSCN0543

Anyway, onto the post:

This month at work, one of our servers decided to act up and corrupt quite a few things. We had about 10000 images on that server, many of them were corrupted, many of them were not. Instead of going through all of the images one by one, opening them and checking if they were corrupted, I was tasked with writing a php script to find them. My first instinct was to do something like this:

 
<?php
 
if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
 
    /* loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        if(@getimagesize($file) === false){
            echo "$file\n";
        }
    }
}
 
?>

Unfortunately, getimagesize only looks at the start of a png file to get the information, meaning that if the file was corrupted after those first few bits of information it would not show up on the list.

The second idea was to use this method:

<?php
 
if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
 
    /* Since they are all png files, this will work: */
    while (false !== ($file = readdir($handle))) {
        if(@imagecreatefrompng($file) === false){
            echo "$file\n";
        }
    }
}

Which, in theory, should have worked because it reads the whole file in and then returns false if the image could not be created. Unfortunately there was (is) something wrong with the libpng library so I kept getting errors like this locally:

libpng warning: Ignoring bad adaptive filter type
libpng warning: Extra compressed data.
libpng warning: Extra compression data

Which would be fine if they were php errors. I could have ignored them because the script worked properly and listed all of the files that had problems. Unfortunately for what ever reason, the server was dying on these errors instead of continuing like it was doing locally.

In comes imagemagick to the rescue. Using the identify command (at the command line) it reads in the whole image file and then tells you what it is. Since you might have 10000 images like we did, it is probably also a good idea to send the output to a file instead:

    identify "./path/to/files/*" >badImages.txt 2>&1

You will also want to make sure that the path has ” ” around it, because otherwise you will end up with an “Argument list too long” error. In the badImages.txt file you will have a list of images that are in the folder you specified. Any of the lines that start with identify are no good.


11
Feb 10

Getting Started With PHPUnit

We all agree that testing code is better than not testing it right? So why do we tend to avoid writing unit tests to make sure that we are writing code that works? I’m looking at you PHP guys. Good thing there is PHPUnit and it is as easy as can be to get started with it.

First you have to discover the channel:

pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com

Sidenote: phpunit.de has many useful packages in it including phpcpd, phpdcd, and phploc.

Then you install it (I usually install all of the dependencies with PHPUnit, but you don’t really have to).

pear install phpunit/PHPUnit

After that you can start writing tests.

Say you have a simple class like this:

<?php
class needsTesting{
	public function divide($a, $b){
		return $a/$b;
	}
}

Your unit test file would be this (prior to adding any test cases to it):

<?php 
 
require_once 'PHPUnit/Framework.php';
 
require_once 'needsTesting.php';
 
class needsTestingTest extends PHPUnit_Framework_TestCase {
	/**
	 * @var needsTesting
	 */
	protected $object;
 
	/**
	 * This method is called before a test is executed.
	 */
	protected function setUp() {
		$this->object = new needsTesting;
	}
 
	/**
	 * This method is called after a test is executed.
	 */
	protected function tearDown() {
	}
 
	public function testDivide() {
 
	}
}

After you have added the obvious test cases, your testDivide() function should look something like this:

<?php
//[...]
	public function testDivide() {
		$array_a = array(
			10,
			20,
			30,
			40,
			50,
		);
		$array_b = array(
			2,
			4,
			6,
			8,
			0,
		);
		$array_expected = array(
			5,
			5,
			5,
			5,
			0,
		);
		foreach($array_a as $k=>$a){
			$b = $array_b[$k];
			$expected = $array_expected[$k];
			$result = $this->object->divide($a, $b);
 
			$this->assertEquals($expected, $result);
		}
	}
//[...]

Running the code is quite easy, if your ide has a PHPUnit extension (like Netbeans does) you can run it from there, or you can run it from the command line (in the folder you are writing the tests in):

 
phpunit needsTesting

Since one of the tests will give you an error, you should get this:

 
 
PHPUnit 3.4.9 by Sebastian Bergmann.
 
E
 
Time: 0 seconds, Memory: 5.25Mb
 
There was 1 error:
 
1) needsTesting::testDivide
Division by zero
 
[...]/needsTesting.php:4
[...]/needsTestingTest.php:56
 
FAILURES!
Tests: 1, Assertions: 4, Errors: 1.

Now it is up to you to decide how your application handles the Divide by 0 error that you got (or any other errors for that matter). While testing first is a good way to define exactly how your app should work, testing after is good too (as long as you are testing it is a good thing).

Check it out:

http://www.phpunit.de/manual/current/en/


09
Feb 10

New posts every Wednesday

So, it turns out that having a blog is not of much use if you don’t actually post anything on it. Unfortunately I have been neglecting mine as of late, so I am going to try and remedy that by forcing myself to post something on every Wednesday. Why Wednesday? Why not.


07
Feb 10

Daily Digest for February 7th

twitter (feed #2)
twitter (feed #2)
@chriscoyier That seems to be moving way too far in FF 3.6 [SeanJA]
twitter (feed #2)
@late2game Aren’t there enough monopoly games already? [SeanJA]
twitter (feed #2)
RT @donttrythis: This: http://www.youtube.com/watch?v=h-8PBx7isoM Is one of the loveliest PSAs I’ve ever seen. [SeanJA]
twitter (feed #2)
Little known fact about Christopher Walken: He can fly [SeanJA]
twitter (feed #2)
twitter (feed #2)
"I am infinitely more intelligent than you can ever hope to be." http://vogons.zetafleet.com/files/orly.jpg [SeanJA]
twitter (feed #2)
I am going to start at infinity and count backwards until I have enumerated all of the values between it and 0 [SeanJA]
twitter (feed #2)
Infinity…. uhhh…. infinity minus one…. infinity minus two…. infinity minus three… #ThisCouldTakeAWhile [SeanJA]
twitter (feed #2)
People people people…. Representable != Enumerable [SeanJA]
twitter (feed #2)
Ok… I mean representable is not necessarily enumerable [SeanJA]
twitter (feed #2)
Time to kill some zombies [SeanJA]
googlereader (feed #5)
twitter (feed #2)
Think of me as your parole officer with a mace. [SeanJA]
twitter (feed #2)
rt @mike_acton: Me: "Being a teenager is like being a newbie person." Daughter: "No, I *am* a person!" Me: "Whatever, noob." [SeanJA]
twitter (feed #2)
Remember when movie websites were not all flash? http://www2.warnerbros.com/spacejam/movie/jam.htm [SeanJA]
twitter (feed #2)
… and @levarburton as Black Lightning… awesome [SeanJA]
twitter (feed #2)
Screw you, I don’t want to log in to vote up an answer… I shouldn’t have to [SeanJA]
twitter (feed #2)
Ghost Hunters is a rather silly show… Just people walking around in the dark with cameras [SeanJA]
twitter (feed #2)
Oh my god! Fake heartbeat sound! Intense! #OhWaitGhostsArentReal [SeanJA]
twitter (feed #2)
100 episodes of this show?!?! [SeanJA]
twitter (feed #2)
@late2game When the Me generation grew up and earned a lot of money by being greedy? [SeanJA]
twitter (feed #2)
For professional ghost hunters… they really scare easily [SeanJA]

06
Feb 10

Daily Digest for February 6th

twitter (feed #2)
twitter (feed #2)
@late2game Ya, I saw that a while ago, this is a much better quality version of it though [SeanJA]
twitter (feed #2)
Apparently there is some sort of football thing this weekend [SeanJA]
twitter (feed #2)
@late2game He gives checkmate? [SeanJA]
twitter (feed #2)
Want to see a magic trick? [SeanJA]
twitter (feed #2)
@feliciaday Damn you… I want a lightsaber [SeanJA]
twitter (feed #2)
@feliciaday Pft you held it upside down… [SeanJA]
twitter (feed #2)
$%#$%# you bell, not letting me download legal torrents at any kind of speed… [SeanJA]
twitter (feed #2)
@neilhimself That is what you get for trusting Steve [SeanJA]
twitter (feed #2)
twitter (feed #2)
They originally named it "Scrotum humanum"? Really? Come on scientists… http://bit.ly/bFoO8v [SeanJA]
twitter (feed #2)
Who’s playing the super-bowl? [SeanJA]
twitter (feed #2)
Reward: 3000 Mega credits [SeanJA]
twitter (feed #2)
There is always a surplus of dry ice machines running on space ships [SeanJA]