The model view controller pattern is the most used pattern for today’s world web applications. It has been used for the first time in Smalltalk and then adopted and popularized by Java. At present there are more than a dozen PHP web frameworks based on MVC pattern.
Despite the fact that the MVC pattern is very popular in PHP, is hard to find a proper tutorial accompanied by a simple source code example. That is the purpose of this tutorial.
The MVC pattern separates an application in 3 modules:
Model, View and Controller:
The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.
The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla,
The controller handles the model and view layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output.
Tuesday, November 26, 2013
Example to add watermark to an image
example to add watermark to an image:
here stamp.png is real image and photo.jpeg would be the generated image havng watermark.<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Function to generate random code
Here is a sample function that generates random code,it can be used for generating encryption codes or passwords etc.
<?php
function gen_code($total_length = 8, $use_caps = true, $use_numeric = true, $use_specials = true) {
$capsall = array();
$nums = array();
$nums_specials = 0;
$reglength = $total_length;
$tws = array();
for ($ch = 97; $ch <= 122; $ch++) $chars[] = $ch; // create a-z
if ($use_caps) for ($ca = 65; $ca <= 90; $ca++) $capsall[] = $ca; // create A-Z
if ($use_numeric) for ($nu = 48; $nu <= 57; $nu++) $nums[] = $nu; // create 0-9
$all = array_merge($chars, $capsall, $nums);
if ($use_specials) {
$reglength = ceil($total_length*0.75);
$nums_specials = $total_length - $reglength;
if ($nums_specials > 5) $nums_specials = 5;
for ($si = 33; $si <= 47; $si++) $signs[] = $si;
$rs_keys = array_rand($signs, $nums_specials);
foreach ($rs_keys as $rs) {
$tws[] = chr($signs[$rs]);
}
}
$rand_keys = array_rand($all, $reglength);
foreach ($rand_keys as $rand) {
$tw[] = chr($all[$rand]);
}
$compl_str = array_merge($tw, $tws);
shuffle($compl_str);
return implode('', $compl_str);
}
echo gen_code(10);
?>
<?php
function gen_code($total_length = 8, $use_caps = true, $use_numeric = true, $use_specials = true) {
$capsall = array();
$nums = array();
$nums_specials = 0;
$reglength = $total_length;
$tws = array();
for ($ch = 97; $ch <= 122; $ch++) $chars[] = $ch; // create a-z
if ($use_caps) for ($ca = 65; $ca <= 90; $ca++) $capsall[] = $ca; // create A-Z
if ($use_numeric) for ($nu = 48; $nu <= 57; $nu++) $nums[] = $nu; // create 0-9
$all = array_merge($chars, $capsall, $nums);
if ($use_specials) {
$reglength = ceil($total_length*0.75);
$nums_specials = $total_length - $reglength;
if ($nums_specials > 5) $nums_specials = 5;
for ($si = 33; $si <= 47; $si++) $signs[] = $si;
$rs_keys = array_rand($signs, $nums_specials);
foreach ($rs_keys as $rs) {
$tws[] = chr($signs[$rs]);
}
}
$rand_keys = array_rand($all, $reglength);
foreach ($rand_keys as $rand) {
$tw[] = chr($all[$rand]);
}
$compl_str = array_merge($tw, $tws);
shuffle($compl_str);
return implode('', $compl_str);
}
echo gen_code(10);
?>
Subscribe to:
Comments (Atom)