Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Image Base64_Encode

// Encode pics in base64

<?php

## Config
$imgDir = './img/';
$banDir = array ('.','..');
$goodExt = array ('png','jpg','jpeg','gif'); -- A venir
## End Config


$img = '';
$case = '';
$handle = opendir($imgDir);

while ($file = readdir($handle)) 
{
	$ext = array_pop(explode('.', $file));

	if(!is_dir($file) && !in_array($file, $banDir) && in_array($ext, $goodExt)){
	
		$path = $imgDir.$file;
		$name = strrev(substr(strrev($file), 4));
		
		$img .= '$img_'.$name.' = <<< EOFILE'."<br />";
		$img .= base64_encode(file_get_contents($path))."<br />";
		$img .= 'EOFILE;'."<br />";
				
		$case .= 'case \'img_'.$name.'\' :'."<br />";
		$case .= 'header("Content-type: image/'.$ext.'");'."<br />";       
		$case .= 'echo base64_decode($img_'.$name.');'."<br />";       
		$case .= 'exit();'."<br />";   
		
		
	}

}
			print $img;		
				print "<br /><hr /><br />";
			print $case;
			

?>

You need to create an account or log in to post comments to this site.


Click here to browse all 5137 code snippets