Created
February 4, 2011 16:19
-
-
Save jmathai/811297 to your computer and use it in GitHub Desktop.
function.php
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 | |
/* | |
* 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; | |
} | |
?> |
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
<img src="<?php echo getAssetImage('/img/logo.png'); ?>"> |
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
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