Friday, February 26, 2010

Browser Compatibility Check for Internet Explorer Versions from 5.5 to 8

IETester is a free WebBrowser that allows you to have the rendering and javascript engines of IE8, IE7 IE 6 and IE5.5 on Windows 7, Vista and XP, as well as the installed IE in the same process.

link: http://www.my-debugbar.com/wiki/IETester/HomePage

Tuesday, February 23, 2010

PHP, MySql - Check table existance | table exist

This function is to check whether the required table exist or not

code:
function TableExists($tablename, $db) {

// Get a list of tables contained within the database.
$result = mysql_list_tables($db);
$rcount = mysql_num_rows($result);

// Check each in list for a match.
for ($i=0;$i<$rcount;$i++) {
if (mysql_tablename($result, $i)==$tablename) return true;
}
return false;
}

Wednesday, February 17, 2010

PHP - Image upload resizing script | Image resize by script

Process an image after upload.

code:

function process_image($image, $folder, $prefix = "", $newwidth = 800, $newheight = NULL,
$allow = array("image/jpeg", "image/png", "image/gif"), $sizelimit = 2000000) {

global $error;

if (!is_dir($folder)) {
mkdir($folder, 0777);
}

if (in_array($image["type"], $allow)) {
if ($image["size"] < $sizelimit) {
$uploadedfile = $image['tmp_name'];
switch($image["type"]) {
case "image/jpeg":
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
case "image/png":
$src = imagecreatefrompng($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagepng($tmp,$filename);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
case "image/gif":
$src = imagecreatefromgif($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagegif($tmp,$filename);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
}
} else {
$error = "File size to big.";
return false;
}
} else {
$error = "File type not allow.";
return false;
}
}


note: $image is an array of the image uploaded (from $_FILES)

PHP - Send email with attachment | Email script

I've tested this in gmail, yahoo, and hotmail. So far, it worked fine.

code:


function send_email($data, $subject, $content, $attachment = NULL) {

$name_from = $data["name_from"]; // Who the email is from
$email_from = $data["email_from"]; // Who the email is from
$email_to = $data["email_to"]; // Who the email is to
$email_cc = $data["email_cc"]; // Who the email is cc
$email_bcc = $data["email_bcc"]; // Who the email is bcc
$fileatt = $attachment["tmp_name"]; // Path to the file
$fileatt_type = $attachment["type"]; // File Type
$fileatt_name = $attachment["name"]; // Filename that will be used for the file as the attachment
$email_subject = $subject; // The Subject of the email
$email_message = $content; // Message that the email has in it

$semi_rand = md5(time());
$mime_boundary = $semi_rand;

$headers = "From: $name_from<$email_from>\r\n";
$headers .= ($email_bcc != "")? "Bcc: ". $email_bcc ."\r\n" : "";
$headers .= ($email_cc != "")? "Cc: ". $email_cc ."\r\n" : "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$mime_boundary."\r\n";

// This two steps to help avoid spam
//$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
//$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

if (!is_null($attachment)) {
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
} else {
$email_message .= "--{$mime_boundary}--\n";
}
$ok = mail($email_to, $email_subject, $email_message, $headers);
return ($ok)? true : false;
}


note: $data and $attachment are arrays