Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

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);
?>

Friday, 18 May 2018

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);
}
?>

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";
?>

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++;
  }
}
?>

Thursday, 27 November 2014

To find url given string and replace with anchor tag in php

<?php
$str = "Hi I am using google.com going http://google.com and 
where in https://google.com ahcinkg http://google.co.in else http://google.co";
$ss = "";
$sd = explode(" ",$str);
//print_r($sd);
$c=count($sd)."
"; $i=0; while($c>$i){ $regex = "((https?|ftp)\:\/\/)?"; // SCHEME $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass $regex .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP $regex .= "(\:[0-9]{2,5})?"; // Port $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor // $regexp = '/^[a-zA-Z0-9][a-zA-Z0-9\-\_]+[a-zA-Z0-9]$/'; if (true == preg_match("/^$regex$/", $sd[$i])) { if(!in_array("http", parse_url($sd[$i])) && !in_array("https", parse_url($sd[$i]))) { $s1 ="http://".$sd[$i]; echo $ss ="$sd[$i]  "; }elseif(in_array("http", parse_url($sd[$i])) || in_array("https", parse_url($sd[$i]))) { echo $ss ="$sd[$i]  "; } }else{ echo $sd[$i]."  "; } $i++; } ?>

Thursday, 20 November 2014

Save image from url using php

<?php
$url = "http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg";
$content = file_get_contents($url);
file_put_contents('flower.jpg', $content);
?>
<img src="flower.jpg">

Friday, 14 November 2014

PHP mail() Function with html content type

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

Tuesday, 14 October 2014

Thursday, 9 October 2014

Enter Floating Number only

Input field onkeyup check floating values only to allow
onkeyup="if (!isFinite(this.value)) this.value = this.value.replace(/(\s+)?.$/,'')"

How to send e-mail messages using PHP?

<?php
    mail("recipient@example.com",
        "This is the message subject",
        "This is the message body",
        "From: sender@example.com" . "\r\n" . "Content-Type: text/plain; charset=utf-8",
        "-fsender@example.com");
?>

How to protect your file content using htaccess?

# Prevent Apache from serving .htaccess files:
<FilesMatch "^\.htaccess">
    Order allow,deny
    Deny from all
</FilesMatch>

How to enable and disable the PHP magic quotes directive?

When the magic quotes directive is enabled, PHP automatically escapes 
data from HTTP GET and POST requests and cookie data. For example, 
if a user types “hello” (with the quotation marks) in a form, PHP 
automatically escapes the quotation marks and stores the value as 
\“hello\”. To enable this functionality, use a text editor to 
modify the magic quotes directive in the php.ini file as follows:

magic_quotes_gpc = on

To disable this functionality, modify the magic quotes directive in the php.ini file as follows:

magic_quotes_gpc = off

How to enable and disable the PHP register_globals directive?

When the register_globals directive is enabled, PHP creates variables 
automatically from HTML form parameters and cookie data. 
To enable this functionality, use a text editor to modify the 
register_globals directive in the php.ini file as follows:

register_globals = on

To disable this functionality, modify the register_globals 
directive in the php.ini file as follows:

register_globals = off

How to enable and disable the PHP allow_url_fopen directive?

To enable this functionality, use a text editor to modify 
the allow_url_fopen directive in the php.ini file as follows:

allow_url_fopen = on

To disable this functionality, modify the allow_url_fopen 
directive in the php.ini file as follows:

allow_url_fopen = off

How to change the maximum number of input variables for PHP scripts?

PHP obtains input variables from HTML forms (through GET and POST requests), 
as well as from any cookies enabled on a page. 
By default, the maximum number of input variables allowed for 
PHP scripts is set to 1000. You can change this amount by 
adding the max_input_vars directive to a php.ini file.

To change the maximum number of input variables allowed, use a text editor
 to add the max_input_vars directive to your php.ini file. For example,
 to set the maximum number of input variables to 1500, add the following setting:

max_input_vars = 1500

How to enable and disable the expose_php directive?

When the expose_php directive is enabled, PHP includes the following line in 
the HTTP response header when a PHP page is requested 
(the exact version number may differ depending on your configuration):

X-Powered-By: PHP/5.3.27

By default, the expose_php directive is enabled. However, you may not want to 
broadcast the specific PHP version your site is using. Similarly, 
some third-party applications require the expose_php directive to be disabled.

To disable the expose_php directive, use a text editor to modify your php.ini file as follows:

expose_php = off

With the expose_php directive disabled, PHP will not send the X-Powered-By header. 
To re-enable the expose_php directive and send the X-Powered-By header, 
modify your php.ini file as follows:

expose_php = on

How to do copy files from one directory to another directory using php?

Copy files from one directory to another directory using php
copy('foo/test.php', 'bar/test.php');