Monday, July 21, 2008

AJAX Username Availability checking

Put this javascript within your "Head" tag.


the form control should like this,

call the CheckUsername() function inside the username control in onchange event,
onChange='CheckUsername(this.value);'
create one "div" tag put inside that tag
id='usernameresult'

create one check.php file, it should consists,

mysql_connect("localhost","root","1234");
mysql_select_db("database_name");

if(isset($_GET['username'])){ // ElseIf they are want to check their username.

$user=stripslashes(strip_tags(htmlspecialchars($_GET['username'], ENT_QUOTES))); // Cleans all nasty input from the username.
$check=mysql_num_rows(mysql_query("SELECT * FROM `registration` WHERE `Usern` = '".$user."'")); // Checks to see if a user is in the `users` table or not.

if($check == 0){ // If there is no username in the database that matches the input one.
echo ''.$user.' is Available!'; // Yay we can use it.
}elseif($check != 0){ // ElseIf there is a username in the database.
echo ''.$user.' is Not Available!'; // None for you higgans.
} // End If.

Image re sizing on the fly - PHP

Hi,
you can re size your uploading image using php script given below:


Class ImageResizeUpload {

function ImageResizeUpload($upload_folder,$thumb_max_height,$thumb_max_width,$quality,$file_max_size,$allowed_extensions) {
$this->upload_path = $upload_folder;
$this->thumb_max_height = $thumb_max_height;
$this->thumb_max_width = $thumb_max_width;
$this->quality = $quality;
$this->file_max_size = $file_max_size;
$this->allowed_extensions = $allowed_extensions;
$this->rand_image_tag = rand(10000,99999);
}

function fileExtension($filename) {
return end(explode(".",$filename));
}

function isValidFormat() {
foreach(explode("|",$this->allowed_extensions) as $value) {
if($this->fileExtension()==$value) { //We have a match, it is valid!
$valid_extension="true";
break;
}
}
if(isset($valid_extension) && $valid_extension=="true") { return true; } else { return false; }
}

function isValidSize($tmp_filename) {
if(filesize($this->tmp_filename)<=($this->file_max_size*1024)) {
return true;
} else {
return false;
}
}

function fileName($filename,$type, $id) {
if($type==0) { return $id."_".$filename; } else { return $id."_".$filename; }
}

function calculateThumbSize($tmp_filename) {
$image_size = getimagesize($tmp_filename);
//check to see if we even need to resize it
if($image_size[0] > $this->thumb_max_width || $image_size[0] > $this->thumb_max_width) {
//We do, so lets do it!
if($image_size[0] > $image_size[1]) {
//width is greater than height so make width our max width and height proportional
$width = $this->thumb_max_width;
$height = ($image_size[1] * $width) / $image_size[0];
} else {
//height is greater than width
$height = $this->thumb_max_height;
$width = ($image_size[0] * $height) / $image_size[1];
}
} else {
//No need to resize, just return same h/w
$width = $image_size[0];
$height = $image_size[1];
}
return array("width"=>$width,"height"=>$height);
}

function resizeCopyImage($filename,$tmp_filename, $id) {
//echo $id;
$thumb_size = $this->calculateThumbSize($tmp_filename);
//switch ($this->fileExtension($filename)) {
// case "jpg" || "jpeg":
$file_extension = $this->fileExtension($filename);
$file_extension = strtolower($file_extension);
if($file_extension == 'jpg')
$src_img = imagecreatefromjpeg($tmp_filename);
elseif($file_extension == 'gif')
$src_img = imagecreatefromgif($tmp_filename);
elseif($file_extension == 'png')
$src_img = imagecreatefrompng($tmp_filename);
elseif($file_extension == 'bmp')
$src_img = imagecreatefromwbmp($tmp_filename);

$dest_img = imagecreatetruecolor($thumb_size['width'],$thumb_size['height']);
imagecopyresampled($dest_img,$src_img,0,0,0,0,$thumb_size['width'],$thumb_size['height'],imagesx($src_img),imagesy($src_img));
$new_thumb_filename = $this->upload_path."/".$this->fileName($filename,1, $id);
@chmod($new_thumb_filename,0777);

if($file_extension == 'jpg')
imagejpeg($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'gif')
imagegif($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'png')
imagepng($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'bmp')
imageWBMP($dest_img,$new_thumb_filename,$this->quality);

return true;
}

function moveUploadedFile($filename,$tmp_filename) {
$new_filename = $this->upload_path."\\".$this->fileName($filename,0);
move_uploaded_file($tmp_filename,$new_filename);
// @chmod($new_filename,0755);
return true;
}

}


$path="../photo";
$image=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$rand=rand(1, 1000000);
$upload_image = new ImageResizeUpload($path,100,100,100,800,"jpg|jpeg|png");
$upload_image->resizeCopyImage($image,$tmp,$rand);
$image= $rand. '_' . $image;

I think this code will help you.