/var/www/vhosts/nabawater/common/helpers
Edit: /var/www/vhosts/nabawater/common/helpers/GeoHelper.php (12535B)
*/
class GeoHelper
{
/**
* @var string
*/
private $path;
/**
* @var string
*/
private $basePath;
/**
* @var UploadHelper
*/
private static $instance;
/**
* @var string
*/
private $location;
private $mapKey;
/**
* UploadHelper constructor.
* @throws \yii\base\InvalidParamException
*/
private function __construct()
{
$this->basePath = Yii::getAlias('@uploads');
$this->mapKey = Configuration::get(Configuration::MAP_KEY);
}
/**
* @return UploadHelper
* @throws \yii\base\InvalidParamException
*/
public static function getInstance()
{
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param $fromLatitude
* @param $fromLongitude
* @param $toLatitude
* @param $toLongitude
* @throws \yii\base\Exception
*/
/**
* returns the distance of given geo points.
* calculates the unit based on the resultant distance, if distance is less than 1000 then
* returns in meter else in kilometer
*
* @param $fromLatitude
* @param $fromLongitude
* @param $toLatitude
* @param $toLongitude
* @param string $unit
* @return float
*
* @link https://stackoverflow.com/a/10054282/5798881
* @link https://stackoverflow.com/a/37184359/5798881
* @modified : A Vijay
*
*/
public static function distance($fromLatitude, $fromLongitude, $toLatitude, $toLongitude, $unit = 'Km')
{
$theta = $fromLongitude - $toLongitude;
$distance = (sin(deg2rad($fromLatitude)) * sin(deg2rad($toLatitude))) + (cos(deg2rad($fromLatitude)) * cos(deg2rad($toLatitude)) * cos(deg2rad($theta)));
$distance = acos(min(max($distance, -1.0), 1.0));
# $distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch ($unit) {
case 'Mi':
break;
case 'Km' :
$distance = $distance * 1.609344;
}
return (round($distance, 2));
}
/**
*
* @param array $location
* @return $this
*/
public function setLocation(array $location) {
$this->location = $location;
return $this;
}
/**
*
* @return string
*/
public function getAddress() {
$geoData = [];
$url = sprintf(
'http://maps.googleapis.com/maps/api/geocode/json?latlng=%s&sensor=false&key=%s', implode(',', $this->location),$this->mapKey
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
if (!Com::isJson($json)) {
goto skip;
}
$json = json_decode($json, true);
if (
!ArrayHelper::keyExists('status', $json) ||
strtolower($json['status']) !== 'ok'
) {
goto skip;
}
$geoData = $this->format($json);
skip:
return new GeoData($geoData);
}
private function format($addressArr) {
if (!ArrayHelper::keyExists('results', $addressArr)) {
return [];
}
$addressArr = $addressArr['results'];
if ($addressArr === []) {
return [];
}
$addressArr = $addressArr[0];
if (!ArrayHelper::keyExists('address_components', $addressArr)) {
return [];
}
$addressArr = $addressArr['address_components'];
$locationParam = ['locality', 'street_number', 'route', 'administrative_area_level_1', 'country', 'postal_code'];
$addressData = [];
foreach ($addressArr as $element) {
if (($location = array_intersect($locationParam, $element['types'])) === []) {
continue;
}
$location = end($location);
$addressData[$location] = $element['long_name'];
}
return $addressData;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param $path
* @return string
* @throws \yii\base\InvalidParamException
*/
public function getRealPath($path)
{
$path = ltrim(str_ireplace(Yii::getAlias('@approot'), '', $path) , DIRECTORY_SEPARATOR);
/**
* @note: DIRECTORY_SEPARATOR is different for linux based system and windows so, replacing it
* with forward slashes for web accessible URL
* @author A Vijay
*/
return str_replace([DIRECTORY_SEPARATOR], '/', $path);
}
/**
* @param string $path
* @return string
* @throws \yii\base\InvalidParamException
*/
public function getAbsPath( $path )
{
$path = ltrim($path, DIRECTORY_SEPARATOR);
$appRoot = Yii::getAlias('@approot');
$match = ltrim($appRoot, DIRECTORY_SEPARATOR);
$match = preg_quote($match, DIRECTORY_SEPARATOR);
$match = sprintf('/%s/', $match);
if (preg_match($match, $path) !== 0 ) {
return DIRECTORY_SEPARATOR . $path;
}
return $appRoot . DIRECTORY_SEPARATOR . $path;
}
/**
* @param $path
*/
public function clean($path)
{
$path = rtrim($path, DIRECTORY_SEPARATOR);
if( !file_exists($path) ){
return;
}
if( is_file($path) ){
unlink($path);
}else{
$files = glob(sprintf('%s%s*', $path, DIRECTORY_SEPARATOR)); // get all file names
foreach($files as $file){ # Iterate files
if(is_file($file) && !in_array(basename($file), ['.htaccess', 'index.php'], true)){
unlink($file); # Delete file
}
}
}
}
/**
* @param $pathArr
*/
public function cleanByArray(array $pathArr)
{
foreach ($pathArr as $path) {
$this->clean($path);
}
}
/* ******************** */
/* Deprecated functions */
/* ******************** */
/* A Vijay */
/* ******************** */
/**
*
* @throws \yii\base\InvalidParamException
* @throws \yii\base\Exception
*
* @deprecated
*/
private static function init()
{
self::$path = Yii::getAlias('@uploads');
if( self::check() ){
self::setPath();
}
}
/**
* @param $path
* @throws \yii\base\InvalidParamException
* @throws \yii\base\Exception
*
* @deprecated
*/
private static function join( $path )
{
if( self::$path === null ){
self::init();
}
self::$path = self::$path . DIRECTORY_SEPARATOR . $path;
}
/**
* @param $path
* @param bool $includeDirSep
* @return string
* @throws \yii\base\Exception
* @throws \yii\base\InvalidParamException
*/
public static function getPath1( $path, $includeDirSep = true )
{
self::join($path);
if( self::check() ){
self::setPath();
}
if( $includeDirSep ){
self::$path .= DIRECTORY_SEPARATOR;
}
return self::$path;
}
/**
*
* @param string $path
* @param boolean $absPath
* @return string
*/
public static function getThumbName( $path, $absPath = true )
{
// [$srcName, $ext] = explode('.', basename($path));
$srcName .= "_thumb.$ext";
if ( $absPath === true ) {
return dirname($path) . DIRECTORY_SEPARATOR . $srcName;
}
return $srcName;
}
/**
* @param $path
* @param bool $realPath
* @return string
* @throws \yii\base\InvalidParamException
*/
public static function generateThumb($path, $realPath = true)
{
$image = file_get_contents( $path );
$imageAttr = getimagesizefromstring( $image );
$imageType = explode('/', $imageAttr['mime']);
$imageType = $imageType[1];
switch( 1 ){
case 1: # Thumbnail
$width = (new Config())->get(Config::THUMBNAIL_WIDTH);
if( $width === null || (int)$width === 0 ){
$width = 145;
}
$height = ( $width * $imageAttr[1] ) / $imageAttr[0];
break;
case 2: # Medium
$width = 210;
$height = 200;
break;
case 3: # Large
// [$width, $height] = $imageAttr;
# $width = $imageAttr[0];
# $height = $imageAttr[1];
break;
default:
$width = 145;
$height = ( $width * $imageAttr[1] ) / $imageAttr[0];
break;
}
$src = imagecreatefromstring( $image );
$dst = imagecreatetruecolor( $width, $height );
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $imageAttr[0], $imageAttr[1]);
imagedestroy( $src );
$path = self::getThumbName($path);
switch( $imageType ){
case 'png':
imagepng( $dst, $path );
break;
case 'jpg':
case 'jpeg':
imagejpeg( $dst, $path );
break;
default:
imagepng( $dst, $path );
break;
}
imagedestroy($dst);
if( $realPath === true ){
return self::getRealPath($path);
}
return $path;
}
/**
* @param string $path
* @param array $option
* @param bool $realPath
* @return string
* @throws \yii\base\InvalidParamException
* @throws \yii\base\Exception
*
* @deprecated
*
* @author A Vijay
*/
public static function crop($path, array $option, $realPath = true)
{
$defaultOptions = ['height' => 100, 'width' => 100, 'x' => 0, 'y' => 0, 'path' => false, 'name' => time()];
$option = array_merge($defaultOptions, $option);
if( !file_exists($path) ){
throw new Exception('Requested source not found !');
}
$image = file_get_contents( $path );
$imageAttr = getimagesizefromstring( $image );
$imageType = explode('/', $imageAttr['mime']);
$imageType = $imageType[1];
$src = imagecreatefromstring( $image );
$dst = imagecreatetruecolor( $option['width'], $option['height'] );
/**
* @link: https://www.sitepoint.com/community/t/gd-cropping-with-imagecopyresampled/2915/3
*/
imagecopyresampled(
$dst,
$src,
0,
0,
$option['x'],
$option['y'],
$option['width'],
$option['height'],
$option['width'],
$option['height']
// $imageAttr[0] - $cross * $option['x'],
// $imageAttr[1] - $cross * $option['y']
);
imagedestroy( $src );
if( $option['path'] === false ){
$option['path'] = dirname($path);
}
$path = rtrim($option['path'], DIRECTORY_SEPARATOR);
$path = sprintf('%s%s%s.%s', $path, DIRECTORY_SEPARATOR, $option['name'], $imageType);
switch( $imageType ){
case 'png':
imagepng( $dst, $path );
break;
case 'jpg':
case 'jpeg':
imagejpeg( $dst, $path );
break;
default:
imagepng( $dst, $path );
break;
}
imagedestroy($dst);
if( $realPath === true ){
return self::getRealPath($path);
}
return $path;
}
}