Skip to content

Instantly share code, notes, and snippets.

@mxmaxime
Created November 9, 2017 22:43
Show Gist options
  • Save mxmaxime/34e74d096b10251af8396c2d12e3ebd6 to your computer and use it in GitHub Desktop.
Save mxmaxime/34e74d096b10251af8396c2d12e3ebd6 to your computer and use it in GitHub Desktop.
<?php
function assets($path)
{
$file = realpath($path);
if (!file_exists($file)) throw new Exception("Le fichier {$file} n'existe pas");
$mtime = filemtime($file);
$path_info = pathinfo($path);
$newPath = $path_info['dirname'] . '/' . $path_info['filename'] . '.' . hash('md5', $mtime) . '.' . $path_info['extension'];
$newRealPath = realpath($newPath);
if (!file_exists($newRealPath)) {
copy($file, $newRealPath);
}
return $newPath;
}
?>
// Utilization
<img src="<?= assets("img/coffee.jpg") ?>" alt="coffeeeeeeeeeeee"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment