Image Resize script php

php-sniplets No Comments »

Image Resize

You will be able to use this function to generate html “width/height” for use with showing a thumbnail image
using the original image. This will help save in making duplicate images merely for display as a thumbnail.

It takes only two arguments, the path to where the image is stored and the wanted width or height.

/** resize_image()
* This functions resizes an image to any size
* defined.
*                         – $image: This is the path to the image to be resized
*                         – $target: This is the target width/height
*                            – htm width/height string for an image tag
function resize_image($image,$target)
// Get the actual image size
$image = getimagesize($image);

// Get the percent to size the image
if ($image[0] > $image[1])
$percent = $target / $image[0];
$percent = $target / $image[1];

// Get the new sizet
$w = round($image[0] * $percent);
$h = round($image[1] * $percent);

// Generate as an htm widht/height property
$htmWH = “width=\”" . $w . “\” height=\”" . $h . “\”";
return $htmWH;

This way won’t decrease download time if you’re resizing a huge image. This is just telling the web browser to
show the image at a certain size when the image is downloaded.

php Password generator script

php-sniplets No Comments »

Really Simple Password generator

This function produces comparatively secure random passwords. It is by no intends ideal, just it ought play
innermost noncrucial situations. The good matter is the generator efforts to make passwords that anyone can
suppose and selects letters that will not be incorrect for other people (specified the number “1″, an capital letter
“i” and a little “L”). To maintain the code short some of this practicality is really basic, but it is better than naught.

* Renders somewhat more human-intelligible random passwords
* @param int $len
* @return string
function makePassword($len = 8)
$vowel = array(‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘y’);
$confusing = array(‘I’, ‘l’, ’1′, ‘O’, ’0′);
$replacement = array(‘A’, ‘k’, ’3′, ‘U’, ’9′);
$choice = array(0 => rand(0, 1), 1 => rand(0, 1), 2 => rand(0, 2));
$part = array(0 => ”, 1 => ”, 2 => ”);

if ($choice[0]) $part[0] = rand(1, rand(9,99));
if ($choice[1]) $part[2] = rand(1, rand(9,99));

$len -= (strlen($part[0]) + strlen($parts[2]));
for ($i = 0; $i < $len; $i++)
if ($i % 2 == 0) $part[1] .= chr(rand(97, 122));
else $part[1] .= $vowel[array_rand($vowel)];
if ($choice[2]) $part[1] = ucfirst($part[1]);
if ($choice[2] == 2) $part[1] = strrev($part[1]);

$r = $part[0] . $part[1] . $part[2];
$r = str_replace($confusing, $replacements, $r);
return $r;

* Model:

echo makePassword(12);

php Random Password Generator

php-sniplets No Comments »

Random Password Generator (based on word list)

This is a random password generator that creates understandable passwords supported word lists. I have just
enclosed a three entry world list since you had better selected a list based on your password requisites and your
users. Whenever you want to get passwords that are fourteen characters long, you’ll require different list than
if you are getting in eight character passwords. And dependant on your users, you may prefer to apply certain
lists. The list I use is almost 4000 words that are 5-7 characters long, all known words that have accepted
potentially offensive content removed. For security grounds I do not want to let in this list.

A mention on security: although this returns relatively solid passwords for the ordinary user, they’re particularly
subject to brute-force attempts. This is even more a matter if someway your word list finds compromised. I’d
not recommend applying this function for anything where an extremely secure password is required.

A mention on selecting your list: you will as well see that I have improved the system to avoid returning
passwords with zeros and ones in them. This is because zero and capital “o” can be mixed-up equally can one,
lowercase “L” and uppercase “i.” When selecting my word list I was as well certain to clean away all words that
begin on the letter “i” or “o” (to keep the optional ucfirst() from making 0 / O and 1 / I mix-up) and words that
carry the letter “L” (to keep l/1 mix-up). I feel that this greatly helps on preventing mix-up, but once more
breaks the protection of the passwords a few. It is your option.

function generatePassword($maxLen = 8)
$words = array(‘alaska’, ‘aline’, ‘alone’,); // Use your personal list here!!!

while (strlen($word) > $maxLen || !$word) $word = $words[array_rand($words, 1)];
if (rand(0,1)) $word = ucfirst($word);
for ($i = 0; $i < $maxLen – strlen($word); $i++) rand(0,1) ? $opener .= rand(2,9) : $closer .=

return “{$opener}{$word}{$closer}”;

for ($i = 0; $i < 20; $i++)
echo generatePassword() . ” “;


Produces: 6Aline43 79Aline7 aline4 2Aline85 58alaska5 59Aline3 4aline Aline3 6alaska44 3Alone Aline587 Aline994
8Aline56 Aline259 699aline 4Aline88 alone3 alone3 Alaska322 267Alaska

use of php mail function

php-sniplets No Comments »

PHP supplies a function named mail that sends email from your script.

The format is as follows: mail(headers,address,message,subject);

These are the values you need to fill in:

address: The e-mail address that receives the message
subject: A string that goes on the subject line of the e-mail message
message: The content that comes in the e-mail message
headers: A string that sets values for e-mail headers

You may set and send an email message as follows:

$to = “”;
$subj = “Testing”;
$mess = “Testing the mail function”;
$headers =\r\n
$mailsend = mail($to,$subj,$mess,$headers);

The message is sent to the address in the $to variable.

You are able to send the message to more than one individual by using the following argument:

$to= “,”;

The $headers string in this case as well sends a blind copy of the message to You will
be able to let in more than one header as follows:

$header = “\r\”;

Headers are optional. Just the first 3 parameters are needed.

The $mailsend variable holds TRUE or FALSE. However, TRUE is no guarantee that the mail will get to wherever
it is going. It just implies that it started out O.K..

Validate an email address with php

php-sniplets No Comments »

This little function with regex is used for Email Address Verification – short and simple, but powerfull, i use it really often. Validating the proper format of an email address is handy, so your users cant type in invalid adresses and you dont have databases full of wrong email adresses.

Email addresses always have a standard format. We can check those with regular expressions:

function validateEmail($email) {
if (!preg_match(“/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/” , $email)) {
return false;
return true;

You can now check your input data:

$email = $_POST['email'];
if (validateEmail($email)) {
} else {
echo ‘false’;

Impressum Entries RSS Log in