Creating Anaglyphs with PHP
I created some stereographic pairs yesterday and used some online software to create an anaglyph for viewers with red and cyan glasses. Creating anaglyphs is in fact a really easy task. All that has to be done is to separate the red layer from the left image and the green and blue layers from the right image. Finally the three layers are combined to create a colour anaglyph. Simple.
Having a little experience with PHP, I decided to create a simple script to create anaglyphs. First I needed to plan out how the script was going to work. The GD library has no native image filters except in PHP5. My plan works like so.
Find the size of the image
For every x coordinate
For every y coordinate
Get the red value of the left pixel
Get the green value of the right pixel
Get the blue value of the right pixel
Set the result pixel to red + green + blue
Output image
In PHP this results in:
// The file for the left eye
$left = 'left.jpg';
// The file for the right eye
$right = 'right.jpg';
// Get the dimensions
list($width, $height) = getimagesize($left);
// Define our source images
$src_right = imagecreatefromjpeg($right);
$src_left = imagecreatefromjpeg($left);
// Create the canvas
$bwimage= imagecreatetruecolor($width, $height);
//Reads the origonal colors pixel by pixel
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
$rgb_left = imagecolorat($src_left,$x,$y);
$r = ($rgb_left >> 16) & 0xFF;
$rgb_right = imagecolorat($src_right,$x,$y);
$g = ($rgb_right >> 8) & 0xFF;
$b = $rgb_right & 0xFF;
//This is where we create the color which is a mix of the red, green and blue channels
$color = imagecolorallocate($bwimage,$r,$g,$b);
imagesetpixel($bwimage,$x,$y,$color);
}
}
// This sets it to a .jpg, but you can change this to png or gif if that is what you are working with
header('Content-type: image/jpeg');
imagejpeg($bwimage);
?>
I didn’t think this would work at first because it seems too simple. My tests revealed the images I was outputing were the same as other programmes so it was working. Having said that since I cant find any red and cyan glasses I can’t really test it properly! Can anyone else verify the images below?


Update: If you want to create your own anaglyphs why not use my free anaglyph creator.
You can leave a comment, or trackback from your own site.
On June 30th, 2008 Swell 3D said…
Mars Phoenix and the Milburn Anaglyph Creator…
Thomas Milburn, a student in Berkshire, England, has written a nifty PHP program that generates a red/cyan color anaglyph from any two photos you upload from an HTML form. (Although, being English, Milburn might prefer the spellings “nifftea,” “prog…
Yes, the 3D images work just fine with glasses (though I hope you’ll have found yours by now). Good work!
What sort of jet is that? Is it a Sukhoi Su27?
@Sean
I’m glad you like my php program. Your site is wicked with all the 3D stuff. I’m afraid I haven’t a clue what the jet is.
to work this php script how can I find left.jpg and right.jpg of one photo ?
@Alp
Actually you need two photos. One taken from slightly to the left and one taken from slightly to the right. These are your left.jpg and right.jpg images.
Leave a Comment
Search
Archives
Categories
Blogroll
About Me
I’m Thomas Milburn the owner and designer of this blog. I really enjoy designing and developing websites. Although I only made my first proper website three years ago, I’m now fluent in standards compliant HTML and CSS and I know a good deal of PHP and SQL. I enjoy coding in PHP especially designing stuff for WordPress. At the moment I’m improving my JavaScript skills, getting to grips with JQuery and learning Python.
I’m in the upper sixth at Kennet school in Thatcham and am doing my A levels. I’m studying maths, chemistry, physics and french. Hopefully next year I’m going to study engineering at university.
In my free time I enjoy going out cycling or running. I also swim for Newbury Swimming Club and compete in cross country and 1500m for Team Kennet. I’m in the Berkshire Team for cross country and have ran in the nationals for the last few years.
Photos
Proudly powered by WordPress with plane theme by me! | © 2007 Thomas Milburn | Valid CSS and HTML