使用PHP从SWF提取图像时保持PNG透明度

使用PHP从SWF提取图像时保持PNG透明度,php,png,flash,transparency,Php,Png,Flash,Transparency,我试图从SWF中提取图像。下面的代码可以工作,但在提取png图像时,背景会变得不透明。任何帮助都将是巨大的;这就是我所拥有的: class SWFextractImages { private $swf; private $jpegTables; private $shipID; public function doExtractImages($shipID, $b) { $this->shipID = $shipID; $this->swf = new SWF($

我试图从SWF中提取图像。下面的代码可以工作,但在提取png图像时,背景会变得不透明。任何帮助都将是巨大的;这就是我所拥有的:

class SWFextractImages {
private $swf;
private $jpegTables;
private $shipID;

public function doExtractImages($shipID, $b) {
    $this->shipID = $shipID;
    $this->swf = new SWF($b);
    $this->jpegTables = '';
    foreach ($this->swf->tags as $tag) {
        if($tag['type']==6){
            if($this->defineBits($tag)){
                continue;
            }
        }
    }
}

private function defineBits($tag) {
    $ret = $this->swf->parseTag($tag);
    $imageData = $ret['imageData'];
    if (strlen($this->jpegTables) > 0) {
        $imageData = substr($this->jpegTables, 0, -2) . substr($imageData, 2);
    }
    if($ret['characterId']==5){
        $filename = sprintf('images/'.$this->shipID.'.jpg', $ret['characterId']);
        file_put_contents($filename, $imageData);
        return true;
    }
    }

}

不确定这是否有帮助,但这也会从SWF文件中提取其他数据

函数defineBits仅适用于jpg图像

对于png,请尝试以下方法:

private function defineBitsLossless($tag) {
$ret = $this->swf->parseTag($tag);

$bitmapFormat = $ret['bitmapFormat'];
$bitmapWidth = $ret['bitmapWidth'];
$bitmapHeight = $ret['bitmapHeight'];
$pixelData = $ret['pixelData'];

$img = imageCreateTrueColor($bitmapWidth, $bitmapHeight);

if ($bitmapFormat == 3) {
    $colorTable = $ret['colorTable'];

    // Construct the colormap
    $colors = array();
    $i = 0;
    $len = strlen($colorTable);
    while ($i < $len) {
    $red = ord($colorTable[$i++]);
    $green = ord($colorTable[$i++]);
    $blue = ord($colorTable[$i++]);
    $colors[] = imageColorAllocate($img, $red, $green, $blue);
    }

    $bytesPerRow = $this->alignTo4bytes($bitmapWidth * 1); // 1 byte per sample

    // Construct the image
    for ($row = 0; $row < $bitmapHeight; $row++) {
    $off = $bytesPerRow * $row;
    for ($col = 0; $col < $bitmapWidth; $col++) {
        $idx = ord($pixelData[$off++]);
        imageSetPixel($img, $col, $row, $colors[$idx]);
    }
    }
} else if ($bitmapFormat == 4) {
    $bytesPerRow = $this->alignTo4bytes($bitmapWidth * 2); // 2 bytes per sample

    // Construct the image
    for ($row = 0; $row < $bitmapHeight; $row++) {
    $off = $bytesPerRow * $row;
    for ($col = 0; $col < $bitmapWidth; $col++) {
        $lo = ord($pixelData[$off++]);
        $hi = ord($pixelData[$off++]);
        $rgb = $lo + $hi * 256;
        $red = ($rgb >> 10) & 0x1f; // 5 bits
        $green = ($rgb >> 5) & 0x1f; // 5 bits
        $blue = ($rgb) & 0x1f; // 5 bits
        $color = imageColorAllocate($img, $red, $green, $blue);
        imageSetPixel($img, $col, $row, $color);
    }
    }
} else if ($bitmapFormat == 5) {
    $bytesPerRow = $this->alignTo4bytes($bitmapWidth * 4); // 4 bytes per sample

    // Construct the image
    for ($row = 0; $row < $bitmapHeight; $row++) {
    $off = $bytesPerRow * $row;
    for ($col = 0; $col < $bitmapWidth; $col++) {
        $off++; // Reserved
        $red = ord($pixelData[$off++]);
        $green = ord($pixelData[$off++]);
        $blue = ord($pixelData[$off++]);
        $color = imageColorAllocate($img, $red, $green, $blue);
        imageSetPixel($img, $col, $row, $color);
    }
    }
}
imagePNG($img, sprintf('images/img_%d.png', $ret['characterId']));
imageDestroy($img);
}
private函数definebitslessle($tag){
$ret=$this->swf->parseTag($tag);
$bitmapFormat=$ret['bitmapFormat'];
$bitmapWidth=$ret['bitmapWidth'];
$bitmapHeight=$ret['bitmapHeight'];
$pixelData=$ret['pixelData'];
$img=ImageCreateTureColor($bitmapWidth,$bitmapHeight);
如果($bitmapFormat==3){
$colorTable=$ret['colorTable'];
//构建颜色映射
$colors=array();
$i=0;
$len=strlen($colorTable);
而($i<$len){
$red=ord($colorTable[$i++]);
$green=ord($colorTable[$i++]);
$blue=ord($colorTable[$i++]);
$colors[]=imageColorAllocate($img、$red、$green、$blue);
}
$bytesPerRow=$this->alignTo4bytes($bitmapWidth*1);//每个示例1个字节
//构建形象
对于($row=0;$row<$bitmapHeight;$row++){
$off=$bytesPerRow*$row;
对于($col=0;$col<$bitmapWidth;$col++){
$idx=ord($pixelData[$off++]);
imageSetPixel($img,$col,$row,$colors[$idx]);
}
}
}else if($bitmapFormat==4){
$bytesPerRow=$this->alignTo4bytes($bitmapWidth*2);//每个示例2个字节
//构建形象
对于($row=0;$row<$bitmapHeight;$row++){
$off=$bytesPerRow*$row;
对于($col=0;$col<$bitmapWidth;$col++){
$lo=ord($pixelData[$off++]);
$hi=ord($pixelData[$off++]);
$rgb=$lo+$hi*256;
$red=($rgb>>10)&0x1f;//5位
$green=($rgb>>5)&0x1f;//5位
$blue=($rgb)&0x1f;//5位
$color=imageColorAllocate($img,$red,$green,$blue);
imageSetPixel($img,$col,$row,$color);
}
}
}else if($bitmapFormat==5){
$bytesPerRow=$this->alignTo4bytes($bitmapWidth*4);//每个示例4个字节
//构建形象
对于($row=0;$row<$bitmapHeight;$row++){
$off=$bytesPerRow*$row;
对于($col=0;$col<$bitmapWidth;$col++){
$off++;//保留
$red=ord($pixelData[$off++]);
$green=ord($pixelData[$off++]);
$blue=ord($pixelData[$off++]);
$color=imageColorAllocate($img,$red,$green,$blue);
imageSetPixel($img,$col,$row,$color);
}
}
}
imagePNG($img,sprintf($images/img_u%d.png',$ret['characterId'));
图像处理(img);
}

从出现的一些问题中,你可以阅读更多,理解更多,使用更多;看起来我们想要得到的图像类型是35,这是带有扩展功能的JPEG3。我们使用它获得了图像,但图像仍然不透明。你有什么想法吗?首先,a.jpg不能透明。你声称不透明的图像的扩展是什么?这些图像是png。必须如此,因为它们是SWF使用的渲染。但是当我们测试标记类型时,它返回35,即DefineBitsJPEG3。使用
defineBitsLossless($tag)
函数处理图像,看看会发生什么。我将case35更改为$ret=$this->parseDefineBitsLosslessTag($bytePosEnd,1);并强迫它尝试使用无损标签进行解析。我得到gzuncompress():数据错误。