sample #07
converting image to swf
  1. <?php
  2. include("sample_utils.php"); // provides some utils and using mk
  3.  
  4. $imageFN = "85569310_beac638d2f.jpg"; // source image file name
  5.  
  6. $bm = new Bitmap_item(); // mk bitmap item class
  7. $bmTag = $bm->create_from_file($imageFN); // create swf tag from file
  8. $bitmapWidth = $bm->get_bitmapWidth(); //
  9. $bitmapHeight = $bm->get_bitmapHeight(); // width, height of image
  10.  
  11. $mad = array(); // this ...
  12. $mad["x"] = 150; // ... array is used ...
  13. $mad["y"] = 20; // ... to place bitmap
  14. $mad["width"] = $bitmapWidth; // ... properly
  15. $mad["height"] = $bitmapHeight; //
  16.  
  17. $shape1 = new MKshape_container("", 2); // the shape that will wrap bitmap
  18.  
  19. // add a bitmap fill, there are some kind
  20. // for a single bounded image use CLIPPED_BITMAP_FILL
  21. // the last parameter will place the bitmap in the desired position
  22. // note that in this case we set position for bitmap, not for shape itself
  23. $shape1->add_bitmap_fill(1, CLIPPED_BITMAP_FILL, $mad);
  24. $shape1->add_rect($mad["x"], $mad["y"], $mad["width"], $mad["height"]); // image bounding box
  25.  
  26. $shapeTD1 = $shape1->update_tagData(); // get shape tag
  27. $shapePB1 = $shape1->update_place_tagData();
  28.  
  29. $swf = new MKswf("bitmap.swf"); // start a fresh swf
  30. $swf->set_swfDir(SERVER_MKTMP); // change default working dir
  31. $swf->set_backgroundColor("#FF9933"); // change background color
  32. $swf->add_tagData($bmTag); // populate with image
  33. $swf->add_tagData($shapeTD1); // populate with shape wrapper
  34. $swf->add_tagData($shapePB1); // place on stage
  35. $swf->swf_output(); // save to disk
  36.  
  37. $swfHeader = $swf->fetch_assoc();
  38.  
  39. $str = "<br />" .render_swf_box($swfHeader, $imgDir, false, false, true);
  40. $str .= "<br/><br/><strong>bitmap.swf created in " .SERVER_MKTMP ."</strong>";
  41. echo show_content($str, basename(__FILE__));
  42. ?>

there are some issues to consider when including image in an swf. Flash supports various bitmap type combined in different behaviour, e.g. lossy or lossless image, 8, 15, 24 bit/pixel and alpha channel stored with lossy o lossless bitmap.

Mk try to manage all bitmap type, betting the best way to store data. If you load a png with transparency mk will preserve alpha channel. Jpegs does not support alpha channel, so if you are going to include a lossy image + alpha channel you must deal with Bitmap_item by some of its methods, first create_from_file, and then create_alpha_from_file, picking data from a separate bitmap (grayscaled png is preferable).
Mk can also manage jpeg quality, see the reference.
Note that I used a shape container with add_rect to bound image, but you may prefer what shape you want, e.g. circle/ellipse or a weirdest amoeba contour.

Some words about bitmap fill
Flash supports some bitmap fill type, clipped and its variant clipped non-smoothed are the most used in swf, because they give their typical look (a simple image that you can view by any image viewer), but there are some nice variation, such repeating and repeating non-smoothed tiled image, so useful in games.
Using mk you can load images as tiled background, flash 8 now support this features also at runt-time, but earlier version does not.
Combining alpha channel with tiled image you may reach unexplored result without modifying your web application, simply updating the source file name.

mikrokosmos