Friday, March 5, 2010

Image watermark with PHP

Steps:

1. Load both images
2. Get size of both images
3. Copy watermark to main image
4. Print image to screen



PHP functions:
imagecreatefromgif
imagecreatefromjpeg
getimagesize
imagecopymerge
header
imagejpeg
imagedestroy

Watermark image:



Main image:




<?php  

$main_img = "Happy_valentines_day.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark

$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic

if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");


$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];

$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;


// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);


// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>


Check password safety with JavaScript while typing

<script language="javascript">

function check_password_safety(pwd){

var msg = "";
var points = pwd.length;
var password_info = document.getElementById('password_info');

var has_letter = new RegExp("[a-z]");
var has_caps = new RegExp("[A-Z]");
var has_numbers = new RegExp("[0-9]");
var has_symbols = new RegExp("\\W");

if(has_letter.test(pwd)) { points += 4; }
if(has_caps.test(pwd)) { points += 4; }
if(has_numbers.test(pwd)) { points += 4; }
if(has_symbols.test(pwd)) { points += 4; }


if( points >= 24 ) {
msg = 'Your password is strong!';
} else if( points >= 16 ) {
msg = 'Your password is medium!';
} else if( points >= 12 ) {
msg = 'Your password is weak!';
} else {
msg = 'Your password is very weak!';
}

password_info.innerHTML = msg ;
}


</script>

<input type="text" name="pwd" id="pwd" size="20" onkeyup="return check_password_safety(this.value);" />

Type your password!