Search:

Categories



Site is down redirection

<?php
$host = ‘www.yoursitehere.com’;
$up = ping($host);
//site is up
if( $up )
{
header(‘Location: http://’.$host);
}

//site is down
else
{
header(‘Location: http://www.yourothersite.com’);
}
?>

WordPress old post warning

<?php
// Put inside Post Loop

$time_defined_as_old = 60*60*24*365; // A Year

if((date(‘U’)-get_the_time(‘U’)) > $time_defined_as_old) {
echo ‘<div><strong>Notice</strong><br />This article is published more than one year ago.</div>’;
}

?>

Convert a PDF to a jpeg

Convert a PDF to a jpeg using imagemagick

 

/**
* PDF2JPG script
* with ImageMagick
*/

$pdf_file = ‘./pdf/demo.pdf’;
$save_to = ‘./jpg/demo.jpg’; //make sure that apache has permissions to write in this folder! (common problem)

//execute ImageMagick command ‘convert’ and convert PDF to JPG with applied settings
exec(‘convert “‘.$pdf_file.’” -colorspace RGB -resize 800 “‘.$save_to.’”‘, $output, $return_var);

if($return_var == 0) { //if exec successfuly converted pdf to jpg
print “Conversion OK”;
}
else print “Conversion failed.<br />”.$output;

HSV to RGB example

HSV to RGB example

Code

function HSV_TO_RGB(array $hsv) {
list($H,$S,$V) = $hsv;
//1
$H *= 6;
//2
$I = floor($H);
$F = $H – $I;
//3
$M = $V * (1 – $S);
$N = $V * (1 – $S * $F);
$K = $V * (1 – $S * (1 – $F));
//4
switch ($I) {
case 0:
list($R,$G,$B) = array($V,$K,$M);
break;
case 1:
list($R,$G,$B) = array($N,$V,$M);
break;
case 2:
list($R,$G,$B) = array($M,$V,$K);
break;
case 3:
list($R,$G,$B) = array($M,$N,$V);
break;
case 4:
list($R,$G,$B) = array($K,$M,$V);
break;
case 5:
case 6: //for when $H=1 is given
list($R,$G,$B) = array($V,$M,$N);
break;
}
return array($R, $G, $B);
}

Random video wordpress shortcode

This is an example of a wordpress shortcode that would display a random video, not very technical as the video embed is hard coded in but nice and basic

<?php
function md_random_video() {
	$videos = array(
		'Video Embed Code Goes Here',
		'Another Video Embed Code Goes Here',
		'So on',
		'So forth'
	);
	$rand_vid = array_rand( $videos );

	echo $videos[$rand_vid];
}

//Shortcode for WordPress only!
add_shortcode( 'randvid', 'md_random_video' );
?>

 

CSV Parser

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Data Import</title>
</head>

<body>

<?php

//error_reporting(E_ALL);
//ini_set(“display_errors”, 1);

$databasehost = “xxxxx”;
$databasename = “xxxxx”;
$databasetable = “xxxxxx”;
$databaseusername =”xxxxxx”;
$databasepassword = “xxxxxx”;
$fieldseparator = “\t”;
$lineseparator = “\r”;
$csvfile = “xxxxxx.txt”;
echo “vars set — “;
$addauto = 0;

$save = 0;
$outputfile = “output.sql”;

if(!file_exists($csvfile)) {
echo “File not found. Make sure you specified the correct path.\n”;
exit;
} else {
echo “$csvfile File Exist….<br />”;
}

echo “opening file<br />”;
$file = fopen($csvfile,”r”);

if(!$file) {
echo “Error opening data file.\n”;
die(“oops!”);
} else {
echo “file opened…<br />”;
}

$size = filesize($csvfile);

if(!$size) {
echo “File is empty.\n”;
exit;
} else {
echo “File Size is $size”;
}
echo “about to read…”;

if (!$csvcontent = fread($file,$size)) {

die(“Could Not Read File <br />”);

} else {

echo “File Read <br />”;

}

fclose($file);

if (isset($_GET['import'])) {

//$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
//mysql_select_db($databasename) or die(mysql_error());

$lines = 0;
$queries = “”;
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

$lines++;

$line = trim($line,” \t”);

$line = str_replace(“\r”,”",$line);

$line = str_replace(“‘”,”\’”,$line);

$line = str_replace(“\”",”",$line);

$linearray = explode($fieldseparator,$line);

$linemysql = implode(“|”,$linearray);
//echo “$linemysql<br /><br />”;
$fields = explode(“|”,$linemysql);
print_r($fields);

echo “<br /><br />”;

$query = “INSERT INTO $databasetable (xxxxxx,xxxxxx,xxxxxx) VALUES (‘$linemysql’);”;

//echo “$query — $lines — <br /><br />”;

//mysql_query($query);

}

} // end if is set GET_

mysql_close($con);

?>

</body>
</html>