Friday 27 July 2012

Increase Memory Limit to resolve Server error while generating PDF using third party library like TCPDF

When generating a PDF using TCPDF or any of the third party PDF libraries, and if you find the following issues while generating pdf;

------------------------------------------------------------------------------
Server error The website encountered an error while retrieving http://admincentre.flubstage.info/fcc/reporting/download_pdf_chain_report/01. It may be down for maintenance or configured incorrectly. Here are some suggestions: Reload this web page later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.
------------------------------------------------------------------------------

Solution: In that case, most probably, it would be memory limitation issue. So, you should increase the memory limit by adding the following code before the pdf generate code(the very beginning of the code).
/* Set the memory limit for the PDF. */
ini_set('memory_limit', '256M'); 

Tuesday 24 July 2012

Calculating the Standard Deviation using PHP

// Calculating the Standard Deviation.
private function standard_deviation($aValues, $bSample = false)
{
 $fMean = array_sum($aValues) / count($aValues);
 $fVariance = 0.0;
 foreach ($aValues as $i)
 {
  $fVariance += pow($i - $fMean, 2);
 }
 $fVariance /= ( $bSample ? count($aValues) - 1 : count($aValues) );
 return (float) sqrt($fVariance);
}

Friday 20 July 2012

Resize the image in various sizes using PHP

This is the mail resizing image function which actually resized the image based on provided parameter.
/**
 * This will resize the image into specific image sizes.
 * @param array $data:  'old_image_path' = full path of the old image which to be reszied;
 *  'new_image_path' = full path the new image to be created;
 *  'width' = New Width of the image;
 *  'height' = New Height of the image;
 *  (just for orignal) 'copy_original' = set 'true' if you like to just copy original image and you don't need to provide above 'width' and 'height', otherwise just ignore this.;
 *   
 * @return void;
 */
private function resizing_image($data)
{
 // Set copy_original=true, if you like to create original image.
 $create_original = isset($data['copy_original']) && ($data['copy_original']) ? true : false;
 
 //Set target path
 $target_path = $data['new_image_path'];
  
 //Get the width and height of upload image
 list($width, $height) = getimagesize($data['old_image_path']);
 
 if ($create_original) 
 {
  $bigwidth = $width;
  $bigheight = $height;
 }
 else 
 {
  $n_wight = $data['width'];
  $n_height = $data['height'];
  
  if ($width > $n_wight) {
   //If the width is greater than our maximum width preference, use the maximum width
   $newwidth = $n_wight;
   //Set new ratios for width and height
   $newheight = ($newwidth/$width) * $height;
   $bigwidth = $newwidth;
   $bigheight = $newheight;
  } else {
   //If width is less than our maximum, use that value (so as not to pixelate the image)
   $bigwidth = $width;
   $bigheight = $height;
  }
   
  if ($bigheight > $n_height) {
   //If the height is greater than our maximum height preference, use the maximum height
   $newheight = $n_height;
   //Set new ratios for width and height
   $newwidth = ($newheight/$bigheight) * $bigwidth;
   $bigwidth = $newwidth;
   $bigheight = $newheight;
  }
 }
 
 //Initiate image magick class
 $im = new Imagick();
 //Get image
 $im->readImage( $data['old_image_path'] );
 //Create thumbnail image
 $im->thumbnailImage( $bigwidth, $bigheight );
 //Write to file
 $im->writeImage( $target_path );
 /*
 echo '

Original Image Size: Width-'. $width. ' Height:-'.$height;
 echo '
New Image Size: Width-'. $bigwidth. ' Height:-'.$bigheight;
 echo '
'.$target_path;
 */
 //Destroy temp
 $im->destroy();
}

This function is to resize image in various sizes
// This function is to resize image in various sizes
private function resize_image($product_id, $old_image_path)
{
 define('PATH_RESIZED_PRODUCT_IMAGE', "/home/admin/assets/products/");
 $new_path = PATH_RESIZED_PRODUCT_IMAGE; //"";
  
 $product_id = 23;
 $old_image_path = 'http://ecx.images-amazon.com/images/I/81RXp-hIaNL._AA1500_.jpg';
 //$old_image_path = 'http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png';
 
 /** Get File Extension. */
 $path_info = pathinfo($old_image_path);
 $ext = $path_info['extension'];
 
 // Create Thumbnail (max 40px by 40px)
 $new_image_path = $new_path.'t_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";) 
 $data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>40, 'height'=>40);
 $this->resizing_image($data);
 
 // Create Standard (max 310px by 235px)
 $new_image_path = $new_path.'s_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
 $data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>310, 'height'=>235);
 $this->resizing_image($data);
 
 // Large (max 500px by 500px)
 $new_image_path = $new_path.'l_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
 $data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>500, 'height'=>500);
 $this->resizing_image($data);
 
 // For Copying Original Image.
 $new_image_path = $new_path.'orig_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
 $data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'copy_original'=>true);
 $this->resizing_image($data);
 
 echo 'All sizes: 
- t_'.$product_id.'.'.$ext.' = Thumbnail (max 40px by 40px); 
- s_'.$product_id.'.'.$ext.' = Standard (max 310px by 235px); 
     
- l_'.$product_id.'.'.$ext.' = Large (max 500px by 500px); and 
- orig_'.$product_id.'.'.$ext.' = Copying Original Image, 
 are copied into '.PATH_RESIZED_PRODUCT_IMAGE.' path.';
 die;
 
}

Call the above function to create different sizes image;
// Call the above function to create different sizes image;
$product_id = 18;
$old_image_path= 'http://ecx.images-amazon.com/images/I/81RXp-hIaNL._AA1500_.jpg';
resize_image($product_id, $old_image_path);

Thursday 19 July 2012

Validation of URL (Website Address) and Email using PHP

//Validation of URL (Website Address) and Email using PHP
$post = $_POST;

// Validation of URL or Website Address
if (!isset($post['api_user']) || empty($post['api_user']))
 $errors[] = 'Enter Website name';  
else if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $post['website']))
 $errors[] = ''.$post['website'].' website name is not valid.';

//Validation of Email Address
if (!isset($post['paypal_address']) || empty($post['paypal_address']))
 $errors[] = 'Enter valid Paypal Email Address.';
else if (!(filter_var($post['paypal_address'], FILTER_VALIDATE_EMAIL)))
 $errors[] = 'Enter valid PayPal email address.';

Monday 9 July 2012

Usage of preg_replace, search and replace {variable}


Regular Expression: Usage of preg_replace, search and replace {variable}, {variable_one} and join{string_var_able}.
$string = "This {variable} will be {variable_with_underscore}  and {variable}with will be replaced";
$pattern = array('/($|\{(\w+))\}/'); 
$replace = array('...'); 
$string = preg_replace($pattern, $replace, $string);