Re: Include images in php file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2007-06-29 at 00:20 -0400, Robert Cummings wrote:
> On Thu, 2007-06-28 at 22:56 -0500, Larry Garfield wrote:
> > Skip raster images and just output the images as SVG.  OK, only works in 
> > Firefox 1.5+, but it would get you everything in one file without bloated 
> > encoding. :-)
> 
> No need to have bloated encoding.. just have the file check itself for a
> flag, if it is set then read itself, replace base64 data segments with
> binary data segments update flag, and resave, and redirect to self for
> proper request. Future requests will work with the deflated version
> directly. The only bloat will be the extra chars needed to properly
> escape special characters such that binary image data can be stored in a
> string. I suggest using single quote delimiters for the string :)

Just for fun I've whipped up a little script to illustrate the ease of
embedding images into your own PHP script without the hassle of manually
doing the embedding. I've only registered one image, but feel free to
register as many as you want. The first time the script loads it will
embed any image resources that are registered into itself. Then you can
send anyone the script. This technique can be done with any kind of
resource.

<?php

$build = true;

$images = array
(
    'banner' => 'banner.png',
);

$imagesImported = '[[IMPORTED_IMAGE_DATA]]';

checkBuild();
handleRequest();

function handleRequest()
{
    $type = isset( $_GET['type'] ) ? $_GET['type'] : null;
    $what = isset( $_GET['what'] ) ? $_GET['what'] : null;

    if( $type === 'image' )
    {
        showImage( $what );
    }
    else
    {
        showPage( $what );
    }
}

function showPage( $page )
{
    showHeader();

    echo '<p>Blah blah blah blah blah - Rob was here!</p>'."\n";

    showFooter();
}

function showHeader()
{
    $banner = htmlspecialchars( getResource( 'banner', 'image' ) );

    echo '<html>'
        .'<body>'
        .'<img src="'.$banner.'" />'
        .'<br /><br />';
}

function showFooter()
{
    echo '</body>'
        .'</html>';
}

function getResource( $what, $type=null )
{
    $me = $_SERVER['PHP_SELF'];

    $params = array();

    if( $type !== null )
    {
        $params['type'] = $type;
    }

    $params['what'] = $what;

    foreach( $params as $key => $value )
    {
        $params[$key] = urlencode( $key ).'='.urlencode( $value );
    }

    return $me.'?'.implode( '&', $params );
}

function showImage( $name )
{
    if( !isset( $GLOBALS['images'][$name] ) )
    {
        header( 'HTTP/1.0 404 Not Found' );
        exit();
    }

    $filename = $GLOBALS['images'][$name];

    if( !is_array( $GLOBALS['imagesImported'] ) )
    {
        $imageStuff = getImageStuff( $filename );
    }
    else
    {
        $imageStuff = $GLOBALS['imagesImported'][$name];
    }

    header( 'Content-type: '.$imageStuff['info']['mime'] );
    echo $imageStuff['content'];

    exit();
}

function getImageStuff( $filename )
{
    $path = ereg_replace( '/[^/]+$', '/', __FILE__ ).$filename;

    return
        array
        (
            'content' => implode( '', file( $path ) ),
            'info'    => getImageSize( $path ),
        );
}

function escapeLiteral( $value )
{
    $value = str_replace( '\\', '\\\\', (string)$value );
    $value = str_replace( "'", "\\'", $value );

    return "'".$value."'";
}

function convertToDeclaration( $anArray )
{
    $dec = 'array( ';

    foreach( $anArray as $key => $value )
    {
        if( is_array( $value ) )
        {
            $value = convertToDeclaration( $value );
        }
        else
        {
            $value = escapeLiteral( $value );
        }

        $key = escapeLiteral( $key );

        $dec .= $key.' => '.$value.', ';
    }

    $dec .= ' )';

    return $dec;
}

function checkBuild()
{
    if( is_array( $GLOBALS['imagesImported'] ) )
    {
        //
        // Already built :)
        //
        return;
    }

    if( !isset( $GLOBALS['build'] )
        ||
        !$GLOBALS['build'] )
    {
        //
        // Not enabled -- testing probably.
        //
        return;
    }

    $imported = array();
    foreach( $GLOBALS['images'] as $name => $filename )
    {
        $imported[$name] = getImageStuff( $filename );
    }

    $source = implode( '', file( __FILE__ ) );

    $source =
        str_replace
        (
            "'"."[[IMPORTED_IMAGE_DATA]]"."'",
            convertToDeclaration( $imported ),
            $source
        );

    if( ($fptr = fopen( __FILE__, 'w' )) !== false )
    {
        fwrite( $fptr, $source );
        fclose( $fptr );
    }
}

?>

-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux