Thursday 31 May 2018

Image Resize and save by using GD function and Percentage based

<?php
$filename = 'test.jpg';
$percent = .5;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$ourFileName = "newtest.jpg";
imagejpeg($thumb, $ourFileName);
imagedestroy($thumb);
?>

Wednesday 23 May 2018

How to create custom log in cakephp?

1. Add bootstrap for this in below

CakeLog::config('customlog', array('engine' => 'File'));

2. Create log message in custom logfile

CakeLog::write('customlog', 'myArray');

Check now you log directory once created

Friday 18 May 2018

Mysql dump in terminal mode

For taking Dump : mysqldump -u username -p test> test>

Then Enter Password if prompts for password


For Restoring Dump :  mysql -u username -p test> < test>

Then Enter Password if prompts for password

Now I have restored the dump. So no need to take dump and restore.

scp -r <username>@<domainname/ip address>:/<file path>  <destination path>


For Windows
mysql.exe -uroot -p dbname < dump
you need to do this in xampp/mysql/bin/ path

Compress & Extract Linux commands

tar -zcvf archive.tar.gz directory/    => compressing folder
tar -zxvf archive.tar.gz                   => extracting the compressed file

PNG to Jpeg in PHP using GD library


<?php
function png2jpg($originalFile, $outputFile, $quality) {
    $image = imagecreatefrompng($originalFile);
    imagejpeg($image, $outputFile, $quality);
    imagedestroy($image);
}
png2jpg("img/test.png", "img/".time().".jpg", 100);
if ($handle = opendir('img/')) {
	$i=0;
   while (false !== ($entry = readdir($handle))) {
      if ($entry != "." && $entry != "..") {
         echo "<img src='img/$entry' width=150 height=100>    ";
			if($i%10==0 && $i!=0){
				echo "<br>";
			}
			$i++;
      }
   }
   closedir($handle);
}
?>

Multple checkbox selected or not

<script type="text/javascript" src='jquery.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$('#extend_auto_expired').click(function(){
$("input[type=checkbox]:checked").each(function() {
alert( $(this).val() );
});
});
});
</script>
<center>
<form name='ck'>
<?php
for($r=0; $r<10;$r++){
echo "<input type='checkbox' name='t[]' value='$r'>$r<br>";
}
?>
<input type="button" id="extend_auto_expired" value="Extend auto expired" class="buttongreen" name="Submit">
</form>
<center>

How to find disk space in PHP?

<?php
$bytes = disk_total_space(".");
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
$base = 1024;
$class = min((int)log($bytes , $base) , count($si_prefix) - 1);
//echo $bytes . '<br />';
$total = sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class];

$bytes = disk_free_space(".");
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
$base = 1024;
$class = min((int)log($bytes , $base) , count($si_prefix) - 1);
//echo $bytes . '<br />';
$free = sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class];

   $used = (float)$total-(float)$free;
echo "Total Space : $total <br/> Available Space : $free <br/> Used Space : $used ".$si_prefix[$class];
?>

Difference between two dates in PHP

<?php
$date1 = "2015-01-23 00:20";
$date2 = "2015-01-23 05:30";
$seconds = strtotime($date2) - strtotime($date1);
$days    = floor($seconds / 86400);
$hours   = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
echo "$days Days, $hours Hrs $minutes mins $seconds sec";
?>

Difference between two dates in Javascript with demo


Enter values in mm/dd/yy hh:mm:ss format
First date and time
Second date and time
Result
In (decimal) hours
In (decimal) minutes
In seconds

Code:
<script type='text/javascript'>
function p (i){
return Math.floor(i / 10) + "" + i % 10;
}
function trunc (i){
var j = Math.round(i * 100);
return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}
function calculate (form){
var date1 = new Date(form.date1.value);
alert(form.date1.value+"\n\n"+date1)
var date2 = new Date(form.date2.value);
var sec = date2.getTime() - date1.getTime();
if (isNaN(sec)){
alert("Input data is incorrect!");
return;
}
if (sec < 0){
alert("The second date ocurred earlier than the first one!");
return;
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
form.result_h.value = trunc(sec / hour);
form.result_m.value = trunc(sec / minute);
form.result_s.value = trunc(sec / second);
var days = Math.floor(sec / day);
sec -= days * day;
var hours = Math.floor(sec / hour);
sec -= hours * hour;
var minutes = Math.floor(sec / minute);
sec -= minutes * minute;
var seconds = Math.floor(sec / second);
form.result.value = days + " day" + (days != 1 ? "s" : "") + ", " + hours + " hour" + (hours != 1 ? "s" : "") + ", " + minutes + " minute" + (minutes != 1 ? "s" : "") + ", " + seconds + " second" + (seconds != 1 ? "s" : "");
}
</script>
<form id="form">
<table cellpadding="3">
<tr>
<td colspan="2" align="center">Enter values in <font color="#800000">mm/dd/yy hh:mm:ss</font> format</td>
</tr>
<tr>
<td>First date and time</td>
<td><input type="text" name="date1" /></td>
</tr>
<tr>
<td>Second date and time</td>
<td><input type="text" name="date2" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" name="submit" value="Calculate" onclick="calculate(this.form)" /></td>
</tr>
<tr>
<td>Result</td>
<td colspan="2"><input type="text" name="result" readonly="readonly" size="40" /></td>
</tr>
<tr>
<td>In (decimal) hours</td>
<td colspan="2"><input type="text" name="result_h" readonly="readonly" /></td>
</tr>
<tr>
<td>In (decimal) minutes</td>
<td colspan="2"><input type="text" name="result_m" readonly="readonly" /></td>
</tr>
<tr>
<td>In seconds</td>
<td colspan="2"><input type="text" name="result_s" readonly="readonly" /></td>
</tr>
</table>

Difference between in Mysql & Mango DB


Cut - copy - paste


<script type='text/javascript' src='http://code.jquery.com/jquery-1.11.3.min.js'></script>
<script type='text/javascript'>
$(document).ready(function(){	
   $('#test').bind("cut copy paste",function(e) {	
       e.preventDefault();	
   });	
});	
</script>
<input type="text" id='test' placeholder="Hello Text"/>

Calculate Number of days Between two dates except saturday & sunday or week off in PHP script


<?php
$strt_date = date_create('2015-m-1');
$end_date = date_create(date('Y-m-d'));
date_sub($strt_date, date_interval_create_from_date_string('1 day'));
$interval = date_diff($strt_date, $end_date);
$num=$interval->format('%a');
$x=1;
for($i=0;$i<$num;$i++){
  date_add($strt_date, date_interval_create_from_date_string('1 day'));
  $next_day=date_format($strt_date, 'd-m-Y');
  echo "<br>";
  $new_date = new DateTime($next_day);
  $weeknum=$new_date->format('w');
  if(($weeknum!=0)&&($weeknum!=6)){
	 echo "$x. not a holiday :$weeknum  $next_day ";
	 $x++;
  }
}
?>