Search:

Categories



Binary to Text

<? 
function bin2text($bin_str) 
{ 
    $text_str = ''; 
    $chars = explode("\n", chunk_split(str_replace("\n", '', $bin_str), 8)); 
    $_I = count($chars); 
    for($i = 0; $i < $_I; $text_str .= chr(bindec($chars[$i])), $i  ); 
    return $text_str; 
} 
 
function text2bin($txt_str) 
{ 
    $len = strlen($txt_str); 
    $bin = ''; 
    for($i = 0; $i < $len; $i  ) 
    { 
        $bin .= strlen(decbin(ord($txt_str[$i]))) < 8 ? str_pad(decbin(ord($txt_str[$i])), 8, 0, STR_PAD_LEFT) : decbin(ord($txt_str[$i])); 
    } 
    return $bin; 
} 
print text2bin('How are you gentlements?'); 
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Generate HTML Calendar of a month of year

Generate HTML Calendar of a month of year

 

<?php 
 
/** 
* calendar class : 
* @package 
* @access public 
**/ 
class calendar{ 
    /** 
     * Constructor 
     * @access protected 
     */ 
    function calendar(){ 
 
    } 
 
    /** 
     * calendar::html_month_calendar() 
     * 
     * @param integer $m        The month value, could be returned with $m = date("m"); 
     * @return 
     **/ 
    function html_month_calendar($m = "",$y = ""){      
        if (trim($m) == "") { 
            $m = date($m); 
        } 
        if (trim($y)=="") { 
            $y = date("Y"); 
        } 
        $curr_month = date("m"); 
        $d = date("d"); 
        if ($m==1) { 
            $m_1 = 12; 
            $y_1 = $y-1; 
        } else { 
            $m_1 = $m-1; 
            $y_1 = $y; 
        } 
        $to = mktime(0,0,0,$m_1,0,$y_1); 
        $days_in_month = strftime ("%d",$to); 
        $from = mktime(0,0,0,$m,1,$y); 
 
        $from_a = getdate($from); 
        $to_a = getdate($to); 
        $calendar = "\n<table bgcolor=white cellpadding=2 cellspacing=0> 
<tr> 
    <th>Dim</th> 
    <th>Lun</th> 
    <th>Mar</th> 
    <th>Mer</th> 
    <th>Jeu</th> 
    <th>Ven</th> 
    <th>Sam</th> 
</tr>\n"; 
 
        $Days = 0; 
 
        for($i = 1; $i <= 6; $i++){ 
            $calendar .= "<tr>\n"; 
            for($j = 0; $j <= 6 ; $j++){ 
                if ($d == ($Days+1) and $curr_month==$m) { 
                    $b1 = "<b>";$b2 = "</b>"; 
                } else $b1 = "";$b2 = ""; 
 
                if ($from_a["wday"]==$j and $Days==0) { 
                    $Days++; 
                    $calendar .= "<td>$b1 $Days $b2</td>"; 
                } elseif($Days==0) { 
                    $calendar .= "<td></td>"; 
                } else { 
                    $Days++; 
                    if ($Days<=$days_in_month) { 
                        $calendar .= "<td>$b1 $Days $b2</td>"; 
                    } else { 
                        $calendar .= "<td></td>"; 
                    } 
                } 
            } // for 
            $calendar .= "</tr>\n"; 
        } // for 
        $calendar .= "</table><center>\n"; 
 
        return $calendar; 
    } 
 
    /** 
     * 
     * @access public 
     * @return void 
     **/ 
    function html_year_calendar($y = ""){ 
        if (trim($y)=="") { 
            $y = date("Y"); 
        } 
 
        $year_calendar = "<table bgcolor=white cellpadding=2 cellspacing=0 width=100% border=1>\n<tr>\n"; 
 
        for($i = 1; $i <= 12; $i++){ 
            $year_calendar .= "<td valign=top>Mois $i <br>".$this->html_month_calendar($i,$y )."</td>"; 
            if ($i == 3 or $i == 6 or $i == 9) { 
                $year_calendar .= "</tr>\n<tr>\n"; 
            } 
        } // for 
 
        $year_calendar .= "</tr><table>"; 
 
        return $year_calendar; 
    } 
 
 
} 
 
$cal = new calendar; 
//echo $cal->html_month_calendar(); 
echo $cal->html_year_calendar(); 
 
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Alternate Row Colours

<? 
 
// Header rows 
print ("<body bgcolor=black text=white>"); 
print ("<table border=1 width=100%> 
<tr> 
    <td width=18%><font face=Verdana, Arial, Helvetica, sans-serif size=2><b>Name</b></font></td> 
    <td width=82%><font face=Verdana, Arial, Helvetica, sans-serif   
size=2><b>Email</b></font></td>"); 
 
// DB connection information 
$connection = mysql_connect("localhost","username","password"); 
$db = mysql_select_db("database", $connection); 
 
// SQL Query 
$sql = mysql_query("SELECT * FROM table"); 
 
$alternate = "2"; // number of alternating rows 
while ($row = mysql_fetch_array($sql)) { 
$name = $row["name"]; //name of field 
$email = $row["email"]; 
 
if ($alternate == "1") { 
$colour = "#000000"; 
$alternate = "2"; 
} 
 
else { 
$colour = "#464646"; 
$alternate = "1"; 
} 
 
print ("</tr><tr><td bgcolor=$colour width=18%><font face=Verdana, Arial, Helvetica,   
sans-serif size=2>$name</font></td><td bgcolor=$colour width=82%> 
<font face=Verdana, Arial, Helvetica, sans-serif size=2>$email</font></td> 
</tr>"); 
} 
print ("</table>"); 
 
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Create a lighter shade of a hex color:

Create a lighter shade of a hex color

 

<? 
function hexLighter($hex,$factor = 30) 
    { 
    $new_hex = ''; 
 
    $base['R'] = hexdec($hex{0}.$hex{1}); 
    $base['G'] = hexdec($hex{2}.$hex{3}); 
    $base['B'] = hexdec($hex{4}.$hex{5}); 
 
    foreach ($base as $k => $v) 
        { 
        $amount = 255 - $v; 
        $amount = $amount / 100; 
        $amount = round($amount * $factor); 
        $new_decimal = $v + $amount; 
 
        $new_hex_component = dechex($new_decimal); 
        if(strlen($new_hex_component) < 2) 
            { $new_hex_component = "0".$new_hex_component; } 
        $new_hex .= $new_hex_component; 
        } 
 
    return $new_hex;     
    } 
?> 
 
Example, making a new colour 50% lighter than the first: 
<? 
$myCol = "FFCC99"; 
$newCol = hexLighter($myCol, 50); 
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Thumbnail function

<?php 
 
 
function make_thumb($imagefile, $new_w=75, $new_h=75) 
{ 
// get source image info.
$img_data=getimagesize($imagefile); 
$src_w=$img_data[0]; 
$src_h=$img_data[1]; 
 
// get source image
$src_img=ImageCreateFromPNG($imagefile) or die ("Cannot open source"); 
 
// create a target image
$dst_img=imagecreate($new_w,$new_h); 
 
// create the thumbnail
imagecopyresized($dst_img,$src_img, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src 
_h); 
 
// switch to subdir.
chdir("thumbnails");
 
// if the image already exists blow it away.
if ( file_exists("thumb_".$imagefile) ) 
{ 
unlink("thumb_".$imagefile); 
} 
 
// I use thumb_ to show that it is a thumbnail. 
// save the new image as a png.
imagepng($dst_img,"thumb_".$imagefile); 
 
// free up the memory.
ImageDestroy($src_img); 
ImageDestroy($dst_img); 
 
} 
 
// demo of use.
make_thumb("image1.png"); 
 
make_thumb("image1.png", 200, 200);
 
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Write Text to the Image

<?php
 
//=========== change the text to display in the image
//$text = 'mydomain.com';
//$imgfile="smp.jpg";
$font = 'arial.ttf';
$ext=substr($imgfile,-3);
$ext=strtolower($ext);
 
if($ext=="jpg" || $ext=="jpe") $im=@imagecreatefromjpeg("$imgfile");
elseif ($ext=="gif") $im=@imagecreatefromgif("$imgfile"); 
else {print "Unknown image format"; exit;}
 
 if (!$im) { /* See if it failed */
       $im = ImageCreate (150, 30); /* Create a blank image */
       $bgc = ImageColorAllocate ($im, 255, 255, 255);
       $tc  = ImageColorAllocate ($im, 0, 0, 0);
       ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); 
       /* Output an errmsg */
       ImageString($im, 1, 5, 5, "Error loading $imgfile", $tc); 
	return $im;
   }
 
$x=imagesx($im);
$y=imagesy($im);
$fontsize=$x/20;
$fontsize=floor($fontsize);
if($fontsize<10) $fontsize=10;
 
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
 
imagettftext($im, $fontsize, 0, 12, $fontsize+8, $black, $font, $text);
imagettftext($im, $fontsize, 0, 10, $fontsize+6, $white, $font, $text);
 
if($ext=="gif") 
	{
	header("Content-type: image/gif");
	imagegif($im);
	}
else
	{
	header("Content-type: image/jpeg");
	imagejpeg($im);
 
	}
imagedetroy($im);
?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare