Skip to content

Instantly share code, notes, and snippets.

View achudars's full-sized avatar
🥔
every challenge is an opportunity

Aleks achudars

🥔
every challenge is an opportunity
View GitHub Profile
@achudars
achudars / web-snippet-021 ( Clearing floated objects using CSS and HTML )
Last active December 17, 2015 00:19
Clearing floated objects using CSS and HTML
<div style="overflow:hidden; width:100%">
<div style="float:left">
...
</div>
</div>
@achudars
achudars / web-snippet-020 ( Parsing GET and POST in Node.js )
Last active December 17, 2015 00:19
Parsing GET and POST in Node.js
// Parsing GET
// parse URL
var url_parts = url.parse(req.url);
// parse query
var raw = querystring.parse(url_parts.query);
// some juggling e.g. for data from jQuery ajax() calls.
var data = raw ? raw : {};
data = raw.data ? JSON.parse(raw.data) : data;
@achudars
achudars / web-snippet-019 ( Database Connection in PHP )
Last active December 17, 2015 00:19
Database Connection in PHP
<?php
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();
$dbHost = "localhost"; //Location Of Database usually its localhost
$dbUser = "xxxx"; //Database User Name
$dbPass = "xxxx"; //Database Password
$dbDatabase = "xxxx"; //Database Name
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
@achudars
achudars / web-snippet-018 ( dominant colour of an image using PHP )
Last active December 17, 2015 00:19
Determine the dominant colour of an image using PHP
$i = imagecreatefromjpeg("image.jpg");
for ($x=0;$x<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$y++) {
$rgb = imagecolorat($i,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
$b = $rgb & 0xFF;
$rTotal += $r;
@achudars
achudars / web-snippet-017 ( Use Gravatars in Your Application using PHP )
Last active December 17, 2015 00:19
Use Gravatars in Your Application using PHP
/******************
*@email - Email address to show gravatar for
*@size - size of gravatar
*@default - URL of default gravatar to use
*@rating - rating of Gravatar(G, PG, R, X)
*/
function show_gravatar($email, $size, $default, $rating)
{
echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).
@achudars
achudars / web-snippet-016 ( Get Real IP Address of Client )
Last active December 17, 2015 00:19
Get Real IP Address of Client (even if he is behind a proxy server) using PHP
function getRealIpAddr()
{
if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
@achudars
achudars / web-snippet-015 ( Encode Email Address using PHP )
Last active December 17, 2015 00:19
Encode Email Address using PHP
function encode_email($email='[email protected]', $linkText='Contact Us', $attrs ='class="emailencoder"' )
{
// remplazar aroba y puntos
$email = str_replace('@', '&#64;', $email);
$email = str_replace('.', '&#46;', $email);
$email = str_split($email, 5);
$linkText = str_replace('@', '&#64;', $linkText);
$linkText = str_replace('.', '&#46;', $linkText);
$linkText = str_split($linkText, 5);
@achudars
achudars / web-snippet-014 ( Watermark images automatically using PHP )
Last active December 17, 2015 00:19
Watermark images automatically using PHP
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'arial.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
@achudars
achudars / web-snippet-013 ( Creates image data URIs using PHP )
Last active December 17, 2015 00:19
Creates image data URIs using PHP
// A few settings
$image = 'cricci.jpg';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;
// Echo out a sample image
@achudars
achudars / web-snippet-012 ( Use of regular expressions in HTML5 )
Last active December 17, 2015 00:19
Use of regular expressions in HTML5
<form action="" method="post">
<label for="username">Create a Username: </label>
<input type="text"
name="username"
id="username"
placeholder="4 <> 10"
pattern="[A-Za-z]{4,10}"
autofocus
required>
<button type="submit">Go </button>