Wednesday, October 31, 2012

Delete duplicate rows in mysql and php

How to delete duplicate records in table?
For example :
user_new table - total rows - 45000
duplicate rows - 5150

I have used below this code.It works fine.cheers!!!

<?php 
ini_set("max_execution_time","18800");
$con = mysql_connect("localhost","root","root");
mysql_select_db("testdb",$con);

$query = "SELECT email,count(email),group_concat(user_id) as uid FROM `user_new` group by email having count(email) > 1  ";

$result = mysql_query($query);
$total_rows = mysql_num_rows($result);
$del_ids = array();

if($total_rows>0)
{
  while($row=mysql_fetch_object($result))
  {
    $uid = $row->uid;
$aUid = explode(",",$uid);
array_shift($aUid);
 $rids = array_merge($del_ids,$aUid);
 $del_ids =  $rids;
   }
 //   print_r($del_ids);
   $Sdel_ids = implode(",",$del_ids);
   $del_query = "DELETE FROM user_new where user_id in ($Sdel_ids) ";
   echo $del_query;
   mysql_query($del_query);
}

?>

Tuesday, October 23, 2012

Image re-size exactly in php

Simply two steps to re-size the image as your given dimensions.

Step 1:
 // *** Include the class
include("resize_class.php");
// *** 1) Initialise / load image
$resizeObj = new resize('images/cars/large/original.jpg');
// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj -> resizeImage(230 , 108, 'exact');
// *** 3) Save image
$resizeObj -> saveImage('images/cars/small/resizeimage.jpg', 100);

Step 2:
Save this content as resize_class.php.


Class resize
{
// *** Class variables
    private $image;
   private $width;
   private $height;
    private $imageResized;

function __construct($fileName)
{
    // *** Open up the file
    $this->image = $this->openImage($fileName);

   // *** Get width and height
   $this->width  = imagesx($this->image);
   $this->height = imagesy($this->image);
}

private function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));

switch($extension)
{
case '.jpg':
case '.jpeg':
$img = @imagecreatefromjpeg($file);
break;
case '.gif':
$img = @imagecreatefromgif($file);
break;
case '.png':
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}

public function resizeImage($newWidth, $newHeight, $option="auto")
{
// *** Get optimal width and height - based on $option
$optionArray = $this->getDimensions($newWidth, $newHeight, $option);

$optimalWidth  = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];


// *** Resample - create image canvas of x, y size
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);


// *** if option is 'crop', then crop too
if ($option == 'crop') {
$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
}
}

private function getDimensions($newWidth, $newHeight, $option)
{

  switch ($option)
{
case 'exact':
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
break;
case 'portrait':
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
break;
case 'landscape':
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
break;
case 'auto':
$optionArray = $this->getSizeByAuto($newWidth, $newHeight);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
break;
case 'crop':
$optionArray = $this->getOptimalCrop($newWidth, $newHeight);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
break;
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}

private function getSizeByFixedHeight($newHeight)
{
$ratio = $this->width / $this->height;
$newWidth = $newHeight * $ratio;
return $newWidth;
}

private function getSizeByFixedWidth($newWidth)
{
$ratio = $this->height / $this->width;
$newHeight = $newWidth * $ratio;
return $newHeight;
}

private function getSizeByAuto($newWidth, $newHeight)
{
if ($this->height < $this->width)
// *** Image to be resized is wider (landscape)
{
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
}
elseif ($this->height > $this->width)
// *** Image to be resized is taller (portrait)
{
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
}
else
// *** Image to be resizerd is a square
{
if ($newHeight < $newWidth) {
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
} else if ($newHeight > $newWidth) {
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
} else {
// *** Sqaure being resized to a square
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
}
}

return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
     
         private function getOptimalCrop($newWidth, $newHeight)
{

$heightRatio = $this->height / $newHeight;
$widthRatio  = $this->width /  $newWidth;

if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}

$optimalHeight = $this->height / $optimalRatio;
$optimalWidth  = $this->width  / $optimalRatio;

return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}


      private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
{
// *** Find center - this will be used for the crop
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );

$crop = $this->imageResized;
//imagedestroy($this->imageResized);

// *** Now crop from center to exact requested size
$this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
}

public function saveImage($savePath, $imageQuality="100")
{
// *** Get extension
        $extension = strrchr($savePath, '.');
        $extension = strtolower($extension);

switch($extension)
{
case '.jpg':
case '.jpeg':
if (imagetypes() & IMG_JPG) {
imagejpeg($this->imageResized, $savePath, $imageQuality);
}
break;

case '.gif':
if (imagetypes() & IMG_GIF) {
imagegif($this->imageResized, $savePath);
}
break;

case '.png':
// *** Scale quality from 0-100 to 0-9
$scaleQuality = round(($imageQuality/100) * 9);

// *** Invert quality setting as 0 is best, not 9
$invertScaleQuality = 9 - $scaleQuality;

if (imagetypes() & IMG_PNG) {
imagepng($this->imageResized, $savePath, $invertScaleQuality);
}
break;

// ... etc

default:
// *** No extension - No save.
break;
}

imagedestroy($this->imageResized);
}
}

Friday, October 19, 2012

Remove special characters from file name

       If you use special characters filename

            $f_name = "filename"; // given filename

            $f_name = trim($f_name,".");  // trim the first or last character "."

            $afilename=explode(".",$f_name);  // explode filename using separator  "."

            $f_ext = end($afilename);  // end keyword used to return the array end of value

            $f_name = reset($afilename); //  reset keyword used to return the array start of value

            $filename = $f_name.".".$f_ext;

            $str    = $filename;

            $sp_chr = array("[,]","[']","[=]","[;]");

            $str = preg_replace($sp_chr,"-",$str);   // using preg replace to replace the . , = ;

            $str = preg_replace("/&/","and",$str);  // replace & to and





           // below steps are replace the . or * or ? or / <  > change to -.

            $str = str_replace(".","-",$str); 
            $str = str_replace(":","-",$str);
            $str = str_replace("*","-",$str);
            $str = str_replace("/","-",$str);
            $str = str_replace("<","-",$str);
            $str = str_replace(">","-",$str);
            $str = str_replace("\"","-",$str);

            $filename = $str;




Thursday, October 18, 2012

Ogone payment integration in php

Ogone payment simply done in two steps.

 Step 1:        
 $PSPID = 'YOURPSPID'; //'OGONETEST';
 $Price = "100"; // PAYPAL
 $passphrase = 'YOURSECRETKEY';
 $unique_id = RAND(890000,895689596);
 $pm = "CREDIT CARD";
$accept_url="http://yourreturnurl.com"; //Like IPN
$cancelurl="http://yourcancelurl.com";

//If you add any parameters in  Ogone_sha1 order by Alphabet order.
 $Ogone_sha1 =  
                        "ACCEPTURL=".$accept_url.$passphrase.
                        "AMOUNT=".$credit.$passphrase.
                         "BGCOLOR=#010000".$passphrase.
                         "CANCELURL=".$cancelurl.$passphrase.
                         "CURRENCY=EUR".$passphrase.
                         "LANGUAGE=en_us".$passphrase.
                         "ORDERID=".$unique_id.$passphrase.
                         "PM=".$pm.$passphrase.
                         "PSPID=".$PSPID.$passphrase.
                         "TITLE=Payment via Ogone".$passphrase;
//Creating the signature
   $Ogone_sha1 = sha1($Ogone_sha1);

// For test use the url in action https://secure.ogone.com/ncol/test/orderstandard.asp

 $form1 = '<form name="directpayment1" id="directpayment" action="https://secure.ogone.com/ncol/prod/orderstandard.asp" method="post" >
      <input name="PSPID" type="hidden" value="'.$PSPID.'" />
      <input name="AMOUNT" type="hidden" value="'.$credit.'" />
      <input name="ACCEPTURL" type="hidden" value="'.$accept_url.'" />
      <input name="CANCELURL" type="hidden" value="'.$cancelurl.'" />
      <input name="ORDERID" type="hidden" value="'.$unique_id.'" />
      <input name="CURRENCY" type="hidden" value="EUR" />
      <input name="LANGUAGE" type="hidden" value="en_us" />
      <input name="PM"  type="hidden" value="'.$pm.'">
      <input name="TITLE"  type="hidden" value="Payment via Ogone">
      <input name="BGCOLOR"  type="hidden" value="#010000">
      <input name="SHASIGN" type="hidden" value="'.$Ogone_sha1.'" />    
      </form><script>document.getElementById("directpayment").submit()</script>';   
      echo $form1;

Step 2:

If the ogone payment is done it calls the accept url.

$pay_status= $_REQUEST['STATUS'];
//Status 9 is success for PAYPAL and 5 is authorized for CREDIT CARD
$pay_id=$_REQUEST['PAYID'];
//Like Transaction id
$pay_types=$_REQUEST['BRAND'];
$pay_ipaddress=$_REQUEST['IP'];
$paytype =$_REQUEST['PM'];
$amount=$_REQUEST['AMOUNT'];
$invoiceNumber = $_REQUEST['ORDERID'];

Just all the values are get from request like PAYID , ORDERID , STATUS values. Thats all. simply ogone payment is implemented.
Cheers!!!!

Create thumbnails for mp4 using ffmpeg

First of all note this point. If you use this code in your local server use ffmpeg for windows exe file.otherwise use linux server user use linux version.

//In your ffmpeg.exe file path
$ffmpeg_path = 'ffmpeg';

//In your video file
$video_path = 'video_old.mp4';

// Image destination path
$image = "video_old".time().'.jpg';

// default time to get the image
$second = 1;

// get the duration and a random place within that
$comd = "$ffmpeg_path -i $video_path -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";

shell_exec($comd);


Note: not need to given path is ffmpeg.exe.only given ffmpeg.

Thursday, October 04, 2012

PayPal Recurring Payments


When you use PayPal Subscriptions and Recurring Payments, your customers can purchase automatically recurring subscriptions from your website, or even using a link in an email!
Subscriptions and Recurring Payments is a low-cost way for you to accept credit card and bank account payments for content site subscriptions, newsletter fees, club dues, or recurring donations, and can be fully integrated with your website in a few easy steps. Subscriptions and Recurring Payments is only available for Business or Premier accounts.


What are the benefits?

Save time and money with PayPal's hassle-free Subscriptions and Recurring Payments:
  • Easy to implement - flexible and automatic billing frees you from sending invoices
  • No up-front costs - you'll have the same low fee schedule used when you receive other PayPal payments
  • Sell with ease - PayPal maintains detailed transaction records on our website
  • Improve buyer experience - with customizable buttons and secure payments, happy customers become repeat customers

How to implement:

This is simple. You just need to add three  hidden fields listed below.

<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1"> 
<input type="hidden" name="srt" value="1">

src

Recurring payments. Subscription payments recur unless subscribers cancel their subscriptions before the end of the current billing cycle or you limit the number of times that payments recur with the value that you specify for srt.
Allowable values are:
  • 0 – subscription payments do not recur
  • 1 – subscription payments recur
The default is 0.

srt
Optional
Recurring times. Number of times that subscription payments recur. Specify an integer with a minimum value of 1 and a maximum value of 52. Valid only if you specify src="1".

sra

Reattempt on failure. If a recurring payment fails, PayPal attempts to collect the payment two more times before canceling the subscription.
Allowable values are:
  • 0 – do not reattempt failed recurring payments
  • 1 – reattempt failed recurring payments before canceling
The default is 1.