Perl SHA digest

Problem

You want to generate a SHA digest for a given string.



Solution

Perl has great encryption and digesting algorithms.

In the example tab we use the Digest Perl module.



Example



perl -M'Digest::SHA1 qw(sha1_hex)'
-e '$dig=sha1_hex("my big test"); print "$dign";'

419e6139a21f51a3f2ea1a783cfe536a0dada873



Reference

[tags]Perl SHA Digest, Perl, SHA, Encryption, Perl Coding School[/tags]



pinging host measured in micro seconds

Problem

You have a requirement to capture network speed, in microseconds.

The required speed was less than 0.25 of a second (or quarter of second).



Solution

We use PHP to capture the current time in microseconds. Run a system ping command. Capture the new time and minus one from the other.



Example


Here's the PHP code, using the microtime function and a simple ping.

<?php

list($usec, $sec)=
explode(" ",microtime());

$thisDate=sprintf("%f", ($usec+$sec));

$host="$argv[1]";

system("ping -c1 $host > /dev/null 2>&1",$response);

if($response==0) {

list($usec, $sec)=explode(" ",microtime());

$nextDate=sprintf("%f", ($usec+$sec));

printf("%0.4fn",$nextDate-$thisDate);

} else {

print("ping failed to host: $hostn");

}

?>

Here is a run through:

$ php -q pingHost.php bagend
0.0098
$ php -q pingHost.php bagend
0.0066
$ php -q pingHost.php bagend
0.0065
$ php -q pingHost.php bagend
0.0066





Useful PHP script – running queries against sybase database

Problem

you want to run queries against a Sybase Database, using the PHP API.



Solution

A PHP script using sybase API for PHP, connect as given user, with your password on specific host.



Example


Useful for monitoring sybase dbs, although in this instance it does not actually hit any database tables.

<?php

$user = "your_user";
$passwd = "password";
$host = "your_host";
$sql="select getdate(), @@version ";
$conn=sybase_connect($host,$user,$pass); // connect to db
print("running query host: $hostnuser: $usern");
$result = sybase_query ( $sql ); // Run query and store in array
$i = 0;
while($row = sybase_fetch_array($result)) {
// Loop around the array and print field or column 1 and 2
$i++;
$item1 = $row[0];
$item2 = $row[1];
echo "$i $item1 $item2";
}
sybase_close();
?>



Reference

[tags]Sybase PHP example, PHP Coding School[/tags]



display an image with a label superimposed

Problem

You want to generate an image, with text over the top.



Solution

Why of course we use PHP extensive image library routines! 🙂



Example


<?php

header(“Content-type: image/png”);
$string = $_GET[‘label’];

if(isset($_GET[‘font’])) { $font = $_GET[‘font’]; } else { $font=“2″; }
$len=strlen($string);
$text_width = imagefontwidth($font);
$text_height = imagefontwidth($font);
/* Create a blank image */
$im = imagecreate (($len*$text_width)+10, $text_height+10);
$bgc = imagecolorallocate ($im, 0, 0, 0);
$tc = imagecolorallocate ($im, 255, 255, 255);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
imagestring ($im, $font, 5, 0, “$string”, $tc);
imagepng($im);
imagedestroy($im);

?>



Reference

I’ve now created an online tool, using this code Label Your Images

[tags]Image Creation, Label Image, PHP Coding School[/tags]



PHP and AJAX tool to transform text

Problem

You want to transform text form ascii to base64, urlencode it, digest it, etc.

Or you want to decode these formats.



Solution

Just wrote this tool, to allow instant transformation of data into following formats:

base64 encode decode, urlencode decode, md5 and sha1 hashing, addslashes and htmlentities

click here to access the transform tool
Pretty cool to see ‘all’ too – check it out.



Example


Here is the code:


<?php

if(isset($_GET['mode'])) {

$mode=urlencode($_GET['mode']);

$val=$_GET['val'];

switch($mode) {

case "base64_encode": echo base64_encode($val); break;
case "base64_decode": echo base64_decode($val); break;
case "urldecode": echo urldecode($val); break;
case "urlencode": echo urlencode($val); break;
case "htmlentities": echo htmlentities($val); break;
case "rot13": echo str_rot13($val);break;
case "md5": echo md5($val);break;
case "sha1": echo sha1($val);break;
case "all":

echo "base64_encode: ".base64_encode($val);
echo "base64_decode: ".base64_decode($val);
echo "urldecode: ".urldecode($val);
echo "urlencode: ".urlencode($val);
echo "htmlentities: ".htmlentities($val);
echo "addslashes: ".addslashes($val);
echo "rot13: ".str_rot13($val);
echo "md5: ".md5($val);
echo "sha1: ".sha1($val);
break;

}

exit(0);

}

?>





making a thumbnail image on the fly

Problem

You need to make a thumbnail of an image, on the fly.



Solution

Here is a short PHP script to make a thumbnail out of the image.

It takes two arguments, one for the image’s file name and one for the compression rate.



Example


displayThumb.php
<?php

$filename = $_GET['filename'];
$compression = $_GET['compression'];

Header("Content-type: image/png");

$im = imagecreatefrompng("$filename");

$currWidth=ImageSX($im);
$currHeight=ImageSY($im);

$newWidth=$currWidth * ($compression / 100);
$newHeight=$currHeight * ($compression / 100);

$im1 = imagecreatetruecolor($newWidth,$newHeight);
$bgc = imagecolorallocate ($im1,255,255,255);
imagefilledrectangle ($im1, 0, 0, $newWidth, $newHeight, $bgc);
imagecopyresampled ($im1, $im, 0, 0, 0, 0,
$newWidth, $newHeight, $currWidth, $currHeight);
imagepng($im1);
imagedestroy($im1);

?>



Reference

[tags]Image manipulation, thumbnail creation on the fly, PHP Coding School[/tags]



converting a GIF to a PNG with PHP

Problem

You need to convert a GIF into a PNG.



Solution

Another short bit of PHP, which takes a GIF filename as a parameter and a compression tag,
so you can convert to PNG and change size too.

If you don’t want to compress the image, just use a value of 100.



Example


convertGifPng.php

<?php

$filename = $argv[1];
$compression = $argv[2];

Header("Content-type: image/png");

$im = imagecreatefromgif("$filename");

$currWidth=ImageSX($im);
$currHeight=ImageSY($im);

$newWidth=$currWidth * ($compression / 100);
$newHeight=$currHeight * ($compression / 100);

$im1 = imagecreatetruecolor($newWidth,$newHeight);
$bgc = imagecolorallocate ($im1,255,255,255);
imagefilledrectangle ($im1, 0, 0, $newWidth, $newHeight, $bgc);
imagecopyresampled ($im1, $im, 0, 0, 0, 0,
$newWidth, $newHeight, $currWidth, $currHeight);
imagepng($im1);
imagedestroy($im1);

?>



Reference

[tags]Convert Images GIF PNG, PHP Coding School[/tags]