Chris Jean
Linux, WordPress, programming, anime, and other stuff
  • Home
  • Linux
  • Development
  • Random Ramblings

Archive for PHP

Creating a PHP ICO Creator for Favicons

by Chris Jean
July 21st, 2011

For the past couple of weeks, I have worked on creating a PHP library to create ICO files. This has been difficult as all the documentation for the ICO format is either sparse or not completely documented.

I’m doing this since I have yet to find a way of creating a valid favicon file using just PHP and PHP’s GD library. Sure, I can create a PNG file and change the extension to “.ico”, but that doesn’t work in all browsers, on all operating systems (such as Windows XP), and is more of a hack than a valid method for creating favicon files.

So, it is time to make one. I have a placeholder github repository named php-ico.

The primary goal is to make a robust, reliable library that creates valid ICO files that can be properly rendered on Windows XP+ and all the major browsers. Thus, my initial goal is not to support every format that exists nor is it to support parsing the ICO files. If the project gets enough attention, I’ll look at expanding the scope.

Here are the resources that I’ve found helpful as I’ve worked on this project:

  • ICO (file format) – Wikipedia article about the ICO file format. This has a very comprehensive description of the ICO header. It is sparse on details about the format of the image data.
  • BMP file format – Wikipedia article about the BMP file format. This helped me understand the headers and pixel data format for the BMP image data.
  • Portable Network Graphics – Wikipedia article about the PNG file format. This gave me what I needed to know to identify PNG image data.
  • Windows Bitmap File Format – Very good article that delves into more details about the BMP file format. The extra details about the different types of headers, compression methods, compression encoding, and examples were very helpful.
  • Icons – A Microsoft Developer Network article from 1995 that provides the original ICO file format spec. While this is the most authoritative, I really didn’t find this to be as much help as other sources.
  • Replacing ICON resources in EXE and DLL files – Article from The Code Project that has a breakdown of the format similar to what is found in the MSDN article. Example C code is provided.
  • Support Vista-style ICO files – Mozilla Bugzilla filed bug. The discussion has some great information about methods used to reliably parse varying ICO formats and structures. It also has some good test ICO files to try.
  • Convert HBITMAP to .ICO file – This Chironex Software blog post provided the last piece I needed: how to structure the opacity mask data. Where he found this information (since I didn’t find it in any of the above articles) I haven’t a clue.

If this post gets some traction and interest, I’ll consider putting together a post detailing all the different pieces I’ve found here. I’m not a spec writer, but I’d try my best. Let me know if you’d like to have me work on this.

Categories Development
Comments (0)

Fix PHP 4 “Client does not support authentication protocol requested by server”

by Chris Jean
May 25th, 2010

I’m working on building an ideal server setup that allows for both PHP 4 and PHP 5 on Apache with suPHP (I’ll blog about this later). While testing my PHP 4 build, I got the following error:

Warning: mysql_connect() [function.mysql-connect]: Client does not support authentication protocol requested by server; consider upgrading MySQL client in /var/www/test-php.php on line 3
Couldn’t authenticate with MySQL

The code I used to test this is quite simple:

<?php

if ( false === ( $db = mysql_connect( 'localhost', 'username', 'password' ) ) )
    die( "Couldn't authenticate with MySQL" );

if ( false === mysql_select_db( 'database' ) )
    die( "Couldn't connect to database" );

echo "Yay!";

?>

After digging around for a bit, I found that mixing PHP 4 with a MySQL version greater than or equal to 4.1 causes this problem. MySQL 4.1 introduced a new password caching scheme that PHP 4 can’t work with.

The solution is to update the database user’s password using the OLD_PASSWORD function of MySQL. For example:

[chris@office ~]$ mysql -u root -p mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 267
Server version: 5.1.41-3ubuntu12.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> update user set Password=OLD_PASSWORD('password') WHERE User='username';
Query OK, 0 rows affected (0.02 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

Note the underlined areas. That is where you’ll want to provide your own username and password.

Once you’ve followed these steps, both PHP 4 and PHP 5 will be able to communite with the database.

Thanks to digitalpeer for providing the answer to my issue.

Categories Linux
Comments (2)

Use PHP Pear with Ubuntu

by Chris Jean
November 2nd, 2009

PEAR is PHP’s equivalent of Perl’s CPAN. It offers hundreds of ready-to-use code modules that can make projects go much more quickly than having to hand code everything. However, it never seems like PEAR is easy to get running.

Installing the Needed Software

In Ubuntu, installing the following packages will quickly get you started with PEAR: php5-cli, php5-dev, and php-pear.

Make sure that you read the next section about problems with using PEAR to install PEAR packages if you are running 9.10, Karmic Kaola.

Here’s an example of how to quickly install those packages from the terminal.

[chris@rommie ~]$ sudo apt-get install php5-cli php5-dev php-pear
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
autoconf autoconf2.13 automake automake1.4 autotools-dev libltdl-dev libssl-dev libtool m4 php5-common shtool zlib1g-dev
Suggested packages:
autobook autoconf-archive gnu-standards autoconf-doc gettext libtool-doc automaken gfortran fortran95-compiler gcj php5-suhosin
The following NEW packages will be installed:
autoconf autoconf2.13 automake automake1.4 autotools-dev libltdl-dev libssl-dev libtool m4 php-pear php5-cli php5-common php5-dev shtool zlib1g-dev
0 upgraded, 15 newly installed, 0 to remove and 5 not upgraded.
Need to get 0B/8,690kB of archives.
After this operation, 27.7MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Selecting previously deselected package m4.
(Reading database ... 127272 files and directories currently installed.)
Unpacking m4 (from .../archives/m4_1.4.13-2_i386.deb) ...
...

With these packages, you are ready to roll with PHP and PEAR in Ubuntu.

Problem with Ubuntu 9.10 Karmic Kaola

A great thing about PEAR is that you can quickly install packages with a simple command. For example, “sudo pear install PHP_Parser-0.2.1″ will install the PHP_Parser package. However, this doesn’t work properly in Ubuntu 9.10, Karmic Kaola.

Output of the standard PEAR install command can be seen below:

[chris@rommie ~]$ sudo pear install PHP_Parser-0.2.1
downloading PHP_Parser-0.2.1.tgz ...
Starting to download PHP_Parser-0.2.1.tgz (70,782 bytes)
.................done: 70,782 bytes

While this doesn’t look wrong, it has actually failed. Rather than installing the package, it has simply downloaded the archive, encountered an unchecked error, and crashed. A successful installation has a message saying that the installation is successful.

This problem can be easily fixed by giving the install command the “-Z” option. For example:

[chris@rommie ~]$ sudo pear install -Z PHP_Parser-0.2.1
downloading PHP_Parser-0.2.1.tar ...
Starting to download PHP_Parser-0.2.1.tar (Unknown size)
.............................................................................done: 533,504 bytes
install ok: channel://pear.php.net/PHP_Parser-0.2.1

Notice the “install ok: …” portion of the message. That’s what you should see at the end of a successful installation.

For more details on this bug, please check out Bug #451314 on the Ubuntu bug tracker.

Categories Linux, Tips 'n Tricks
Comments (0)

Generating MIME Type in PHP is not Magic

by Chris Jean
February 14th, 2009

Sorry about the late post everyone. I had a long week and went to bed before making sure a post was queued. Hopefully you’ll forgive me. :)

I’m working on a project where I needed to generate a MIME type given a file name. Not only did I need to create a solution that worked, I also needed the solution to be compatible with PHP 4/5 and not require any additional software to be installed on the host. I thought this would be a simple matter of finding a PHP function that does this. Unfortunately, things were not as simple as this.

Read More→

Categories Development, Tips 'n Tricks
Comments (17)

Unicode Support on CentOS 5.2 with PHP and PCRE

by Chris Jean
January 31st, 2009

Yesterday, I talked about how to get the most out of running regular expressions in PHP. The reason that I needed to dig in deep on regular expression syntax with PHP is because I needed to write some regular expressions that deal with Unicode characters.

After much reading, I believed that I knew everything that I needed. I started writing some regex strings and testing the code. Unfortunately, every time I ran a test with a string that contained Unicode characters, the match failed. When I removed the Unicode characters from the string and tested again, it would work. I was baffled.

Read More→

Categories Linux, Tips 'n Tricks
Comments (63)

PHP Regular Expression Syntax References

by Chris Jean
January 30th, 2009

Since beginning work on my DNS Yogi site, I’ve had to do numerous regular expressions to matching all sorts of string bits. I quickly ran into problems when I realized that I need to add support for Unicode characters since certain TLD registrars support registrations with non-Latin characters.

The main issue is that there are multiple regular expression engines. PHP uses a flavor of the PCRE (Perl Compatible Regular Expression) engine. Each engine and varient of an engine has a slightly different way of handling regular expression syntax. I needed to find out exactly how the PHP regular expression engine worked, and finding that information was not easy.

Read More→

Categories Development, Tips 'n Tricks, WordPress
Comments (0)

Install FFmpeg and ffmpeg-php on CentOS Easily

by Chris Jean
January 13th, 2009

FFmpeg is an amazing collection of open-source tools that can record and stream video and audio. However, it can also transcode video and audio (convert the files to different formats), and that is what has me so excited. There’s also a great PHP package called ffmpeg-php that allows for easy use of FFmpeg from inside PHP scripts. Today, I’m going to see if I can’t help you get both of these set up on your system.

Admittedly, it’s been a while since I’ve tried to install FFmpeg, about two years. I recently thought up some ideas on how I’d like to use FFmpeg, so I thought it was time to give it a try yet again. Today, I’m proud to say that installing FFmpeg is so much easier to install compared to the past, that I dare say it’s simple.

Here is my experience with installing FFmpeg on my server and how to fix the pitfalls that I encountered.

Read More→

Categories Development, Linux, Tips 'n Tricks
Comments (29)

Convert XML to Associative Array in PHP

by Chris Jean
January 12th, 2009

I’m working on a project where I needed to convert an XML doc into an associative array in PHP easily. I hadn’t done any work with XML in PHP yet, so I started digging around the usual places.

Soon, it looked like my best option would be to create a completely-custom XML parsing engine out of PHP’s built-in XML Parser. I started working on a set of code based off of the External Entity Example since that is the only example that actually incorporates the ability to retrieve data and not just attributes. My results were slow and very error prone. Why is this so tough? XML isn’t exactly new, and it is very widespread. Similarly, PHP is an extremely popular language. So, why is it so hard to combine the two and work with them easily? I shouldn’t have to create a custom syntax parser just to be able to read a standard format document into a PHP data structure.

Read More→

Categories Development
Comments (22)

smrsh: “php” not available for sendmail programs (stat failed)

by Chris Jean
January 7th, 2009

I had to configure Sendmail in order for Ubersmith to be able to receive support requests via email. Basically, you configure an alias (/etc/aliases) like so:

support: "|php -q -f /home/html/cron/gateway.php domain.com 1"

This all seemed to be very straightforward. I didn’t even think to test it until hours later. When I finally did test out the support request by email function, I received this very abrupt rebuke from Sendmail in the form of a bounceback:

Read More→

Categories Linux, Tips 'n Tricks
Comments (2)

Trouble with Ubersmith, Ioncube, and mb_internal_encoding

by Chris Jean
January 6th, 2009

Today I’m installing Ubersmith, a billing system solution, on a server to test it out. I have to say that Ubersmith has a more complex installation process than I’m used to these days. I guess that I’ve been spoiled.

Here’s a quick word about what I’m running. My test server is a dedicated system running CentOS 5.2 64-bit. The software setup is nothing special as most of the packages are straight from the repository.

I loaded the release files on the server, untarred them, and proceeded to read the instructions. Everything looked to be straight-forward. I quickly got down to the part about installing Ioncube. That’s when the trouble started.

Read More→

Categories Linux, Tips 'n Tricks
Comments (0)
Chris Jean
Copyright © 2012 All Rights Reserved
iThemes Builder by iThemes
Powered by WordPress