
- Drop me a line! Contact me here.
Steganography With GD
I decided to use a nice picture of a butterfly.

As with most image scripts the first thing to do is setup and open the two image files as GD resources. This is basic stuff.
The next stage is to loop through the pixels of the source image.
Finally we have to encode the message into the destination image. This is where the binary maths process occurs. Using "&~" (and not) makes the least significant bit of the value 0, while using "|" (or) sets it to 1. We use the "&~" operator if the pixel in the message file is black, and the "|" operator is anything else. Of course, this process could be applied to an image filtering script to extract particular colours, but thats going off on a tangent.
Once we have set the least significant bit to be the value of the message image we then set the colour and draw the pixel in the destination image, and we're ready to save it and send the image somewhere.

As with most image scripts the first thing to do is setup and open the two image files as GD resources. This is basic stuff.
<?php
$m = imagecreatefrompng("message.png");
$s = imagecreatefromjpeg("source.jpg");
$image = imagecreatetruecolor(imagesx($s),imagesy($s));
?>
The next stage is to loop through the pixels of the source image.
<?php
for ($x=0;$x<imagesx($s);$x++) {
for ($y=0;$y<imagesy($s);$y++) {
//Steganography process
}
}
?>
Finally we have to encode the message into the destination image. This is where the binary maths process occurs. Using "&~" (and not) makes the least significant bit of the value 0, while using "|" (or) sets it to 1. We use the "&~" operator if the pixel in the message file is black, and the "|" operator is anything else. Of course, this process could be applied to an image filtering script to extract particular colours, but thats going off on a tangent.
<?php
$rgb = imagecolorat($s,$x,$y);
$r=($rgb>>16)&0xFF;
$g=($rgb>>8)&0xFF;
$b=$rgb&0xFF;
$rgb = imagecolorat($m,$x,$y);
if ($rgb == 0) {
$r=$r&~1;
$g=$g&~1;
$b=$b&~1;
} else {
$r=$r|1;
$g=$g|1;
$b=$b|1;
}
$col = imagecolorallocate($output,$r,$g,$b);
imagesetpixel($output,$x,$y,$col);
?>
Once we have set the least significant bit to be the value of the message image we then set the colour and draw the pixel in the destination image, and we're ready to save it and send the image somewhere.
Comments
Comments are not currently being accepted for this article.
Sidebar
Published:
21/06/2007
Views:
4438
Author:
Chris Neale
Labels:
Print:
Sidebar:
Wikipedia steganography page
PHP image function reference
PHP image function reference
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.0415 seconds
