Category Archives: PHP

tips, tools, functions and general ideas relating to PHP

Directory Listing (the old way)

function list_dir($handle) {
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "$file\n";
       }
   }
   closedir($handle);
}
}


list_dir('images');

Filesize function

     function file_size($size)
     {
          $sizename = array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
          return ($size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . " " . $sizename[$i] : "0 Bytes");
     }

Sanitise input function

     //filters input array
     function ap_sanitize_input($input,$escape_mysql=false,$sanitize_html=false,$sanitize_special_chars=false,$allowable_tags=''){
          unset($input['submit']); //we use 'submit' variable for all of our form
                   
          $input_array = $input;
         
          //array is not referenced when passed into foreach
          //this is why we create another exact array
          foreach ($input as $key=>$value){
               if(!empty($value)){
                   
                    //stripslashes added by magic quotes
                    if(get_magic_quotes_gpc()){
                         $input_array[$key] = stripslashes($input_array[$key]);
                    }    
                   
                    if($sanitize_html){
                         $input_array[$key] = strip_tags($input_array[$key],$allowable_tags);
                    }
                   
                    if($sanitize_special_chars){
                         $input_array[$key] = htmlspecialchars($input_array[$key]);
                    }                   
                   
                    if($escape_mysql){
                         $input_array[$key] = mysql_real_escape_string($input_array[$key]);
                    }
               }
          }
         
          return $input_array;
    
     }

PHP Image change type

// Create a new image in the memory from the file 
$png_image = imagecreatefrompng( "input.png" );
// Save the image as output.jpg
imagejpeg( $png_image, "output.jpg" );
// Clear the memory of the tempory image 
imagedestroy( $png_image );

PHP Image crop

// Crop dimensions.
$width = 100;
$height = 100;
// Set the path to the image to resize
$input_image = "House.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Prepare canvas
$canvas = imagecreatetruecolor( $width, $height );
// Create a new image in the memory from the file 
$cropped = imagecreatefromjpeg( $input_image );
// Prepare image  crop - center the crop on the image
$newwidth = $size[0] / 2;
$newheight = $size[1] / 2;
$cropLeft = ( $newwidth/2 ) - ( $width/2 );
$cropHeight = ( $newheight/2 ) - ( $height/2 );
// Generate the cropped image
imagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, $size[0], $size[1], $newwidth, $newheight );
// Save the cropped image as cropped.jpg
imagejpeg( $canvas, "cropped.jpg" );
// Clear the memory of the tempory images
imagedestroy( $canvas );
imagedestroy( $cropped );

PHP Image rotate

// Set the path to the image to rotate
$input_image = "House.jpg";
//How many degrees you wish to rotate
$degrees = 180;
// Create the canvas
$canvas = imagecreatefromjpeg( $input_image ) ;
// Rotates the image
$rotate = imagerotate( $canvas, $degrees, 0 ) ;
// Save the image as output.jpg
imagejpeg( $rotate, "output.jpg" );
// Clear the memory of the tempory image
imagedestroy( $canvas );

PHP Image from watermark

// Create the canvas
$canvas = imagecreate( 200, 100 );  
// Define the colours to use
$black = imagecolorallocate( $canvas, 0, 0, 0 );  
$white = imagecolorallocate( $canvas, 255, 255, 255 );  
// Create a rectangle and fill it white
imagefilledrectangle( $canvas, 0, 0, 200, 100, $white );  
// The path to the font
$font = "verdana.ttf"; 
// The text to use 
$text = "House"; 
// The font size 
$size = "30";   
// Set the path to the image to watermark
$input_image = "House.jpg"; 
// Calculate the size of the text 
// If php has been setup without ttf support this will not work.
$box = imagettfbbox( $size, 0, $font, $text );  
$x = (200 - ($box[2] - $box[0])) / 2;  
$y = (100 - ($box[1] - $box[7])) / 2;  
$y -= $box[7];  
// Add the text to the image
imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );  
// Make white transparent
imagecolortransparent ( $canvas, $white );  
// Save the text image as temp.png
imagepng( $canvas, "temp.png" );  
// Cleanup the tempory image canvas.png
ImageDestroy( $canvas );  
// Read in the text watermark image
$watermark = imagecreatefrompng( "temp.png" );  
// Returns the width of the given image resource  
$watermark_width = imagesx( $watermark );
//Returns the height of the given image resource    
$watermark_height = imagesy( $watermark );    
$image = imagecreatetruecolor( $watermark_width, $watermark_height );    
$image = imagecreatefromjpeg( $input_image );
// Find the size of the original image and read it into an array      
$size = getimagesize( $input_image );  
// Set the positions of the watermark on the image
$dest_x = $size[0] - $watermark_width - 100;    
$dest_y = $size[1] - $watermark_height - 200;    
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);   
// Save the watermarked image as watermarked.jpg 
imagejpeg( $image, "watermarked.jpg" );
// Clear the memory of the tempory image     
imagedestroy( $image );    
imagedestroy( $watermark );    
// Delete the text watermark image
unlink( "temp.png");  

PHP Image from text

// Create the canvas
$canvas = imagecreate( 150, 80 );
// First colour - this will be the default colour for the canvas
$light_blue = imagecolorallocate( $canvas, 176, 226, 255 );
// The second colour - to be used for the text
$black = imagecolorallocate( $canvas, 0, 0, 0 );
// Path to the font you are going to use
$font = "verdana.ttf";
// Text to write
$text = "Text";
// Font size
$size = "40";
// Add the text to the canvas
imageTTFText( $canvas, $size, 0, 15, 60, $black, $font, $text );
// Save as Text.jpg
imagejpeg( $canvas, "Text.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $canvas );

PHP Image resize

// Set the path to the image to resize
$input_image = "House.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Set the new width of the image
$thumb_width = "200";
// Calculate the height of the new image to keep the aspect ratio
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
// Create a new true color image in the memory
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
// Create a new image from file 
$src_img = ImageCreateFromJPEG( $input_image );
// Create the resized image
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
// Save the image as resized.jpg
ImageJPEG( $thumbnail, "resized.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $thumbnail );

PHP URL parsing for multiple variables

OK, this one I did write myself.  It is used for building the URL link when filtering multiple columns..

Use is as the URL inside the ‘href’, passing the url var that you want to change, or remove.  (This would also be useful for pagination links..)

function buildQryReplace($field,$value){
    $gets = parse_url($_SERVER['REQUEST_URI']); // get URL
    parse_str($gets['query'],$getVarsArray);    // create GET vars as an array
    $getVarsArray[$field] = $value;             // overwrite the var that we want to change / add
    return '?'.http_build_query($getVarsArray); // build the array back with the new value
}
// ?name=Chris&age=50
// buildQryReplace('name','Joe');
// ?name=fff&age=50