On June 3rd, 2008 milburn wrote…

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:

> 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.

6 Responses to “Creating Anaglyphs with PHP”

  1. 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…

  2. On June 30th, 2008 Sean Gleeson said…

    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?

  3. On June 30th, 2008 milburn said…

    @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.

  4. On September 24th, 2008 Alp said…

    to work this php script how can I find left.jpg and right.jpg of one photo ?

  5. On September 24th, 2008 milburn said…

    @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.

  6. On December 15th, 2013 Alamin said…

    Something is wrong with the left side image when converting to 3d please do a checkup.. Thanks..

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Proudly powered by WordPress with plane theme by me! | © 2007 - 2019 Thomas Milburn | Valid CSS and HTML