Skip to content

Instantly share code, notes, and snippets.

@staabm
Created September 26, 2012 06:59
Show Gist options
  • Save staabm/3786498 to your computer and use it in GitHub Desktop.
Save staabm/3786498 to your computer and use it in GitHub Desktop.
native stream filter conversion
<?php
function convert($size) {
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
function mem() {
return convert(memory_get_usage(true));
}
function timer()
{
static $microtime_start = null;
return $microtime_start === null ? $microtime_start = microtime(true) : microtime(true) - $microtime_start;
}
function status($prefix) {
echo $prefix . ': ' . mem() . ' :: ' .timer() ."\n";
}
status('init');
$base64File = 'filterbase64.Complex_GER_V1.pdfbase64';
status('bootup');
$base64Content = file_get_contents($base64File);
status('get-content');
$content = base64_decode($base64Content);
status('in-memory-decode');
file_put_contents('put.decoded.pdf', $content);
status('put-content');
$fp = fopen('filter.decoded.pdf', 'w+');
stream_filter_append($fp, 'convert.base64-decode');
fwrite($fp, $base64Content);
fclose($fp);
status('put-filter');
<?php
function convert($size) {
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
function mem() {
return convert(memory_get_usage(true));
}
function timer()
{
static $microtime_start = null;
return $microtime_start === null ? $microtime_start = microtime(true) : microtime(true) - $microtime_start;
}
function status($prefix) {
echo $prefix . ': ' . mem() . ' :: ' .timer() ."\n";
}
status('init');
$pdf = 'Complex_GER_V1.pdf';
$base64Out = 'base64.' . $pdf . 'base64';
status('bootup');
$content = file_get_contents($pdf);
status('get-content');
$base64Content = base64_encode($content);
status('in-memory-encode');
file_put_contents('put' . $base64Out, $base64Content);
status('put-content');
$fp = fopen('filter' . $base64Out, 'w+');
stream_filter_append($fp, 'convert.base64-encode');
fwrite($fp, $content);
fclose($fp);
status('put-filter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment