2014-09-04 27 views
0

我試圖從SWF中提取圖像。下面的代碼工作,但是當提取PNG圖像時,背景變得不透明。任何幫助都會很棒。這是我有:使用PHP從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文件中提取其他數據。 Here's the source code

回答

0

功能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); 
} 

你可以閱讀更多,瞭解更多,使用更從here

+0

一些問題上來;看起來我們試圖獲得的圖像類型是35,這是帶有guessextension函數的JPEG3。我們得到了使用它的圖像,但圖像仍然不透明。你有什麼想法? – user3245228 2014-09-04 09:02:52

+0

首先,.jpg不能是透明的。你聲稱不透明的圖像的擴展是什麼? – 2014-09-04 09:06:03

+0

圖片是PNG。它必須是,因爲它們是SWF使用的渲染。但是當我們測試標籤類型時,它返回35,這是DefineBitsJPEG3。 – user3245228 2014-09-04 09:07:37