Apr 20 2009

Uploading an Image in PHP Gives a Black Box (Another Internet Explorer Error)

Really, I’m tired of supporting IE. Half the time, I find a “mistake” in my HTML coding that I didn’t catch in Firefox, because Firefox is so forgiving with semantics. The other half of my IE errors are really IE’s fault. Let’s take this story (and fix) as one of the flagship examples of Microsoft Idiocracy.

I build a simple image upload script in PHP. On the page where the form posts its data to, I used PHP’s built in getimagesize function to get data about the image. What I wanted was the mime-type–for example if it was a JPG, GIF, or PNG–so I could do further processing on it.

For JPG images, the mime type is “img/jpeg”, for GIF–”img/gif”, and so on. Once you know the mime type, you can use PHP’s imagecreatefromjpeg, for example, to create a new image from the JPG file that was uploaded.

So, in my code, if I wanted to do a switch statement based on the image type, it might look something like this:


$image_data = getimagesize($image_file);

switch($image_data['mime']) {
case “image/jpeg”:
// Do code for a JPG image here
break;

case “image/gif”:
// Do code for a GIF image here
break;
}

In most cases, this works perfectly. That is, until you get to Internet Explorer. Internet Explorer interprets the mime type of a JPG image as “image/pjpeg”, breaking your code, breaking the standards, driving you batty, and adding to it’s lengthy list of inexplicable ways of doing things. To fix, you’d simply change the code to:


case "image/jpeg":
case "image/pjpeg":
// Do code for a JPG image here
break;

… and the world is right again.


Feb 23 2009

Reverse TinyURL, bit.ly, is.gd, and more with PHP and cURL

I’m working on a new project that requires me to take URLs made from url shorteners like tinyurl.com, is.gd, and bit.ly, and reverse them back to their full form.

There are a few online services that do this for you already (even a funny one that takes a small URL and makes it huge), and some of the shortners–like tinyurl–have a few back-door methods to let you reverse their URLs. I needed one I could place in a PHP script–and one that didn’t have the overhead of some I had found across the web.

The PHP code below will take a shortened URL and return you its full form. This has worked on every short-url I’ve tried–but if you find one it stumbles on, please let me know.

See a live demonstration

Code:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://bit.ly/aKAuf");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$result = curl_exec($ch); 

if (preg_match("/Location\:/","$result")) {
	$url = explode("Location: ",$result);
	$reversed_url = explode("\r",$url[1]);
	echo $reversed_url[0];
} else {
	print_r($result);
}

Feb 11 2009

Find Replace in MySQL

Somewhere between the backing-up and restoration of my MySQL WordPress database I picked up some strange characters. Rather than having to go through by hand to edit them out, I used a nice SQL snippet to do a full-database search and replace.

update [table_name] set [field_name] =
replace([field_name],'[string_to_find]','[string_to_replace]');

So in my case, to get rid of these weird “” characters, I ran the statement

update wp_posts set post_content = replace(post_content,'ÂÂ','');

Worked perfectly for me, it should for you as well.


Feb 8 2009

FOWA 2009

As mentioned previously, I’ve taken a new job that requires me to commute an hour each day. This is a bit different than my 10 minute commute I had for the prior 10 years.

The great thing is I get a lot of time each morning and evening to listen to podcasts. I’ve almost caught up on 2 years of shows from Boagworld and various MP3’s from Carsonified events. It’s been great, and I’m surprised how much I’ve learned just listening. I’ll give you a review later of these and other shows I’m having time to listen to.

That being said, there’s nothing as good as hands-on experience. If you get the chance, catch The Future of Web Apps conference this year in Miami or Dublin. Ryan Carson and the rest of the presenters always have great information that will inspire you in some way. And, if you feel like taking me along, drop me a line.


Feb 8 2009

Back again.

It’s been a long time since I’ve been able to post–the new job has really taken its toll on my free time but I finally figured out how to better manage my time.

I moved servers to MediaTemple, who are great, but unfortunately I lost a lot of the images that go with my posts. I am looking for a backup but don’t have a lot of confidence that I’ll find it. UPDATE: FOUND!

Thanks again for all the emails and comments, let’s get this train back on the track.


Jul 19 2008

Now in 2600

If you don’t read The Hacker Quartlery, then this won’t intersest you much, but I had an article published in this quarter’s edition. The article is from one of my January blog posts, Cracking MD5 Hashes With The Webtionary. You can buy the magazine now at most major book retailers.


Jun 7 2008

Versions finally OUT!

Versions, the Mac Subversion Client, is finally out. I really don’t have much more to say about it other than it’s great for lazy people like myself who just don’t like messing with subversion from the command line.

Check it out at http://www.versionsapp.com


May 16 2008

Guitar Chords for the Rest of Us

A year-and-a-half ago, I woke up Christmas morning to an acoustic guitar. This was my first guitar, meaning I’m only about twenty-years or more behind all the kids who received the same thing that year. I have a lot of catching up to do.

Since that time, I’ve bought the usual requisite of books any beginning guitarist would buy, including Guitars for Dummies, which I highly recommend. By now I know all the major chords, most of the minors, and even a few of the crazy Gadd9’s and F#m’s. My biggest problem is finding popular songs to play that are transcribed for newcomers like me.

There are many quality chord sites like Chordie.com (my previous favorite), Ulitmate-Guitar.com, and GuitarETab.com, but I finally found one suited just for me: PJ’s Guitar Chords. PJ has transcribed well over a thousand popular songs, and put them in chords that are easy for any beginning guitarist.

Just as an example, consider “The Scientist” by Coldplay. Most chord sites will have:

Dm           Bb                    F                        F9
Come up to meet you, tell you I'm sorry, you don't know how lovely you are

Where Dm, Bm, F, and F9 are fairly hard chords for beginning guitarists. PJ gives you another way to play it, and to my ear, it sounds just as great:

Em           C                     G                        G9
Come up to meet you, tell you I'm sorry, you don't know how lovely you are

Where Em, C, and G are three of the easiest chords on the guitar, even a non-guitarist could play the intro to “The Scientist” after 5 minutes study. My other favorite thing about this guitar chord site is that each song is formatted in plain-text, which means you can print out your favorites and not have to worry about chords jumping around the lyrics because HTML scrambled it on print (if you’ve tried to print from one of the other chord sites you know what I mean).

If you’re interested in more, check out PJ’s blog or jump straight to his chords at http://www.iol.ie/~murphypj.

For those of you having trouble knowing how to play certain chords, I always keep the guitar chords chart at 8notes.com handy.


May 14 2008

Badly Englished

I post this for three reasons. One, to let you know I’m alive. Two, because it really confused me when I read this, and I wanted to see if it was just me or the author. And three, because it’s really annoying to read this on a “professional” website.

This blurb was found on the Major League Baseball section of ESPN.com. Headlines are meant to be quickly skimmed and comprehended. We shouldn’t have to read a headline more than a couple of times before we understand the point of the story. Read the offending headline highlighted below, and see how long it takes for you to figure out what it’s talking about.

“Rookie called up after kneecap KO’s 2nd Reds SS.”

Maybe it’s just me, but it took me three reads to figure out that a “rookie short-stop was called up from the minor leagues after two previous short-stops for the Cincinnati Reds had suffered knee-cap injuries.” Granted, that’s a lot to say in 10 words or less, but I think “Kneecap injury sidelines second Reds SS | Rookie called up” would have been better.

If you’re from another country that knows nothing about baseball, you probably wouldn’t be on this site–but if by mistake you had stumbled upon it, you would have no idea what that statement meant. You could look at the other headlines around it and easily infer what was going on with them. Maybe mixing two abbreviations (KO’s and SS) should be a no-no when writing headlines.

Yes, I have more important things to do with my time than to rant on ill-formed headlines, but for the sake of humanity I couldn’t help myself.


Apr 29 2008

Accessing your iSight Camera with Flash

This took a while for me to figure out, hope it helps someone. (I’m using Leopard on an iMac, with Flash CS 3). If you follow the Adobe instructions for connecting your Flash app to your built-in iSight camera, things will start great at first, and you should be prompted to connect to your camera when you run the code:

Click allow, and you’ll likely only see a blank screen. And that’s where I became stuck. After some searching, I found you have to actually change the type of camera inside Flash from the default setting, which is “DVCPRO HD”, to a USB based one. To do this, run your SWF, right-click on the movie, then click on “Settings…”

From the Settings menu, click on the small camera icon, and switch to a “USB Video Class Video” camera.

That should work for you. You should see your iSight light activate and see video in your Flash app. Let me know if you have troubles.