Convertendo PNG para SWF com PHP 5
Bom para quem não sabe o Adobe Flash consegue uma compressão incrível com arquivos do tipo PNG, mantendo a sua qualidade e transparência. Vou deixar um exemplo de código PHP para fazer essa conversão sem a ajuda de softwares como o png2swf.exe pois a realidade é que muitos servidores (hosts) não permitem que o script PHP rode softwares.
Classe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?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:
1 | <?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:
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
Posts relacionados:
