PHP Logo

Add Effects To Images Using Image Filters With PHP

24th July 2008 - 5 minutes read time

The imagecreatefromjpeg() function has been part of the PHP library since version 4, allowing programmers to load an image directly from a file. However, the imagefilter() has only been available since version 5 and adds a nice little set of effects to an already useful function.

The effects are created using the function in conjunction with a set of constants. Take the following image taken from the free stock images site.

Default Electronics image

I chose this images because it has lots of colour and will show the techniques working well. To create the effects you need to use the following bit of code, in this case I am using the negative filter.

Linux Logo

Using !$ To Use Last Parameter

22nd July 2008 - 2 minutes read time

Much like using Alt+. to print out last parameter you can also use !$ to use the last parameter from the previous command. Here is a simple example.

# cd ..
# cd !$

In this example we are moving up a directory and then doing the same action again, the !$ is a short cut to get hold of the ... This is more useful when doing things with longer parameters like directory or file names. For example here we are creating a directory and then moving into that same directory.

# mkdir /www/htdocs/directory/
# cd !$
Linux Logo

Raising Skinny Elephants Is Utterly Boring

21st July 2008 - 3 minutes read time

This might sound odd, but this is a mnemonic that helps you remember a sequence of letters that you can enter when your Linux system is locked. This is a last ditch attempt to get things up and running again and should only be used if all else fails and the only other thing that you can do it pull the plug.

If you have also tried pressing Ctrl+Alt+backspace and this does nothing then you can try using the key sequence Raising Skinny Elephants Is Utterly Boring.

Hold down the left Alt key and the SysRq key (found on the print screen button) and press each letter in turn. Make sure that you give a little time between keystrokes.

  • r
  • s
  • e
  • i
  • u
  • b

Here is a description of what you are doing.

PHP Logo

Printing Out File And Class Information In PHP

18th July 2008 - 2 minutes read time

If you are debugging a PHP application then you might want more information than the values of some current variables. There are a number of built in magic variables that can be used to print out the file name, line number, class and method that the debug statement is printed out on. Here is an example that prints out some information from a class.

MySQL Logo

Optimize A MySQL Table Using PHP

17th July 2008 - 3 minutes read time

In MySQL the OPTIMIZE TABLE can be used if you have made changes or have deleted large parts of the table.

Any deleted rows are kept behind the scenes in the server in order to allow the reuse of these spaces. The OPTIMIZE TABLE command reclaims the unused space and defragments the data file.

For a normal MyISAM table the OPTIMIZE command works in the following way.

  1. If the table has deleted or split rows, repair the table.
  2. If the index pages are not sorted, sort them.
  3. If the table's statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.

You can do this automatically by using the following PHP code.

PHP Logo

Overloading Functions In PHP

16th July 2008 - 2 minutes read time

One thing that PHP doesn't allow is the creation of multiple functions with the same name. If you try this then PHP will give you a fatal error. Creating different functions with the same name is useful if you want to pass different parameters to a function that produces the same result. There are two ways around this problem.

The first is that you can pass the function an array of the items that you want. You can then use this array in the function to do whatever you want. For example.

PHP Logo

Look At PHP Execution Times With PHP Benchmark

15th July 2008 - 2 minutes read time

I have been doing a lot of benchmarking lately with regards to what is the most efficient way to program PHP. PHP Benchmark is a site that is dedicated to that single task.

phpbench.org

This site is in the early stages but looks very promising and deals with some of the major assumptions that programmers have about PHP, including the single and double quotes issue. I would say that most of the tests are only run a few times (around 1000) and so this doesn't give a full representative picture, but I look forward to seeing more from this site in the future.

Here are some links to my own benchmarking tests on #! code

Linux Logo

Search And Highlight With Grep

14th July 2008 - 1 minute read time

Searching for things in many files is easy with the grep command, but in order to view the results with the expression results highlighted you need to use grep in conjunction with less.

grep expression *.txt | less +/expression

If you wanted to search all files called something.txt for the letter d you would use this line.

grep d *.txt | less +/d
PHP Logo

Writing phpinfo() To File

11th July 2008 - 2 minutes read time

The contents of phpinfo() are quite useful, and it is usually the first thing that many developers perform to make sure that PHP is installed. However, printing out the phpinfo() function can lead to a security risk because it displays a lot of information about the server.

Here is how to write the contents of the phpinfo() function to a file.

ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
 
$fp = fopen("phpinfo.html", "w+");
fwrite($fp, $info);
fclose($fp);

This is an example of output buffering.

PHP Logo

Fastest Way To Match String With PHP

10th July 2008 - 6 minutes read time

There are many ways to find a string within another string using PHP, but which one is the quickest? I did the same sort of test that I have done with double or single quotes and downloading web pages with a few of the string matching functions available in PHP.

I first generated a very long string that could be used in the string matching functions. The following for loop generates a string containing "iteration 0 and iteration 1 and iteration 2..." right up until "iteration 4999 and".


$testString = '';
for ($i=0; $i < 5000; $i++) {
 $testString .= "iteration " . $i . " and ";
}

This was then passed to a number of string matching functions. This included the regular expression libraries contained in ereg() and preg_match(). The strpos() and strstr() functions were used as these are another common way of string matching, as well as some of their variants for matching case insensitive strings.