#! /usr/bin/php
<?php
/*
$ /Users/kasutaja/Sites/ukjlib/class.data_uri/html5slideshow.php -i/Volumes/UKJ1G/images/ -s"/Volumes/UKJ1G/sounds" -t"My photoshoot"
*/
$pathToScriptDir = pathinfo( __FILE__,PATHINFO_DIRNAME ).DIRECTORY_SEPARATOR;
$scriptFilename = pathinfo(__FILE__,PATHINFO_BASENAME );
$path_to_magic_file = is_file($pathToScriptDir.'magic') ? $pathToScriptDir.'magic' : NULL;
$template_title_default = 'Photoshoot';
$template_title_placeholder = '<slideshowtitle_4njk3 />';
$template_slideshow_placeholder = '<div id="slideshow_4njk3">';
$default_template = $pathToScriptDir . 'html5slideshow-template.htm';
$message_audio_error = "Your browser does not support the audio tag.";
require $pathToScriptDir . 'class.data_uri.php';
$jpegraw = $jpeg_file = $jpeg_directory = $jpegduc =
$mp3raw = $mp3file = $mp3dir = $mp3duc =
$default_template_data = $slideshow_title = '';
$jpeg_files = $default_template_data_a = array();
$image_files_written = $audio_files_written = 0;
/**
* http://code.google.com/p/tylerhall/source/browse/trunk/class.args.php
*/
class Args
{
private $flags;
public $args;
public function __construct()
{
$this->flags = array();
$this->args = array();
$argv = $GLOBALS['argv'];
array_shift($argv);
for($i = 0; $i < count($argv); $i++)
{
$str = $argv[$i];
// --foo
if(strlen($str) > 2 && substr($str, 0, 2) == '--')
{
$str = substr($str, 2);
$parts = explode('=', $str);
$this->flags[$parts[0]] = true;
// Does not have an =, so choose the next arg as its value
if(count($parts) == 1 && isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
{
$this->flags[$parts[0]] = $argv[$i + 1];
}
elseif(count($parts) == 2) // Has a =, so pick the second piece
{
$this->flags[$parts[0]] = $parts[1];
}
}
elseif(strlen($str) == 2 && $str[0] == '-') // -a
{
$this->flags[$str[1]] = true;
if(isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
$this->flags[$str[1]] = $argv[$i + 1];
}
elseif(strlen($str) > 1 && $str[0] == '-') // -abcdef
{
for($j = 1; $j < strlen($str); $j++)
$this->flags[$str[$j]] = true;
}
}
for($i = count($argv) - 1; $i >= 0; $i--)
{
if(preg_match('/^--?.+/', $argv[$i]) == 0)
$this->args[] = $argv[$i];
else
break;
}
$this->args = array_reverse($this->args);
}
public function flag($name)
{
return array_key_exists($name, $this->flags) ? $this->flags[$name] : false;
}
/**
* Returns first matching value of given flags.
* Usage:
* $help_basic = "\n-h Help \n -v Program verson \n -f Input \n -o Output \n -b Verbose mode \n\n";
* $args = new Args();
* if( $args->flags('h','H','?','help','HELP') ) { exit( $help_basic ); }
*
* @author http://ukj.pri.ee http://www.hot.ee/u/ukjoesaar
* @return mixed
*/
public function flags()
{
foreach(func_get_args() as $name){
if( array_key_exists($name, $this->flags) ) return $this->flags[$name];
}
return false;
}
}
$args = new Args();
function text_columns($columns=array(),$left_margin=4) {
if(!is_array($columns)) return "\n\n";
$margin = str_repeat(' ', $left_margin);
$result = '';
$col1_w = 28;
$col2_w = 40;
$col1_merge = $col2_merge = array();
foreach($columns as $row)
{
$len1 = strlen($row[0]);
$len2 = strlen($row[1]);
$col1_a = explode("\n", $margin.str_replace("<ln/>", "\n".$margin, wordwrap($row[0] ,$col1_w,"<ln/>",TRUE) ));
$col1_ac = count($col1_a);
$col2_a = explode("\n", $margin.str_replace("<ln/>", "\n".$margin, wordwrap($row[1] ,$col2_w,"<ln/>",FALSE) ));
$col2_ac = count($col2_a);
if($col1_ac > $col2_ac) {
$col12_a = &$col1_a;
$col22_a = array_pad($col2_a, $col1_ac, ' ');
}
if($col1_ac < $col2_ac) {
$col12_a = array_pad($col1_a, $col2_ac, str_repeat(' ', $left_margin+$col1_w));
$col22_a = &$col2_a;
}
if($col1_ac == $col2_ac);{
$col12_a = &$col1_a;
$col22_a = &$col2_a;
}
$col1_merge = array_merge($col1_merge, $col12_a);
$col2_merge = array_merge($col2_merge, $col22_a);
}
$col_rowc = count($col1_merge);
$col12_alla=array(); foreach($col1_merge as $col12_all) $col12_alla[] = strlen($col12_all);$col12_amll = max($col12_alla);
//print_r($col1_merge); print_r($col2_merge);
for($i=0;$i<$col_rowc;$i++) {
$result .= str_pad($col1_merge[$i], $col12_amll) . $col2_merge[$i] . "\n";
}
return $result;
}
$scriptFilename_len = strlen($scriptFilename) + 3;
$scriptFilename_LLL = str_repeat('-', $scriptFilename_len);
$scriptFilename_SSS = str_repeat(' ', $scriptFilename_len);
$help_basic = "\n"
."html5slideshow.php | Creates slideshow with audio background. \n"
.$scriptFilename_LLL ."+ Usability of result depends heavily how borwser \n"
.$scriptFilename_SSS ." supports data uri and html5 audio. \n\n";
$help_a = array(
array('-h -H -?<ln/>--help --HELP', 'Help'),
array('-i <path_to_dir><ln/>--image-dir <path_to_dir>', 'Add images'),
array('-s <path_to_file><ln/>--sound-file <path_to_file>','Add audio. Sound file or directory. Same sound in different formats(ogg/mp3/mp4/aac).'),
array('-t "tile"<ln/>-title "tile"','Write short description.')
);
$mp3mimes = array(
'ogg'=>'audio/ogg','oga'=>'audio/ogg',
'mp3'=>'audio/mpeg','mp4'=>'audio/mp4','m4a'=>'audio/mp4','aac'=>'audio/aac',
'webma'=>'audio/webm','webm'=>'video/webm','
wav'=>'audio/wav',
'fla'=>'audio/fla');
$data_uri_o = new data_uri;
$data_uri_o->set_magic_file($path_to_magic_file);
/**
* Compiles data: uri
*
* @param string $contents
* @param string $mime
* @param int $encoding 1:base64, 0:rawurlencode
* @param string $charset '' or 'auto' or charset
* @return string;
*/
function data_uri_(&$contents, $mime, $encoding=1, $charset='' )
{
if($encoding==1) {
$data = base64_encode($contents);
$enc = ';base64';
}
elseif($encoding==0) {
$data = rawurlencode($contents);
$enc = '';
}
// charset for textual data
if($charset=='auto') {
if(
is_int(strpos($mime,'text')) or
is_int(strpos($mime,'xml')) or
is_int(strpos($mime,'script')) or
is_int(strpos($mime,'php'))
)
if($charset = mb_detect_encoding($contents)) $charset = ';charset=' . $charset; else ;
else $charset = '';
}
elseif($charset!='') {
$charset = ';charset=' . $charset;
}
else $charset = '';
return 'data:' . $mime . $charset . $enc . ','. $data;
}
/**
* Returns human readfirendly filesize unsing b kb Mb Gb Tb Pb Eb
*
* @param mixed $bytes If not numeric, trys handle as filename
* @param int $decimals
* @return string
*/
function filesize_h($bytes, $decimals = 3) {
if( !is_numeric($bytes) ) {
clearstatcache();
$bytes = filesize($filename);
}
$bytes = (float)$bytes;
$units = array(
'1152921504606846976' => 'Eb', /* Exa Byte 10^18 */
'1125899906842624' => 'Pb', /* Peta Byte 10^15 */
'1099511627776' => 'Tb',
'1073741824' => 'Gb',
'1048576' => 'Mb',
'1024' => 'Kb'
);
if($bytes <= 1024)
return $bytes . ' b';
$decimals = ($decimals < 1)?1: ($decimals>10)?10: $decimals;
foreach($units as $base => $title)
if(floor($bytes / $base) != 0)
return number_format($bytes / $base, (int) $decimals, '.', "'") . ' ' . $title;
} // end filesize_h()
function exit_error_cant_read($file) { exit("Error: Can't read file \"".$file."\" !\n"); }
if(!defined('SCANDIR_SORT_ASCENDING'))define('SCANDIR_SORT_ASCENDING',0);
if(!defined('SCANDIR_SORT_DESCENDING'))define('SCANDIR_SORT_DESCENDING',1);
if(!defined('SCANDIR_SORT_NONE'))define('SCANDIR_SORT_NONE',2);
/**
* Result with no .dotfiles & no tildefiles~
*
* @param string $directory
* @param int $sort 0 SCANDIR_SORT_ASCENDING | 1 SCANDIR_SORT_DESCENDING | 2 SCANDIR_SORT_NONE
* @param array $exclude filenames to exclude
* @author ukj@ukj.pri.ee
* @return array filenemes
*/
function scandir_nd($directory,$sorting_order=SCANDIR_SORT_ASCENDING, $exclude=array() )
{
$files=array();
if($a = scandir($directory)) {
foreach($a as $filename) {
if( ($filename[0] != '.') && (substr($filename, -1, 1 ) != '~')) {
if( !in_array($filename, $exclude) ) $files[] = $filename;
}
}
switch( $sort!=1?0:1 )
{
case 0:sort($files,SORT_STRING);break;
case 1:rsort($files,SORT_STRING);break;
}
return $files;
}
return FALSE;
}
// argv
if( $args->flags('h','H','?','help','HELP') ) { exit( $help_basic . $help_basic .= text_columns($help_a, 4) ); }
if( is_string( $jpeg_directory = $args->flags('i','image-dir') ) ) {
if(is_dir($jpeg_directory) && is_readable($jpeg_directory) )
;
else { exit_error_cant_read($jpeg_directory); }
}
if( is_string( $mp3file = $args->flags('s','sound-file') ) ) {
if(is_file($mp3file) && is_readable($mp3file) )
;
elseif(is_dir($mp3file) && is_readable($mp3file) ) {
$mp3dir = $mp3file; $mp3file = '';
}
else { exit_error_cant_read($mp3file); }
}
if( is_string( $slideshow_title = $args->flags('t','title') ) ) {
if($slideshow_title = trim($slideshow_title) )
;
else $slideshow_title = $template_title_default;
} else $slideshow_title = $template_title_default;
$jpeg_directory = rtrim($jpeg_directory,DIRECTORY_SEPARATOR) .DIRECTORY_SEPARATOR;
$jpeg_Dirname=end(explode('/', trim($jpeg_directory,DIRECTORY_SEPARATOR) ));
if($jpeg_Dirname != '')
$outhtmlfilename = $jpeg_Dirname.'.html';
// READING THE TEMPLATE IN
if(is_file($default_template) && is_readable($default_template) ) {
$default_template_data = file_get_contents($default_template);
if(is_string($default_template_data)) {
$default_template_data_a = explode($template_slideshow_placeholder, $default_template_data);
$default_template_data = '';
} else {
exit_error_cant_read($default_template);
}
} else exit_error_cant_read($default_template);
if($jpeg_directory != '' && count($default_template_data_a)==2) {
// WRITE <TITLE />
$default_template_data_a[0] = str_replace($template_title_placeholder,$slideshow_title,$default_template_data_a[0]);
file_put_contents($outhtmlfilename, $default_template_data_a[0]);
$default_template_data_a[0] = '';
// WRITE <AUDIO />
if($mp3file != '') {
$extension = trim( pathinfo(strtolower($mp3_file), PATHINFO_EXTENSION));
file_put_contents($outhtmlfilename, "\n\n<audio autobuffer autoplay loop >\n".
// "<source src=\"" . $mp3file . "\" type=\"" . $data_uri_o->get_mime() . "\" />\n"
"<source src=\"" . $data_uri_o->fromFile($mp3file) . "\" type=\"".$data_uri_o->get_mime()."\" /> \n".
"$message_audio_error\n</audio>\n\n", FILE_APPEND );
$audio_files_written++;
} elseif($mp3dir != '') {
$mp3dir = rtrim($mp3dir,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
file_put_contents($outhtmlfilename, "\n<audio autobuffer autoplay loop >\n", FILE_APPEND );
$mp3dir_files = scandir_nd($mp3dir);
//count($mp3_files);
foreach($mp3dir_files as $mp3_file) {
$data_uri_o->file_mime_type($mp3dir.$mp3_file);
$mimeclass = $data_uri_o->get_mime_class();
$subtype = $data_uri_o->get_mime_subtype();
//$extension = trim( pathinfo(strtolower($mp3_file), PATHINFO_EXTENSION));
if($mimeclass == 'audio' || $mimeclass=='video') {
$data_uri_o->set_mime_type('');
file_put_contents($outhtmlfilename,
//"<source src=\"".$mp3dir.$mp3_file."\" type=\"".$mp3mimes[$extension]."\" />\n".
"<source src=\"" . $data_uri_o->fromFile($mp3dir.$mp3_file ,'audio/'.$subtype ) . "\" alt=\"" . $mp3_file . "\" type=\"".'audio/'.$subtype . "\" /> \n", FILE_APPEND );
$datauri='';
$audio_files_written++;
}
}
file_put_contents($outhtmlfilename, "\n$message_audio_error</audio>\n\n", FILE_APPEND );
}
// <IMG />
file_put_contents($outhtmlfilename, $template_slideshow_placeholder."\n\n", FILE_APPEND );
//,$sorting_order=SCANDIR_SORT_ASCENDING , $exclude=array()
$jpeg_files = scandir_nd($jpeg_directory);
$image_files_written = 0;
if(is_array($jpeg_files)) {
foreach($jpeg_files as $jpeg_file) {
if(in_array(trim( pathinfo(strtolower($jpeg_file), PATHINFO_EXTENSION) ),array('jpg','jpeg'))) {
$data_uri_o->set_mime_type('');
file_put_contents($outhtmlfilename,"<img src=\"" . $data_uri_o->fromFile($jpeg_directory.$jpeg_file) . "\" alt=\"" . $jpeg_file . "\" /> \n", FILE_APPEND );
$jpegraw = $jpegduc = '';
$image_files_written++;
echo ".";
}
}
}
// FINALIZE
file_put_contents($outhtmlfilename, $default_template_data_a[1], FILE_APPEND );
// REPORT
echo
"\n".
"Slideshow title: $slideshow_title \n".
"$audio_files_written audio file. \n".
"$image_files_written image file. \n".
filesize_h(filesize($outhtmlfilename), 2) .
" written in to \"$outhtmlfilename\" \n".
"\n";
}
?> |