Classe:
<?php
/**
* SwfConvertion
*
* @author Bruno Soares
* @link http://www.bsoares.com.br
*/
class SwfConvertion
{
public function __construct () { }
/**
* image2swf
*
* @param $imagePath String
* @return void
*/
public static function image2swf($imagePath, $outputPath = '')
{
if ($outputPath == '')
{
$outputPath = SwfConvertion::resolveOutputPath($imagePath);
}
$bitmap = new SWFBitmap(file_get_contents($imagePath));
$shape = new SWFShape();
$shape->setRightFill($shape->addFill($bitmap));
$shape->drawLine($bitmap->getWidth(), 0);
$shape->drawLine(0, $bitmap->getHeight());
$shape->drawLine(-$bitmap->getWidth(), 0);
$shape->drawLine(0, -$bitmap->getHeight());
$movie = new SWFMovie();
$movie->setDimension($bitmap->getWidth(), $bitmap->getHeight());
$movie->add($shape);
$movie->save($outputPath);
}
/**
*
*
* @param $imagePath Object
* @return String
*/
private static function resolveOutputPath($imagePath)
{
return preg_replace('/^(.+)\.([A-Za-z]{3,4})$/i', '${1}.swf', $imagePath);
}
}
?>
Exemplo de uso:
<?php SwfConvertion::image2swf('imagem_branco.png', 'imagem_branco_2.swf'); ?>1º Teste:
PNG: imagem_branco.png 167KB
SWF: imagem_branco_2.swf 98KB
41,31% de compressão (menos 69KB)
2º Teste:
PNG: imagem_vermelho.png 309KB
SWF: imagem_vermelho_2.swf 264KB
14,56% de compressão (menos 45KB)
Obs.: É importante que você escolha bem as conversões que deseja fazer, pois em alguns casos o SWF pode ficar mais pesado que o PNG ou seja, se você vai utilizar este script para otimizar suas imagens compare os pesos antes de escolher a definitiva.
Conteúdo relacionado:
Post original: http://blog.bsoares.com.br/flash/converting-png-to-swf-with-php-5
PHP Shockwave Flash: http://br2.php.net/manual/pt_BR/book.swf.php
Funções para SWF: http://br2.php.net/manual/pt_BR/ref.swf.php
Enjoy

Del.icio.us 

0 comentários:
Postar um comentário