Embed Images into a WebPage using PHP

Posted by admin in Coding, Tutorials, ... | 08.27.2006 - 5:32 am

It’s possible to embed images into your web-page using php, such that you have just one single file as your webpage. There are no links to other images, just one single file will contain the whole web-page including all the images in it.

To do this::
1. You need to encode your images using base64_encode() function in php.
You can write an elaborate code for it .. but you can use this simple script called base64img
Download the Script | See a Demo of the Script

2. As a result you will get a code looking something like this ::

function img()
{
header("Content-type: image/gif");
header("Content-length: 102");
echo base64_decode(
'AAABAAEAGRcAAAEAGABYBwAAFgAAACgAAAAZAAAALgAAAAEAGA'.
'AAAAAAAAAAAGAAAABgAAAAAAAAAAAAAAAzMzMzMzMzMzMzMzMz'.
'MzMzMzMzMzMzMzMzMzMzMzMvLzszMzMdHWYzMzMvLzozMzMzMz'.
'MzMzMzMzMzKipGMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMz'.
'MzMzMzMzMzMzTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
'');
}

much longer than this .. of course)

Create such functions for all the images you need to put up. Say you need to put 3 images, then rename the function to img_1(), img_2() and img_3() repectively and copy-paste it anywhere in your php file.
(as the encoded data is really long so better put at the end )

3. Now write the following code at the top of your php file

if (isset($_GET['img']))
{
switch ( $_GET['img'] )
{
case '1': img_1(); break;
case '2': img_2(); break;
case '3': img_3(); break;
default: break;
}
die();
}

Remember to put this code at the TOP of the php file. You should put it before you ‘echo’ anything else or write anything in html ( i.e. before ,etc tags ).
This is necessary as you may get “Warning: Cannot modify header information - headers already sent” error otherwise and it wont work.

4. Now you can embed image in that page or any other page using the image adress as.

http://www.yourwebsitename.com/yourpage.php?img=1

What we are doing here is that whenever we need an image, we call the php page with a query string of ‘?img=1′ attached. The page is executed and sends image data.

This method seems a bit complicated at first sight but it works well.

Uses:

1. We know that we can always link images to outside source, this method is not some hot-shot popular method. But its a possible workaround when you cannot upload images.

2. Very useful while creating your own php scripts and putting it for download over the web. You have just one file, not a folder.

3. Its possible to keep other data, like zip files, anything you want to put up, not just images, for download.

You can store the encoded data ( and the content-length ) in a mysql table too. This is sometimes used for large collection of images

(Source)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related Posts:
  • MyCyberTwin: Your Personal Chatterbot
  • Krishna Animated Epic Movie
  • Opera Mini 4 Beta Released
  • Three New Widgets From Technorati

  • 1 Comment on "Embed Images into a WebPage using PHP" »

    Very cool - I’ve run across this code in several scripts, including simplepie compatibility test and recognized what it was doing, but your explanation helped me to better understand it and to implement it in my own scripts.

    Leave a comment


    Close
    E-mail It