Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Created July 26, 2013 04:38
Show Gist options
  • Select an option

  • Save emersonbroga/6086286 to your computer and use it in GitHub Desktop.

Select an option

Save emersonbroga/6086286 to your computer and use it in GitHub Desktop.
Generates a base64 image constant from an image folder.
<?php
// path to image folder
$folder = dirname(__FILE__);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder)) as $filename)
{
// check if is not file
if(!is_file($filename))
continue;
// get file information
$path_info = pathinfo($filename);
// get file contents
$image = file_get_contents($filename);
// get file base64
$imdata = base64_encode($image);
// create a base64 image string
$base64 = sprintf('data:image/%s;base64,%s',$path_info['extension'], $imdata );
// create a uppercase file name to generate the php constant
$filename = 'IMG_'.strtoupper($path_info['filename']);
// print the php constant
printf('defined(\'%s\') || defined(\'%s\', \'%s\'); <br/><br/>', $filename, $filename, $base64);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment