Jump to content


Photo

encoding images


  • Please log in to reply
3 replies to this topic

#1 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 13 September 2006 - 02:10 PM

Hi,

I`m wondering if anyone knows how to or a script/program that will encode images into base64 encoding?


TIA

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 September 2006 - 02:11 PM

<?php
echo base64_encode(file_get_contents("some_image.png"));
?>

That should do it.

#3 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 13 September 2006 - 02:28 PM

Excellent, thanks.

How could I reverse that?

Meaning if I include it into a script, how would I use the base64?

Something like:

<?php

$string = "encoded string";

$image = eval(base64_decode($string));

echo "<img src=\"$image\">";

?>


#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 September 2006 - 02:39 PM

To reverse it just:

image.php:
<?php
$string = "bla bla encoded image bla bla";
header("Content-type: ..."); // write the mime-type instead of the dots
echo base64_decode($string);
?>

other_file.html:
...
<img src='image.php' alt='image' />
...

Or you may do it like this if you want it in one file:

some_file.php:
<?php
$string = "bla bla encoded image bla bla";
if(isset($_GET['image']))
{
	header("Content-type: ..."); // write the mime-type instead of the dots
	echo base64_decode($string);
	die();
}

echo "<img src='some_file.php?image' alt='image' />";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users