Skip to content

Instantly share code, notes, and snippets.

@YOzaz
Created May 5, 2016 16:44
Show Gist options
  • Save YOzaz/c11140c877d04732b6147d0277a2595d to your computer and use it in GitHub Desktop.
Save YOzaz/c11140c877d04732b6147d0277a2595d to your computer and use it in GitHub Desktop.
<?php namespace MyApp;
use Config;
trait LocalPathsTrait
{
/**
* Replaces remote URL to Local Path
*
* @param string $url remote image or file (with http and www stuff)
*
* @return string
*/
public function convertToLocalPath( $url )
{
$app_url = rtrim( Config::get('app.url'), '/' );
$public_path = rtrim( public_path(), '/' );
$result = $url;
$result = str_replace( $app_url, $public_path, $result );
$app_url = str_replace( 'https://', 'http://', $app_url );
$result = str_replace( $app_url, $public_path, $result );
return $result;
}
/**
* Replaces local path to Remote URL
*
* @param string $url local image or file
*
* @return string
*/
public function convertToRemoteURL( $url )
{
return $this->convertToExternalPath( $url );
}
/**
* Replaces local URL to External URL
*
* @param string $url local image or file
*
* @return string
*/
public function convertToExternalPath( $url )
{
$app_url = rtrim( Config::get('app.url'), '/' );
$public_path = rtrim( public_path(), '/' );
$result = str_replace( $public_path, $app_url, $url );
return $result;
}
/**
* Checks if file is local
*
* @param string $url
*
* @return bool
*/
public function isFileLocal( $url )
{
return starts_with( $url, Config::get('app.url') );
}
/**
* Checks if image is local
*
* @param string $url
*
* @return bool
*/
public function isImageLocal( $url )
{
return $this->isFileLocal( $url );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment