Skip to content

Instantly share code, notes, and snippets.

@jmathai
Created February 4, 2011 16:19
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 jmathai/811297 to your computer and use it in GitHub Desktop.
Save jmathai/811297 to your computer and use it in GitHub Desktop.
function.php
<?php
/*
* This is a sample function that guarantees unique URLs for frontend assets.
* Images for example.
* Include this and your users will never re-download an asset they already
* have just just because you push a new build.
* If including this in your stylesheet then you should set this up in you
* should be using something like Amazon Cloud Front to cache a static
* version of it.
*/
function getAssetImage($path)
{
$md5 = sha1_file($path);
preg_match('/\.([a-zA-Z]{3,4})$/', $path, $matches);
$ext = $matches[1];
$dest = "/img/static/{$md5}.{$ext}";
if(!file_exists($dest))
copy($path, $dest);
return $dest;
}
?>
<img src="<?php echo getAssetImage('/img/logo.png'); ?>">
body{
background-image:url('<?php echo getAssetImage('/img/background.png'); ?>');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment