emersonbroga
7/26/2013 - 4:38 AM

Generates a base64 image constant from an image folder.

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);
}