Skip to content

Instantly share code, notes, and snippets.

@jonarnes
Created January 24, 2022 10:55
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 jonarnes/289b5c09b678e5c37135906938173dcd to your computer and use it in GitHub Desktop.
Save jonarnes/289b5c09b678e5c37135906938173dcd to your computer and use it in GitHub Desktop.
Snippet to enable ImageEngine in laravel apps.
<?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