Skip to content

Instantly share code, notes, and snippets.

@gmilby
Created November 29, 2013 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmilby/7712996 to your computer and use it in GitHub Desktop.
Save gmilby/7712996 to your computer and use it in GitHub Desktop.
Calculate the distance between two coordinates
When we need to measure the distance between two points, we may use one of the following formulas: Haversine formula or Vincenty’s formula. There are two appropriately named functions:
01
function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {
02
// convert from degrees to radians
03
$latFrom = deg2rad($latitudeFrom);
04
$lonFrom = deg2rad($longitudeFrom);
05
$latTo = deg2rad($latitudeTo);
06
$lonTo = deg2rad($longitudeTo);
07
08
$latDelta = $latTo - $latFrom;
09
$lonDelta = $lonTo - $lonFrom;
10
11
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
12
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
13
return $angle * $earthRadius;
14
}
15
16
public static function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {
17
// convert from degrees to radians
18
$latFrom = deg2rad($latitudeFrom);
19
$lonFrom = deg2rad($longitudeFrom);
20
$latTo = deg2rad($latitudeTo);
21
$lonTo = deg2rad($longitudeTo);
22
23
$lonDelta = $lonTo - $lonFrom;
24
$a = pow(cos($latTo) * sin($lonDelta), 2) +
25
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
26
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
27
28
$angle = atan2(sqrt($a), $b);
29
return $angle * $earthRadius;
30
}
Both functions use the following parameters:
float $latitudeFrom – Latitude of start point in [deg decimal]
float $longitudeFrom – Longitude of start point in [deg decimal]
float $latitudeTo – Latitude of target point in [deg decimal]
float $longitudeTo – Longitude of target point in [deg decimal]
float $earthRadius – Mean earth radius in miles
Functions return – (float) Distance between points in miles (same as earthRadius)
Email debug PHP errors
01
function errorHandler($sMessage = '', $aVars = array()) {
02
03
$sScript = $_SERVER['PHP_SELF'];
04
$sParams = print_r($_REQUEST, true);
05
06
$sVars = print_r($aVars, true);
07
08
$aBackTrace = debug_backtrace();
09
unset($aBackTrace[0]);
10
$sBackTrace = print_r($aBackTrace, true);
11
12
$sExplanation = <<<EOF
13
<p>Additional explanation: {$sMessage}</p>
14
<p>Additional variables: <pre>{$sVars}</pre></p><hr />
15
<p>Called script: {$sScript}</p>
16
<p>Request parameters: <pre>{$sParams}</pre></p><hr />
17
<p>Debug backtrace:</p>
18
<pre>{$sBackTrace}</pre>
19
EOF;
20
21
$sHeader = "Subject: Error occurred\r\nContent-type: text/html; charset=UTF-8\r\n";
22
error_log($sExplanation, 1, 'admin@example.com', $sHeader);
23
}
This function is to email you about all occured errors on your website (this is much better instead of displaying it to the public). There are only two optional params:
string $sMessage – Custom message
array $aVars – Additional array to be sent by email
Convert PDF to JPG
1
function pdfToJpg($pdf, $jpg) {
2
$im = new Imagick();
3
$im->setResolution(300,300);
4
$im->readimage($pdf);
5
$im->setImageFormat('jpeg');
6
$im->writeImage($jpg);
7
$im->clear();
8
$im->destroy();
9
}
This function is to convert PDF files into image file. It takes two params:
string $pdf – Path to the initial PDF file
string $jpg – Path to the image file
Get age by birth date
01
function getAge($birthdate = '0000-00-00') {
02
if ($birthdate == '0000-00-00') return 'Unknown';
03
04
$bits = explode('-', $birthdate);
05
$age = date('Y') - $bits[0] - 1;
06
07
$arr[1] = 'm';
08
$arr[2] = 'd';
09
10
for ($i = 1; $arr[$i]; $i++) {
11
$n = date($arr[$i]);
12
if ($n < $bits[$i])
13
break;
14
if ($n > $bits[$i]) {
15
++$age;
16
break;
17
}
18
}
19
return $age;
20
}
This function is to get age by given birthday (format: YYYY-MM-DD).
Extract files from ZIP archive
view sourceprint?
01
function unzipArchive($file, $destinationFolder){
02
// create ZipArchive object
03
$zip = new ZipArchive() ;
04
// open archive
05
if ($zip->open($file) !== TRUE) {
06
die ('Could not open archive');
07
}
08
// extract it's content to destination folder
09
$zip->extractTo($destinationFolder);
10
// close archive
11
$zip->close();
12
}
This function takes two parameters:
string $file – Path to the initial ZIP file
string $destinationFolder – Path to the destination folder for files
Conclusion
Today is all, thank you for your attention, do not forget to visit us from time to time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment