*/ ?>

PHP Error with ImageCreateFromBMP function

Filed Under PHP/MySQL | 2008-10-14, 13:49

This post is for the web/php geeks out there.
I have a page that needs to be able to accept multiple image formats as the users are not to be expected to format their image just right. In short it takes whatever image the user provides and then converts it to a PNG I can use. In an attempt to support as many image formats as possible, I’ve had to jump through a few hoops. Luckily the GD library with PHP has the following functions: imagecreatefrompng, imagecreatefromjpeg, imagecreatefromgif. However what GD does not have built in is imagecreatefrombmp. Boo! But thanks to PHP.net’s users, there’s a function written by DHKold.

So I added this function to my code, and things seem to be working fine. But lately I noticed that my error logs were huge! We’re talking hundreds of megs. I looked into them and one of the main errors flooding my logs was this:

[Tue Oct 14 01:06:39 2008] [error] PHP Warning: unpack() [<a href='function.unpack'>function.unpack</a>]: Type n: not enough input, need 2, have 0 in /usr/www/path/to/my/file.php on line 157

It was repeating itself several hundred times a second. This was not right. I took a look at my code and line 157 was the unpack call in the following block of code:

elseif ($BMP['bits_per_pixel'] == 16)
$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];

Looks like 16bit BMPs aren’t really compatible with this function. *grumble grumble* Image formats are not my forte, and I have zero interest in learning about all the various ins and outs. Luckily the PHP user comments come in handy again! domelca writes:

function ImageCreateFromBMP($filename) don't work with bmp 16 bits_per_pixel
change pixel generator for this
elseif ($BMP['bits_per_pixel'] == 16)
$COLOR = unpack("v",substr($IMG,$P,2));
$blue = ($COLOR[1] & 0x001f) << 3; $green = ($COLOR[1] & 0x07e0) >> 3;
$red = ($COLOR[1] & 0xf800) >> 8;
$COLOR[1] = $red * 65536 + $green * 256 + $blue;

I’ve updated the code in my file and now I’m watching the error logs closely to see if anyone’s images trigger the error again. Why PHP doesn’t provide an official imagecreatefrombmp() I don’t know, but hopefully this fix will help support a few more different images as well as keep my error logs a little smaller. Figured I’d post this to save someone else the hassle of hunting it down.


Disqus Comment System Added

Filed Under PHP/MySQL, Wordpress | 2008-08-21, 16:01

Disqus Logo

Comments are both a love and a hate of mine. I love hearing back from people. It’s great to hear suggestions, arguments, opinions, etc. What I hate is the spam from comments and the not so great WordPress comment system. Don’t get me wrong WordPress, I know how much writing a good comment system sucks. I’m looking at doing this for some other sites that I coded from scratch, and let me tell you, I’m not looking forward to it.

So today I finally decided to install the Disqus plugin for WordPress. For those of you that browse a lot of blogs, chances are you’ve run across Disqus on a site. I’ve been wary of using a 3rd party for something as integral to a site as a comment system as I’ve been burned in the past. The latest Disqus plugin allows you to sync comments back to WordPress. This means that if sometime down the road I decided that the Disqus system isn’t for me, then I can switch back to the default WordPress comments without skipping a beat.

The installation of Disqus was almost painless. I downloaded the .zip file, extracted it into the plugins folder and activated the plugin. The configuration screen asked me for my username/password and then found my account and this blog. That was it. Disqus was setup. If this had been a brand new blog, I would have been done. However I wanted to import all my old WordPress comments into Disqus. So under the Advanced Options, I clicked Import. After a second of working it spit out text from all my comments, and an error saying php had run out of memory. I realized that Spam Karma 2 (a now outdated spam plugin) had marked a couple hundred comments as spam that Disqus was attempting to address. I went into Spam Karma, cleaned everything out and then ran an import again with success.

So what’s so great about Disqus? Well it uses a single sign-in across all the blogs that use it, so you won’t ever have to re-register on a new blog that has it installed. It also ties into FriendFeed for those of you that know what FriendFeed is. I hear it has good spam filtering, which I hope is 100% true. And it also integrates Seesmic, the video comment system that I’ve been meaning to get installed on here for awhile. So, leave me a video comment if you can! And the thing that sealed the deal was the ease of installation and the ability to rollback instantly should I hate it. If Disqus lives up to the expectations, I may also look into using it on my other sites since they plan to have an API soon and that would be great for integrating into my existing code. Let me know what you think of the new comment system.


PHP to quickly rename files based on a CSV

Filed Under PHP/MySQL | 2007-02-27, 23:35

I love php/mysql. Almost all of my sites are written with this pair. I decided earlier this week that I needed to launch a new site. Basically splitting off part of an existing site into its own site. The code was already written and luckily I had kept it in my head to keep it portable, so that took approximately 20-30 minutes to move over, change some settings, test, etc. But the problem was the database, as it referenced folder names that would no longer be applicable since they were based on an auto incrementing number in the database. 228 folders to be exact. My first thought was, “No big deal, some manual renaming, it’ll take an hour or two.” I did 5 of them and said forget this. I exported the folder name and an identifying filename from the old database, then exported the same info from the new database, matched them up in Excel, killed the filename columns, and then exported a csv that contained the new foldername and the old foldername. A little php scripting and in seconds 200+ folders were renamed. Granted I had a few that had to be done manually, but only about 5 or 6. For anyone that cares, here’s the quick and dirty php code to rename based off of a csv list of newname,oldname.

$handle = fopen(“file.csv”, “r”);
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
   if (rename(“$data[1]”, “$data[0]”)) {
    print “Rename: <font color=\”green\”>Success</font><br />\n”;
   } else {
    print “Rename: <font color=\”red\”>FAILED!</font><br />\n”;