Skip to content

Instantly share code, notes, and snippets.

@okonomi
Created September 28, 2010 10:14
Show Gist options
  • Save okonomi/600752 to your computer and use it in GitHub Desktop.
Save okonomi/600752 to your computer and use it in GitHub Desktop.
<?php
require_once 'IO/SWF/Editor.php';
require_once 'IO/SWF/JPEG.php';
class File_SWF
{
const SWF_TAG_End = 0;
const SWF_TAG_ShowFrame = 1;
const SWF_TAG_DefineShape = 2;
const SWF_TAG_PlaceObject = 4;
const SWF_TAG_RemoveObject = 5;
const SWF_TAG_DefineBits = 6;
const SWF_TAG_DefineButton = 7;
const SWF_TAG_JPEGTables = 8;
const SWF_TAG_SetBackgroundColor = 9;
const SWF_TAG_DefineFont = 10;
const SWF_TAG_DefineText = 11;
const SWF_TAG_DoAction = 12;
const SWF_TAG_DefineFontInfo = 13;
const SWF_TAG_DefineSound = 14;
const SWF_TAG_StartSound = 15;
const SWF_TAG_DefineButtonSound = 17;
const SWF_TAG_SoundStreamHead = 18;
const SWF_TAG_SoundStreamBlock = 19;
const SWF_TAG_DefineBitsLossless = 20;
const SWF_TAG_DefineBitsJPEG2 = 21;
const SWF_TAG_DefineShape2 = 22;
const SWF_TAG_DefineButtonCxform = 23;
const SWF_TAG_Protect = 24;
const SWF_TAG_PlaceObject2 = 26;
const SWF_TAG_RemoveObject2 = 28;
const SWF_TAG_DefineShape3 = 32;
const SWF_TAG_DefineText2 = 33;
const SWF_TAG_DefineButton2 = 34;
const SWF_TAG_DefineBitsJPEG3 = 35;
const SWF_TAG_DefineBitsLossless2 = 36;
const SWF_TAG_DefineEditText = 37;
const SWF_TAG_DefineSprite = 39;
const SWF_TAG_FrameLabel = 43;
const SWF_TAG_SoundStreamHead2 = 45;
const SWF_TAG_DefineMorphShape = 46;
const SWF_TAG_DefineFont2 = 48;
const SWF_TAG_ExportAssets = 56;
const SWF_TAG_ImportAssets = 57;
const SWF_TAG_EnableDebugger = 58;
const SWF_TAG_DoInitAction = 59;
const SWF_TAG_DefineVideoStream = 60;
const SWF_TAG_VideoFrame = 61;
const SWF_TAG_DefineFontInfo2 = 62;
const SWF_TAG_EnableDebugger2 = 64;
const SWF_TAG_ScriptLimits = 65;
const SWF_TAG_SetTabIndex = 66;
const SWF_TAG_FileAttributes = 69;
const SWF_TAG_PlaceObject3 = 70;
const SWF_TAG_ImportAssets2 = 71;
const SWF_TAG_DefineFontAlignZones = 73;
const SWF_TAG_CSMTextSettings = 74;
const SWF_TAG_DefineFont3 = 75;
const SWF_TAG_SymbolClass = 76;
const SWF_TAG_Metadata = 77;
const SWF_TAG_DefineScalingGrid = 78;
const SWF_TAG_DoABC = 82;
const SWF_TAG_DefineShape4 = 83;
const SWF_TAG_DefineMorphShape2 = 84;
const SWF_TAG_DefineSceneAndFrameLabelData = 86;
const SWF_TAG_DefineBinaryData = 87;
const SWF_TAG_DefineFontName = 88;
const SWF_TAG_StartSound2 = 89;
const SWF_TAG_DefineBitsJPEG4 = 90;
const SWF_TAG_DefineFont4 = 91;
const IMAGE_TYPE_NONE = 0;
const IMAGE_TYPE_JPEG = 1;
const IMAGE_TYPE_GIF = 2;
const IMAGE_TYPE_PNG = 3;
protected $_swfeditor;
function __construct($content)
{
$this->_swfeditor = new IO_SWF_Editor();
$this->_swfeditor->parse($content);
$this->_swfeditor->setCharacterId();
}
public function getSignature()
{
return $this->_swfeditor->_headers['Signature'];
}
public function getVersion()
{
return $this->_swfeditor->_headers['Version'];
}
public function getLength()
{
return $this->_swfeditor->_headers['FileLength'];
}
public function getFrameRate()
{
return $this->_swfeditor->_headers['FrameRate'] / 256;
}
public function getFrameCount()
{
return $this->_swfeditor->_headers['FrameCount'];
}
public function getBackgroundColor()
{
return unpack('C*', $this->_swfeditor->getTagContent(self::SWF_TAG_SetBackgroundColor));
}
public function setBackgroundColor($r, $g, $b)
{
$color = pack('C*', $r, $g, $b);
$this->_swfeditor->replaceTagContent(self::SWF_TAG_SetBackgroundColor, $color);
return $this;
}
public function setImage($image_id, $content, $image_type)
{
$tag_code = array(
self::SWF_TAG_DefineBits,
self::SWF_TAG_DefineBitsJPEG2,
self::SWF_TAG_DefineBitsJPEG3,
);
switch ($image_type) {
case self::IMAGE_TYPE_JPEG:
$jpeg = new IO_SWF_JPEG();
$jpeg->input($content);
$jpeg_table = $jpeg->getEncodingTables();
$jpeg_image = $jpeg->getImageData();
$content = pack('v', $image_id).$jpeg_table.$jpeg_image;
break;
case self::IMAGE_TYPE_GIF:
$content = pack('v', $image_id).$content;
break;
case self::IMAGE_TYPE_PNG:
$content = pack('v', $image_id).$content;
break;
}
$tag = array('Code' => self::SWF_TAG_DefineBitsJPEG2, 'Content' => $content);
$ret = $this->_swfeditor->replaceTagByCharacterId($tag_code, $image_id, $tag);
/* var_dump($ret); */
/* exit; */
return $this;
}
public function build()
{
return $this->_swfeditor->build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment