[PHP] internally stored binary image

2003-03-20 Thread speedfreak
In order to distribute a single script I would like to store a small
image internally. Is this at all possible with PHP? How would I go about
it? Please feel free to rtfm me to the relevant sections.

Thx in advance,
[EMAIL PROTECTED]

PS: CC me please, as I'm on the digest...


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



Re: [PHP] internally stored binary image

2003-03-20 Thread Ernest E Vogelsinger
At 15:57 20.03.2003, [EMAIL PROTECTED] spoke out and said:
[snip]
In order to distribute a single script I would like to store a small
image internally. Is this at all possible with PHP? How would I go about
it? Please feel free to rtfm me to the relevant sections.
[snip] 

You could make some script to retrieve the hex values of the image byte by
byte, then create a source file like
$imgdata = '\xff\x19\x00\x12   etc...;

Then when you're about to send the image simply transmit the correct
header() and echo out the data.

This little sample works just perfectly:


// use one of your files here, of course...
$file = 'images/dart.gif';
$hf = fopen($file, 'r') or die(Can't open $file for reading);
$bin = fread($hf, filesize($file));
fclose($hf);
$asc = '$img_data = ';
for ($i = 0, $len = strlen($bin); $i  $len; ++$i) {
if ($i  !($i % 16))
$asc .= \ .\n\t\;
$asc .= '\\x' . bin2hex(substr($bin, $i, 1));
}
$asc .= ';';

// $asc now contains the hex-coded binary file.
// Example (a real gif - try it)
$img_data =
\x47\x49\x46\x38\x39\x61\x20\x00\x20\x00\xa2\xff\x00\x00\x00\x00 .
\xff\x00\x00\xff\xff\x00\x00\x00\xff\xc0\xc0\xc0\x00\x00\x00\x00 .
\x00\x00\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x04\x00\x2c\x00\x00 .
\x00\x00\x20\x00\x20\x00\x40\x03\xae\x48\xba\xac\xf0\x10\x34\x36 .
\x06\x83\x21\x87\xc8\xa3\xd0\xd0\x82\x65\xe3\xd6\x81\x0f\x2a\x89 .
\x25\x79\x6a\xe6\x33\xb1\x29\x6c\xab\x80\xd0\x94\x11\x8c\x7d\x31 .
\x80\xca\x51\x0b\xde\x70\xc3\x19\xa1\xc3\x8c\x28\x67\xcd\xe8\xa2 .
\x52\xb9\xb4\x82\x57\x41\xc8\x2a\x74\x15\x6f\xda\xe2\x96\x27\x7e .
\x0c\x04\x5a\xd5\x90\xfc\x6d\xf5\xbc\x34\x5f\xd4\x2d\x83\x5e\x8f .
\xc1\xe7\x93\xa9\xef\x2f\xe7\x75\x53\x54\x76\x6f\x36\x1d\x0a\x02 .
\x55\x3b\x77\x78\x26\x5a\x84\x5d\x5e\x4d\x19\x1f\x4e\x71\x38\x91 .
\x5e\x69\x49\x7f\x91\x8c\x94\x31\x49\x6c\x72\x39\x40\x5e\x6b\x65 .
\x9e\x66\x03\xa9\xa8\xaa\x99\x1c\xa4\xa2\xad\x8d\xa1\x70\x44\xb0 .
\x85\x92\xb9\x2b\xb8\xbc\x93\x38\x90\xb5\x86\x5b\x7b\xb1\x37\x87 .
\x7e\x34\x73\xca\xcd\xce\x09\x00\x3b; 

// Now write the stuff to a file
$hf = fopen($file.asc, 'w') or die(Can't open $file.asc for writing);
fwrite($hf, $asc);
fclose($hf);

// Here you may either include the file, or copy/paste its contents into
your own code.
// The next lines are just to test the stuff - adjust the Content-Type
header to your needs:

header('Content-Type: image/gif');
eval($asc);  // this will render the binary content into $img_data
echo $img_data;  // send it
exit;// and voila


Note that reading the file is only needed once as a tool to create the
ascii version of the binary file.


-- 
   O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/


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



Re: [PHP] internally stored binary image

2003-03-20 Thread speedfreak
Ernest E Vogelsinger wrote:

 At 15:57 20.03.2003, [EMAIL PROTECTED] spoke out and said:
 [snip]
 In order to distribute a single script I would like to store a small
 image internally. Is this at all possible with PHP? How would I go about
 it? Please feel free to rtfm me to the relevant sections.
 [snip]

 You could make some script to retrieve the hex values of the image byte by
 byte, then create a source file like
 $imgdata = '\xff\x19\x00\x12   etc...;

 Then when you're about to send the image simply transmit the correct
 header() and echo out the data.

 This little sample works just perfectly:

 
 // use one of your files here, of course...
 $file = 'images/dart.gif';
 $hf = fopen($file, 'r') or die(Can't open $file for reading);
 $bin = fread($hf, filesize($file));
 fclose($hf);
 $asc = '$img_data = ';
 for ($i = 0, $len = strlen($bin); $i  $len; ++$i) {
 if ($i  !($i % 16))
 $asc .= \ .\n\t\;
 $asc .= '\\x' . bin2hex(substr($bin, $i, 1));
 }
 $asc .= ';';

 // $asc now contains the hex-coded binary file.
 // Example (a real gif - try it)
 $img_data =
 \x47\x49\x46\x38\x39\x61\x20\x00\x20\x00\xa2\xff\x00\x00\x00\x00 .
 \xff\x00\x00\xff\xff\x00\x00\x00\xff\xc0\xc0\xc0\x00\x00\x00\x00 .
 \x00\x00\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x04\x00\x2c\x00\x00 .
 \x00\x00\x20\x00\x20\x00\x40\x03\xae\x48\xba\xac\xf0\x10\x34\x36 .
 \x06\x83\x21\x87\xc8\xa3\xd0\xd0\x82\x65\xe3\xd6\x81\x0f\x2a\x89 .
 \x25\x79\x6a\xe6\x33\xb1\x29\x6c\xab\x80\xd0\x94\x11\x8c\x7d\x31 .
 \x80\xca\x51\x0b\xde\x70\xc3\x19\xa1\xc3\x8c\x28\x67\xcd\xe8\xa2 .
 \x52\xb9\xb4\x82\x57\x41\xc8\x2a\x74\x15\x6f\xda\xe2\x96\x27\x7e .
 \x0c\x04\x5a\xd5\x90\xfc\x6d\xf5\xbc\x34\x5f\xd4\x2d\x83\x5e\x8f .
 \xc1\xe7\x93\xa9\xef\x2f\xe7\x75\x53\x54\x76\x6f\x36\x1d\x0a\x02 .
 \x55\x3b\x77\x78\x26\x5a\x84\x5d\x5e\x4d\x19\x1f\x4e\x71\x38\x91 .
 \x5e\x69\x49\x7f\x91\x8c\x94\x31\x49\x6c\x72\x39\x40\x5e\x6b\x65 .
 \x9e\x66\x03\xa9\xa8\xaa\x99\x1c\xa4\xa2\xad\x8d\xa1\x70\x44\xb0 .
 \x85\x92\xb9\x2b\xb8\xbc\x93\x38\x90\xb5\x86\x5b\x7b\xb1\x37\x87 .
 \x7e\x34\x73\xca\xcd\xce\x09\x00\x3b;

 // Now write the stuff to a file
 $hf = fopen($file.asc, 'w') or die(Can't open $file.asc for writing);
 fwrite($hf, $asc);
 fclose($hf);

 // Here you may either include the file, or copy/paste its contents into
 your own code.
 // The next lines are just to test the stuff - adjust the Content-Type
 header to your needs:

 header('Content-Type: image/gif');
 eval($asc);  // this will render the binary content into $img_data
 echo $img_data;  // send it
 exit;// and voila
 

 Note that reading the file is only needed once as a tool to create the
 ascii version of the binary file.

 --
O Ernest E. Vogelsinger
(\) ICQ #13394035
 ^ http://www.vogelsinger.at/

BullsEye indeed! After adjusting for win32 (added 'b' to fopen's) this script
is a little gem. You really saved me quite some headaches. Thanks!
-speedfreak


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