Wednesday, July 26, 2006

PHP : Getting image size

PHP has a very handy function getimagesize() which returns an array of values like width, height, type etc.

Example

$image_size = getimagesize("path/to/the/image.jpg"); ## URLs can also be passed

$image_size[0] is the width and $image_size[1] is the height of the image.

You can also use the following line (picked from php.net) so as to get the values in four different variables directly with the use of list() function.

list($width, $height, $type, $attr) = getimagesize( "img/flag.jpg");

Wednesday, July 19, 2006

Linux : Appending a tar file with xargs

Whereas -c switch of tar command creates an archive, -r is used to append files to a tar files. In the following example, xargs keeps on sending htm files as input to the tar command. These files are appended to htm_files.tar.

find . -name "*.htm" | xargs tar rvf htm_files.tar

Usual way of tar -cvf does not work for the above purpose.

Sunday, July 16, 2006

Linux : Find and Rename files with -exec

Following command finds .html files in the current working directory and changes the extension to .htm. The command illustrates the use of rename with find's exec switch. A quick way of finding and renaming files.

find . -name "*.html" -exec rename .html .htm {} \;

Find is regarded as a power command by many Linux users. The above example supports the claim ! There are many seemingly complicated or time consuming tasks which can be done quickly with commands.

Tuesday, July 11, 2006

Linux : Counting files per directory with Bash command

With commands, you can do many intricate things. Following command which is a small script, counts the number of .htm files in each subdirectory upto depth 2 of the current working directory.

for x in `find . -type d -maxdepth 2`; do y=`find $x -name "*.htm" | wc -l`; if [ $y != 0 ]; then echo $x - $y; fi done

This command is useful for webmasters who spend some time once in a while to figure out number of files in each of the website's subdirectories.

Notes
  1. The value of -maxdepth may be changed as needed
  2. The above command uses current directory (find . -type ...). The dot may be replaced by path of a directory of which the stats is needed.
  3. "*.htm" part of the filename may also be replaced with whatever you want

Thursday, July 06, 2006

Perl : Obfuscation of ARGV

Did you know ?

$a=eval shift @{$x.=chr for qw * 65 82 71 86 * ;$x};

is just another way of saying

$a = shift @ARGV;

Same thing can be done in many different ways. And some ways are intended at making it hard for humans to grasp the code ! Read more about Code obfuscation.