Friday, August 29, 2008

GREAT SITE LINKS

http://www.geekpedia.com/

How to read XML using PHP DOM This tutorial will demonstrate the use of DOM extension in PHP to read contents of a XML file.

This tutorial is for PHP starters as well as for those who are using XML parser functions to read/write XML files. This tutorial will not go into details on XML. The focus will be more on the Document Object Model (DOM) extension.


This should not be confused with DOM XML extension, which is not packaged into PHP5 and will never be (according to php.net). DOM extension allows PHP users to use the DOM functions. The function then allow users to read, write, rename tags and do all types of crazy stuff with XML documents.


First we need a XML document to experiment with. I have created a simple XML document and named it test.xml

test.xml






Validate data

Validate data in SQL2005 Database with SQL manager






Download Music

Download latest music from site abc








Validate data

Validate data in SQL2005 Database with SQL manager



Download Music

Download latest music from site abc




This XML document stores notes on things to do. You can create one for your library, restaurant, news or book inventory. Every note in this XML
file beings with and ends with . Within the tab I have created a tab for the task name ( ) and task details (
).


Next step is to read this XML and display the results on a web page.


Create another file and call it test.php. Copy the following code into it and run it.


// DOMElement->getElementsByTagName() -- Gets elements by tagname
// nodeValue : The value of this node, depending on its type.
// Load XML File. You can use loadXML if you wish to load XML data from a string

$objDOM = new DOMDocument();
$objDOM->load("test.xml"); //make sure path is correct


$note = $objDOM->getElementsByTagName("note");
// for each note tag, parse the document and get values for
// tasks and details tag.

foreach( $note as $value )
{
$tasks = $value->getElementsByTagName("tasks");
$task = $tasks->item(0)->nodeValue;


$details = $value->getElementsByTagName("details");
$detail = $details->item(0)->nodeValue;

echo
"$task :: $detail
"
;
}


?>


The output on your webpage will be something like this:

Validate data :: Validate data in SQL2005 Database with SQL manager

Download Music :: Download latest music from site abc



I have documented some of the built-in functions in the code. For more details please refer to the php manual.


This tutorial is a kick start for beginners. You can retreive data from a database and create a XML document on-the-fly with DOM functions, rename tabs in an existing XML document and validate format.

Wednesday, August 20, 2008

Put watermark on images using PHP

If you are a professional or a newbie photographer you will probably want to protect your photos from being stolen and used for free. Using PHP you can create different types of watermarks on all of your images. I am going to show you a way how to dynamically put a text messages on your images. What you need is a JPG image file and a font file used for generate the watermark message. I am going to use arial font which you can also download.

Below you can find a watermarkImage() image function which takes 3 parameters ($SourceFile, $WaterMarkText, $DestinationFile) and creates an watermarked image from the source image specified. The first parameter - $SourceFile - is the full server path to the JPG image that you are going to watermark. The second one - $WaterMarkText - is the text message that you want to use for the watermark. And the last parameter - $DestinationFile - can either be blank or full server path to a new file which will be the source file with watermark text on it. What this function does is to read the source file, then create a new image object (using the imagecopyresampled() function). Then using the Arial.ttf font file and the imagettftext() function it writes the WaterMarkText onto the image. The last IF statement checks if it should save a new watermarked file or should just display it on the screen.


function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'arial.ttf';
$font_size = 24;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
}


You need to download the arial.ttf file and upload it on your server. Then create a new PHP file and copy and paste the above function in it. Next 4 lines are used to set the Source file, Watermark text message and Destination file. If you want to just display the watermarked image you need to leave the $DestinationFile variable empty ($DestinationFile=''; ). Also please make sure that for source file and destination file you include the full server path and the image file name. If you want to change the position of the watermark message on your images you can chage that line imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);

$SourceFile = '/home/user/www/images/image1.jpg';
$DestinationFile = '/home/user/www/images/image1-watermark.jpg';
$WaterMarkText = 'Copyright anupfound.blogspot.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
?>

Uploading image and creating thumnails

function UploadImage($temp_name, $name, $uploaddir)
{
$next_location = "../../uploaded_images/model/middle/";
if(is_uploaded_file($temp_name))
{
@move_uploaded_file($temp_name,$uploaddir.$name);
if(file_exists($uploaddir.$name))
$this->CreateThumb($name,$uploaddir,$next_location,100,100);
}
}

function CreateThumb($name1, $src_location, $dest_location, $new_w,$new_h)
{
$name = $src_location.$name1;
$filename = $dest_location.$name1;

$ext = explode(".",$name);
$count = count($ext);
$ext = $ext[$count-1];


switch($ext)
{
case "jpeg":
{
$src_img=imagecreatefromjpeg($name);
break;
}

case "jpg":
{
$src_img=imagecreatefromjpeg($name);
break;
}

case "gif":
{
$src_img=imagecreatefromgif($name);
break;
}
case "png":
{
$src_img=imagecreatefrompng($name);
break;
}
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$ratio = ($old_x < $old_y) ? $new_h/$old_y : $new_w/$old_x;

$thumb_w = $old_x * $ratio;
$thumb_h = $old_y * $ratio;
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

switch($ext)
{
case "jpeg":
{
imagejpeg($dst_img,$filename);
break;
}

case "jpg":
{
imagejpeg($dst_img,$filename);
break;
}

case "gif":
{
imagegif($dst_img,$filename);
break;
}
case "png":
{
imagepng($dst_img,$filename);
break;
}
}

imagedestroy($dst_img);
imagedestroy($src_img);
}


or you can use this function

function MakeIcon($dirname, $filename, $filetype,$maxw,$maxh){
list($width, $height) = getimagesize($dirname.$filename);
$percent = $width/$height;
$x_percent=$maxh/$height;
$y_percent=$maxw/$width;

if($width<=$maxw && $height<=$maxh){
$newheight=$maxh;
$newwidth=$maxw;
}else if(($x_percent*$height)<$maxh){
$newheight=ceil($x_percent*$height);
$newwidth=$maxw;
}else{
$newwidth=ceil($y_percent*$width);
$newheight=$maxh;
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
$iconame = "$filename";
switch($filetype){
case "image/jpeg":
$source = imagecreatefromjpeg($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagejpeg($thumb,$dirname.$iconame);
break;
case "image/gif":
$source = imagecreatefromgif($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagegif($thumb,$dirname.$iconame);
break;
case "image/pjpeg":
$source = imagecreatefromjpeg($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagejpeg($thumb,$dirname.$iconame);
break;
case "image/png":
$source = imagecreatefrompng($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagepng($thumb,$dirname.$iconame);
break;
}
//ini_set("memory_limit", "32MB");
return $iconame;
}

setting parameters now

$uploaddir = "./uploaded_images/event/";


$imgObj->UploadImage($temp_name, $name, $uploaddir);
$dest_location = "./uploaded_images/thumbs/";
if(file_exists($uploaddir.$name))
$imgObj->CreateThumb($name,$uploaddir,$dest_location,100,100);


and

$imgObj->UploadImage($temp_name, $name, $uploaddir,$_FILES['pic']['type'][$i]);
$dest_location = "./uploaded_images/thumbs/";
if(file_exists($uploaddir.$name))
$imgObj->CreateThumb($name,$uploaddir,$dest_location,150,100);