Thursday, October 4, 2012

How to take screenshot from video in php


Here i am going to explain you how to take screenshot(thumbnail) from video in php.

First Of All we need to installed ffmpeg on our server.If your server does not have this facility please contact your web hosting provider.

function captureImageFromVideo($imagename,$videoname)
 {

$ffmpeg = '/usr/bin/ffmpeg';

//video dir
$video = '/public_html/video/'.$videoname;


//where to save the image
$image = '/public_html/image/'.$imagename;

//what time to take screenshot from video, here it will take screenshot after 5 second from it being started.
$interval = 5;

//screenshot size
$size = '290x191';

//ffmpeg command
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";

//PHP built in function execute an external program    
exec($cmd);

}

We can also use ffmpeg to convert videos from one format to another.
For example if you uploading video which have .wmv extension. suppose if i want to convert this video to .mp4 format you can easily achieve this using ffmpeg.
We can use this ffmpeg command for this task
$cmd=”$ffmpeg -i $videofile -vcodec libx264 -acodec libfaac test.mp4“;

The installed ffmpeg should support the libx264 and libfaac.

I hope it will help some of you guys !
Read More »

Wednesday, October 3, 2012

How to create zip and download


In this post I am going to explain you how to create zip file and download.
We need some files on some directory which we will zipped by code.
For this task we are using PHP zip library, it must enabled on your server.
Here is function
function zipFilesDownload($file_names,$archive_file_name,$file_path)

{

    $zip = new ZipArchive();

    //create the file and throw the error if unsuccessful

    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {

                    exit("cannot open <$archive_file_name>\n");

    }

    //add each files of $file_name array to archive

    foreach($file_names as $files)

    {

                    $zip->addFile($file_path.$files,$files);

                    //echo $file_path.$files,$files."<br />";

    }

    $zip->close();

    //then send the headers to force download the zip file

    header("Content-type: application/zip");

    header("Content-Disposition: attachment; filename=$archive_file_name");

    header("Pragma: no-cache");

    header("Expires: 0");

    readfile("$archive_file_name");

    exit;

}
There are three parameters in function  $file_names, $archive_file_name and $file_path
$file_names =>  where you have to passed files in array.
$archive_file_name => It is the name of your zip file which is created by PHP library if it’s not open.
$file_path =>  It is your directory path where your files are placed which you want to zipped.
Read More »

Thursday, September 6, 2012

MySQL Using SUM and CASE in statement


Problems i faced in mysql query

one day i was working on Race project where director can payment online and manually .
I had been stored all Amount in one column "amt".
take a look on below image

MySQL Using SUM and CASE in statement

I was want SUM of manual payment , electronic payment and total of all payment in one query.  That day i had been write following query by using Mysql SUM() function and conditional CASE statement.
Here we need to use conditional CASE statement within a SUM() That's it.
And the query is

SELECT  SUM(CASE WHEN `pay_type`="manual" THEN `amt` ELSE 0 END) AS manual_amt,SUM(CASE WHEN `pay_type`="electronic" THEN `amt` ELSE 0 END) AS electronic_amt,SUM(`amt`) AS total_amt FROM pay

In cakephp you can sum like below
 $condition = array('fields' => array('Message.read_status AS read_status','SUM(CASE WHEN read_status=0 THEN \'1\' ELSE \'0\' END) AS MsgCnt'));

Perhaps it can happen with you so use my query and enjoy! .

Read More »

Tuesday, August 7, 2012

How to install ffmpeg in xampp

Install ffmpeg for windows

Step 1
Install xampp, use latest version. After installation there should be a directory C:\xampp
In this example i use C:\xampp as default directory.

Step 2
Check if gd2 is installed, you can do this by using phpinfo() in you script.

Step 3
Download ffmpeg.exe from one of the following links:
http://ffdshow.faireal.net/mirror/ffmpeg/
http://arrozcru.no-ip.org/ffmpeg_builds
http://ffdshow.faireal.net/mirror/ffmpeg/
http://www.paehl.com/open_source/?Convert_Tools:FFMPEG
http://oss.netfarm.it/mplayer-win32.php

Step 4
Unpack the downloaded zip or 7z file, three files should show up namely ffmpeg.exe, ffplay.exe, pthreadGC2.dll .

Step 5
Download the ffmpeg windows dll files:
http://azzerti.free.fr/php_ffmpeg_win32.zip

Step 6
Unpack the downloaded zip file, five dlls should show up namely php_ffmpeg_20050123.dll, php_ffmpeg_20050212.dll, php_ffmpeg_20050618.dll and also avcodec.dll and avformat.dll

Step 7
Copy 1 of 3 dlls which you've unpacked in step 6 into C:\xampp\php\extensions. Rename the dll to php_ffmpeg.dll

Step 8
Open your php.ini file which you find in C:\xampp\php .Add the following line to the list with dlls:
extension=php_ffmpeg.dll
Check also if extension=php_gd2.dll is uncommented. If not then remove the ;

Step 9
Copy the avcodec.dll and avformat.dll which you've unpacked in step 6 to C:\WINDOWS\system32

Step 10
Copy pthreadGC2.dll which was unpacked in step 4 to C:\WINDOWS\system32

Step 11
Restart xampp

Simple Example
Step 12
Create a directory in C:\xampp\htdocs for example www.test.dev\www
C:\xampp\htdocs\www.test.dev\www

Step 13
Open the httpd.conf file, this one you find in C:\xampp\apache\conf
Add under the line
NameVirtualHost localhost:80

the next data
<VirtualHost localhost:80>
ServerName www.test.dev
DocumentRoot C:/xampp/htdocs/www.test.dev/www
</VirtualHost>

Step 14
Find the hosts file on your computer, somewhere in the windows directory and add the following line:
127.0.0.1 www.test.dev

Step 15
Restart xampp

Step 16
Make a php file in the DocumentRoot (C:\xampp\htdocs\www.test.dev\www) for example test.php

Step 17
Add a flv file to the DocumentRoot. In this example i call it wattan.flv.

Step 18
Copy ffmpeg.exe which you've unpacked in step 4 into the DocumentRoot.(C:\xampp\htdocs\www.test.dev\www)

Step 19
Add the following code to the test.php file:
<?php
$ffmpegpath = "ffmpeg.exe";
$input = 'wattan.flv';
$output = 'wattan.jpg';

if (make_jpg($input, $output)){
echo 'success';
}else{
echo 'bah!';
}

function make_jpg($input, $output, $fromdurasec="01") {
global $ffmpegpath;

if(!file_exists($input)) return false;
$command = "$ffmpegpath -i $input -an -ss 00:00:$fromdurasec -r 1 -vframes 1 -f mjpeg -y $output";

@exec( $command, $ret );
if(!file_exists($output)) return false;
if(filesize($output)==0) return false;
return true;
}
?>

Step 19
Open the url http:\\www.test.dev\test.php in firefox.

Step 20
enjoy!
Read More »

Monday, July 30, 2012

How to find second highest salary in MySQL


if you have MySQL table like below image


if you want to fetch second highest salary of employee following query will be useful for you.
select sal from emp group by sal order by sal desc Limit 1,1

your output will be like below image


if you want to fetch all employee which have second highest salary following query will be useful for you.
select * from emp where sal=(select sal from emp group by sal order by sal desc Limit 1,1)

your output will be like below image


One more query
SELECT * FROM  `users` WHERE  `id` = ( SELECT MAX( id ) -1 FROM  `users` )

SELECT * FROM  `users` WHERE  `id` < ( SELECT MAX( id ) -1 FROM  `users` ) ORDER BY id DESC LIMIT 1


Read More »

Sunday, July 22, 2012

How to delete all inside folder

This function is useful to delete all inner folder of given path of folder.
Lets explain in example:
if you have folder which name is user_1  and user_1 have lots of folder like (my_album, my_profile,friends_photo etc.)
if you want to delete folder (my_albummy_profile,friends_photo etc.) call function like belew
<?php
deleteDirectory($dir); // $dir = path of user_1 folder
?>


function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}

cheers!
Read More »

Saturday, July 21, 2012

Date after 10 days


$your_date = '2012-07-21'; $date_after_ten_days = date("Y-m-d",strtotime(date("Y-m-d", strtotime($your_date)) . " +10 day")); echo $date_after_ten_days; //output 2012-07-31
Read More »

Tuesday, May 8, 2012

Javascript function

function emailCheck() { var email_race_0 = document.getElementById("email_race_0").value; var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(email_race_0) == false) { alert('Invalid Email Address'); return false; } }

function isAlphaOrParen(str) { return /^[a-zA-Z()]+$/.test(str); }

function emty_value_space(str_value) { if(str_value=='' || !str_value.match(/[^\s]/)) {return false;}else{return true;} }

function isNumeric(v) { return v.length > 0 && !isNaN(v) && v.search(/[A-Z]|[#]/ig) == -1; };

function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] + '/' + bits[1] + '/' + bits[0]); return !!(d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0])); }

//Make clone of same form
var id = document.getElementById('cnt_edit').value; //default zero
var id = id +1;
document.getElementById('cnt_edit').value = id;
var firstform=document.getElementById('firstform').innerHTML;
firstform=firstform.replace(/tik_0/g,"tik_"+id);
$("#new_form").append($(firstform).clone(true));
note : use first form element id like this <input type="text" name="reg[tik_0][title]" id="title_tik_0"  />
//End

How to use
if(!isNumeric(donation_amt.value)) { alert("Please Enter Number"); donation_amt.focus(); return false; }

if(!isAlphaOrParen(individual_last_name.value)) { alert("Please Enter Only Alphabates."); individual_last_name.focus(); return false; }

if(!emty_value_space(individual_last_name.value)) { alert("Please Enter Fundraising Last Name"); individual_last_name.focus(); return false; }

var start_date = jQuery('#start_date').val(); if(!isValidDate(start_date.toString())) { alert("Please enter start date in dd/mm/yyyy format"); jQuery('#start_date').focus(); return false; }

Check whether element is exist in document or not

function isElementInDocument(el) {
    var html = document.body.parentNode;
    while (el) {
        if (el === html) {
            return true;
        }
        el = el.parentNode;
    }
    return false;

}
How to use
var liobj = document.getElementById('sort_1');
alert(isElementInDocument(liobj));

check Price format
var cost = 2.5;
var costRegex = /^\d{0,9}(\.\d{0,2})?$/;

if (!(costRegex.test(cost))) {
        $('#add_store_product_error').html("Please enter cost in correct format ex: '123.55'");
        return false;
}


Read More »