PHP Classes

File: sample.php

Recommend this page to a friend!
  Classes of Joshua hatfield   floStereoImg   sample.php   Download  
File: sample.php
Role: Example script
Content type: text/plain
Description: Example of how to use
Class: floStereoImg
Generate stereo images (3D) from eye view images
Author: By
Last change: Completely rewritten sample. Let's consider this an 2.0 alpha version of the sample file. It has been very lightly tested. It's late though and I will do more testing tomorrow. This new version should address every aspect of the class you will likely use and could nearly be considered itself documentation of the functionality.
Date: 12 years ago
Size: 8,733 bytes
 

Contents

Class file image Download
<?
// Run this sample from the same directory where the floStereoImg.php file is,
// or adjust the path here:
include("floStereoImg.php");

// The following are flags which can be passed into the instantiation.
// The values listed below are also the defaults which can be applied by
// ommitting in the instantiation or giving it null.

// This flag is currently untested, use at your own risk. The goal is that
// temp files will be used to store the images instead of image resources.
$use_cache_files = false;
// Also settable in an instantiated class with this method:
// set_cache_to_temp_folder($use_cache_files)

// This flag is well tested. It specifies that any image resources the class
// uses but does not need will be destroyed.
$destroy_objects = true;
// Also settable in an instantiated class with this method:
// set_aggressive_resource_destruction($destroy_objects)

// This flag instructs the class to automatically crop any generated stereo
// images. This is only important if you adjust the offset of the image.
$auto_crop = true;
// Also settable in an instantiated class with this method:
// set_cache_to_temp_folder($use_cache_files)

// This flag sets which method of anaglyph production (red/cyan, etc.) is
// created with generate_anaglyph_resource(), imagepng_anaglyph($file_path = null)
// and imagejpeg_anaglyph($file_path = null).
$channel_map = "floRC-C";
// Also settable in an instantiated class with this method:
// set_anaglyph_channel_map($channel_map)

// The list of available channel maps can be found in the method:
// get_supported_channel_maps(). Pass in the KEY of the Key/Value pair.
// The Value is a human readable description.

// To use this class, we need an instance.
$flostereoimg = new floStereoImg($use_cache_files, $destroy_objects, $auto_crop, $channel_map);

// To try the different methods of loading the stereo image, change this value:
$image_load_method = 1;
switch(
$image_load_method) {
    case
1:
       
// Method 1 (for this sample, not significant for the class itself):
        // Load a cross-eye image path. (Uses image_create_from_string to
        // automatically determine the type of image.)
       
$flostereoimg->set_stereo_image_location("stereo.pns");
        break;
    case
2:
       
// Method 2:
        // Load a cross-eye image resource. Loading by resource is clearly
        // more useful if you already have a resource you're manipulating.
       
$stereo_image = imagecreatefrompng("stereo.pns");
       
$flostereoimg->set_from_stereo_resource($stereo_image);
        break;

// Note, the standard is that the eyes will be on the opposite side of the
// image from their location on the head. This is because the standard was
// made to easily convert from cross-eye files which have long since been
// available and standard. Occationally, a parallel-eye image exists, and so
// I've added the swap_eyes() method to make conversion convenient.

   
case 3:
       
// Method 3:
        // Load a parallel-eye image path.
       
$flostereoimg->set_stereo_image_location("parallel.png");
       
$flostereoimg->swap_eyes();
        break;
    case
4:
       
// Method 4:
        // Load a parralel-eye image resource.
       
$stereo_image = imagecreatefrompng("parallel.png");
       
$flostereoimg->set_from_stereo_resource($stereo_image);
       
$flostereoimg->swap_eyes();
        break;
    case
5:
       
// Method 5:
        // Load each seperate eye image path.
       
$flostereoimg->set_left_image("left.png");
       
$flostereoimg->set_right_image("right.png");
        break;
    case
6:
       
// Method 6:
        // Load each seperate eye image resource. Do not destroy the
        // image resouces passed in because that will remove the reference
        // inside the class itself as well.
       
$left_image_resource = imagecreatefrompng("left.png");
       
$flostereoimg->set_left_resource($left_image_resource);

       
$right_image_resource = imagecreatefrompng("right.png");
       
$flostereoimg->set_right_resource($right_image_resource);
        break;
}

// After the source images have been loaded we can adjust and output them:

// To try different adjustment's, set the value here:
$adjustment_method = 1;

switch (
$adjustment_method) {
    case
1:
       
// Method 1:
        // If the right and left images are not correctly aligned, you
        // can move them around in the resulting stereo images:

        // These define the movement of the left image relative to the right:
       
$x = 10;
       
$y = 5;

       
// If you have made any previous adjustments, setting this to true will
        // add the new adjustments to the previous. This is useful if you are
        // somehow previewing the output while keeping the floStereoImg obejct
        // instantiated.
       
$relative = false;

       
$flostereoimg->set_offset_correction($x, $y, $relative);
        break;
    case
2:
       
// Method 2:
        // If you have 2 points on the left and right you want to be at
        // the same place in the stereo results, you can speficy them and
        // the offsets will be generated automatically:
       
$right_x = 120;
       
$right_y = 200;
       
$left_x = 122;
       
$left_y = 198;

       
$flostereoimg->set_offset_correction_stereo($right_x, $right_y, $left_x, $left_y);
        break;
    case
3:
       
// Method 3:
        // If you have a need to change the flags initially passed into the
        // class, you can do so using these methods (the values here are the
        // opposite of the default values set on class instantiation):
       
$flostereoimg->set_aggressive_resource_destruction(false);
       
$flostereoimg->set_auto_crop(false);

       
// Again, this method is completely untested. It will be tested...it just isn't yet.
       
$flostereoimg->set_cache_to_temp_folder(true);

       
// You can also change the channel_map on the class. This is useful if
        // you want to output an anaglyph image in one style (e.g. red/cyan)
        // and then another (e.g. magenta/green):

        // Remember that it is the KEY of the array here that you pass back into the function:
       
$possible_channel_maps = array_keys($flostereoimg->get_supported_channel_maps());

       
// For this example, I'll just get a random channel map.
       
shuffle($possible_channel_maps);
       
$random_channel_map = array_shift($possible_channel_maps);

       
$flostereoimg->set_anaglyph_channel_map($channel_map);
        break;
}



// To try the different methods of output, change this value:
$output_method = 1;

switch (
$output_method) {
    case
1:
       
// Method 1:
        // Write the left and right eyes to seperate image files.
        // NOTE: The swap_eye() method only affects stereo output.
       
$flostereoimg->imagejpeg_left("left.jpg");
       
$flostereoimg->imagejpeg_right("right.jpg");
       
$flostereoimg->imagepng_left("left.png");
       
$flostereoimg->imagepng_right("right.png");
        break;
    case
2:
       
// Method 2:
        // Write the standard stereo images to seperate image files.
       
$flostereoimg->imagejps("stereo.jps");
       
$flostereoimg->imagepns("stereo.pns");

       
// Alternatively, since technically these are standard jpeg and png
        // files respectively, you can use those file extentions:
       
$flostereoimg->imagejps("stereo.jpg");
       
$flostereoimg->imagepns("stereo.png");
        break;

    case
3:
       
// Method 3:
        // Output parallel-eye format images. I am not including jps and
        // pns because parallel-eye format is not compatible with these
        // file extensions. Parallel-eye format does not appear to be a
        // well accepted standard.

        // The key difference is that the eyes are on the opposite side of the
        // image:
       
$flostereoimg->swap_eyes();

       
$flostereoimg->imagejps("parallel.jpg");
       
$flostereoimg->imagepns("parallel.png");

       
// But you must put them back or else any other calls will have the
        // eyes reversed:
       
$flostereoimg->swap_eyes();
        break;
    case
4:
       
// Method 4:
        // This is why you came. Output an anaglyph image:

        // First, though I have provided it, I do not recommend jpeg output
        // because jpeg compression distorts the anaglyph image.
       
$flostereoimg->imagejpeg_anaglyph("anaglyph.jpg");

       
// png output, on the other hand does not.
       
$flostereoimg->imagepng_anaglyph("anaglyph.png");
        break;
    case
5:
       
// Method 5:
        // Get an anaglyph image resource. Use if you have further
        // php processing you would like to do on the image before
        // outputting it.
       
$anaglyph_image = $flostereoimg->generate_anaglyph_resource();
        break;
    case
6:
       
// Method 6:
        // Get a stereo image resource. This will return the standard
        // cross eye image side by side unless passed true.
       
$give_me_unstandard_parallel_eye_instead = false;
       
$stereo_image = $flostereoimg->generate_stereo_resource($give_me_unstandard_parallel_eye_instead);

}

?>