1. Go to this page and download the library: Download arakne/swf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
arakne / swf example snippets
// Include composer autoloader
SWF file
$file = new SwfFile('my_anim.swf');
// Check if the file is valid
if (!$file->valid()) {
echo 'Invalid SWF file';
exit(1);
}
// Now you can use $file to parse the SWF file
use Arakne\Swf\SwfFile;
use Arakne\Swf\Extractor\SwfExtractor;
use Arakne\Swf\Extractor\Drawer\Svg\SvgCanvas;
use Arakne\Swf\Parser\Structure\Record\Rectangle;
use Arakne\Swf\Extractor\Sprite\SpriteDefinition;
$file = new SwfFile('my_anim.swf');
// You can extract some resources directly from the SwfFile instance
// But if you want to extract multiple resources, it's better to use the SwfExtractor class for performance reasons
// Render a sprite exported with name "anim" to SVG.
// Note: the method toSvg() is not available for all character types, so check the type before calling it.
$svg = $file->assetByName('anim')->toSvg();
// Same as above, but using the character ID (doesn't need to be exported)
$svg = $file->assetById(42)->toSvg();
// You can also retrieve all exported assets from the SWF file
foreach ($file->exportedAssets() as $name => $asset) {
if ($asset instanceof SpriteDefinition) {
// Render each frame of the sprite as SVG
for ($f = 0; $f < $asset->framesCount(); $f++) {
$svg = $asset->toSvg($f);
}
}
}
// You can also extract the main animation timeline
$svg = $file->timeline()->toSvg();
// If you want more control over the extraction process, or if you want to extract multiple resources,
// you should use the SwfExtractor class. It improves performance by caching processed sprites and shapes in memory.
$extractor = new SwfExtractor($file);
// Get all shapes present in the SWF file
foreach ($extractor->shapes() as $shape) {
// Render as SVG string
$svg = $shape->toSvg();
// Get the bounding box of the shape for an accurate placement (if needed)
// Note: bounds are in twips. Divide by 20 to get pixels.
$bounds = $shape->bounds();
// You can apply a color transform to the shape
$transformed = $shape->transformColors(new \Arakne\Swf\Parser\Structure\Record\ColorTransform(redMult: 128));
}
// Get all sprites present in the SWF file
foreach ($extractor->sprites() as $sprite) {
// Render as SVG string
$svg = $sprite->toSvg();
// You can also render any frame of the sprite
$framesCount = $sprite->framesCount();
$otherFrame = $sprite->toSvg(2);
// Like shapes, sprites have a bounding box
$bounds = $sprite->bounds();
}
// Get all raster images present in the SWF file
foreach ($extractor->images() as $image) {
// Render as PNG string
$png = $image->toPng();
// Render as JPEG string with 70% quality
$jpeg = $image->toJpeg(70);
}
// Extract the main animation timeline
$anim = $extractor->timeline();
$framesCount = $anim->framesCount();
// Render all frames as SVG
foreach ($anim->toSvgAll() as $frame => $svg) {
// Process the SVG string
}
// You can also render a single frame
$svg = $anim->toSvg(15);
// Extract a character by its ID
// It can be a shape, sprite or image
$character = $extractor->character(42);
// Create a new renderer engine.
$renderer = new SvgCanvas(new Rectangle(0, 1000, 0, 1000));
// Manually render the character as SVG
$svg = $character->draw($renderer)->render();
// Extract a character by its exported name
// It can be a shape, sprite or image
$character = $extractor->byName('my_sprite');
// You can also extract all exported characters
foreach ($extractor->exported() as $name => $id) {
$character = $extractor->character($id);
// When you work with large SWF files, you can reach the memory limit of PHP.
// In this case, you can call `releaseIfOutOfMemory()` during the extraction process to free memory.
// It can take as parameter the maximum memory usage in bytes, or leave it empty to use 75% of the memory limit.
// Note: release memory have a performance cost, so use it only when you really need it, or with high limit.
$extractor->releaseIfOutOfMemory();
}
// If you want to parse multiple SWF files, it's advised to call `release()` method on the extractor
// when you are done with it. This will free the memory used by the extractor and help the garbage collector.
$extractor->release();
use Arakne\Swf\SwfFile;
use Arakne\Swf\Extractor\SwfExtractor;
use Arakne\Swf\Extractor\Drawer\Converter\Converter;
use Arakne\Swf\Extractor\Drawer\Converter\FitSizeResizer;
$file = new SwfFile('my_anim.swf');
$extractor = new SwfExtractor($file);
// Create the converter
$converter = new Converter();
foreach ($extractor->sprites() as $sprite) {
// Render the sprite to the desired format
$png = $converter->toPng($sprite);
$jpeg = $converter->toJpeg($sprite);
$gif = $converter->toGif($sprite);
$webp = $converter->toWebp($sprite);
// You can also specify the frame to render
$png = $converter->toPng($sprite, 21);
}
// If you want to render as animated image, you can use the `toAnimatedGif()` or `toAnimatedWebp()` methods.
$anim = $converter->toAnimatedWebp($extractor->timeline(), $file->frameRate());
// You can also specify the desired size of the image, and the background color (which is useful for format which don't support transparency)
$converter = new Converter(
new FitSizeResizer(256, 256), // Resize to fit in a 256x256 box
'#333', // Background color. Supports hexadecimal format (e.g. '#FF0000' for red), named colors (e.g. 'red'), rgb() format (e.g. 'rgb(255, 0, 0)' for red), or rgba() format (e.g. 'rgba(255, 0, 0, 0.5)' for semi-transparent red)
);
// No more transparency issue: an opaque background is used
$img = $converter->toJpeg($extractor->byName('staticR'));
use Arakne\Swf\SwfFile;
use Arakne\Swf\Avm\Processor;
use Arakne\Swf\Avm\State;
$file = new SwfFile('my_anim.swf');
// To only extract variables as PHP array, you can use the method `variables()`
// Function calls and object calls are disabled by default
$vars = $file->variables();
// You can configure your own interpreter, if you want to enable function calls
// Note: the processor is stateless, so you can reuse it for multiple SWF files
$processor = new Processor(allowFunctionCall: true);
$vars = $file->variables($processor);
// If you want to keep the same context on multiple SWF files, you can instantiate a State
// which will be passed to the processor
// This state can also be used to provide custom functions from PHP
$state = new State();
// The function "my_custom_function" is now available in the AVM interpreter
$state->functions['my_custom_function'] = function () {
return 42;
};
$file->execute($state, $processor);
// Execute another SWF file with the same state
// So the context will be preserved
$otherSwf = new SwfFile('other_anim.swf');
$otherSwf->execute($state, $processor);
// Now you can access the variables from the two SWF files
$vars = $state->variables;
use Arakne\Swf\SwfFile;
use Arakne\Swf\Parser\Structure\Tag\DefineTextTag;
use Arakne\Swf\Parser\Swf;
$file = new SwfFile('my_anim.swf');
// Process all tags and iterate over them
foreach ($file->tags() as $pos => $tag) {
// The key is the tag position in the SWF file
$characterId = $pos->id;
$size = $pos->length;
if ($tag instanceof DefineTextTag) {
// Process the tag
}
}
// You can select tag types to process
foreach ($file->tags(11, 33) as $pos => $tag) {
// Process the tag
}
// If you want even lower API, you can use `Swf` class from `Parser` package.
$parser = Swf::fromString(file_get_contents('my_anim.swf'));
$header = $parser->header;
foreach ($parser->tags as $pos) {
$tag = $parser->parse($pos);
}
use Arakne\Swf\SwfFile;
$file = new SwfFile('my_anim.swf');
if (!$file->valid(1_000_000)) { // 1 MB is the maximum expected size
echo 'Invalid SWF file';
exit(1);
}
use Arakne\Swf\SwfFile;
use Arakne\Swf\Error\Errors;
// All errors will be ignored, and the library will try to recover from errors.
$failSafe = new SwfFile('my_anim.swf', errors: Errors::NONE);
// Strict error handling, which will throw an exception on any error.
$strict = new SwfFile('my_anim.swf', errors: Errors::ALL);
// Fine tune the error handling to ignore some errors
$strict = new SwfFile('my_anim.swf', errors: Errors::ALL & ~Errors::INVALID_TAG & ~Errors::UNPROCESSABLE_DATA & ~Errors::EXTRA_DATA);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.