Created
January 24, 2022 10:55
-
-
Save jonarnes/289b5c09b678e5c37135906938173dcd to your computer and use it in GitHub Desktop.
Snippet to enable ImageEngine in laravel apps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Computes an ImageEngine src for images as defined in the config. | |
* | |
* May be used in *.blade.php files like this: | |
* <img src="{{ imageengine( "/img/img.jpeg",array("directives"=>"/w_200/") ) }}"> | |
* | |
* @param string $asset | |
* @param array $options | |
* @return string | |
* | |
*/ | |
function imageengine( $asset , $options = null){ | |
// Check if ImageEngine is configured in the app.php file | |
if( !Config::get('app.imageengine') ) | |
return asset( $asset ); | |
// Get ImageEngine info | |
$imgeng = Config::get('app.imageengine'); | |
// Check if the file extension should be pulled through ImageEngine | |
$assetName = explode("?", basename( $asset )); | |
if( preg_match('/^.*\.(' . $imgeng['files'] . ')$/i', $assetName[0]) ){ | |
return ieSrc($imgeng['delivery_address'], $asset, $options); | |
} | |
return $asset ; | |
} | |
function ieSrc($ie, $asset, $options) { | |
$urlparts = parse_url($asset); | |
//Check if absolute asset url should be prefixed | |
if (isset($options['prefix']) && $options['prefix'] === true && isset($urlparts['scheme'])){ | |
//Add any directives | |
if(isset($options['directives']) && strlen($options['directives'])>1){ | |
return "//" . rtrim($ie, "/") . "/" . trim( $options['directives'], "/") . "/" . ltrim( $asset, "/"); | |
}else{ | |
return "//" . rtrim($ie, "/") . "/" . ltrim( $asset, "/"); | |
} | |
} | |
//If no prefixig add or replace the host with the ImageEngine delivery address | |
$asset = rtrim($ie, "/").$urlparts['path'].(isset($urlparts['query']) ? "?".$urlparts['query'] : ""); | |
//append any url directives | |
if(isset($options['directives']) && strlen($options['directives'])>1 ){ | |
$op = (isset($urlparts['query']) ? "&" : "?"); | |
$asset .= $op."imgeng=/".trim($options['directives'],"/"); | |
} | |
return "//" . ltrim( $asset, "/"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment